Skip to content

Commit

Permalink
Provide the ability to disable scheduler monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Jun 16, 2016
1 parent b9d2fd9 commit b96c7d6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 3 additions & 1 deletion coveralls.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"def.+(.+\\\\.+).+do",
"^\\s+use\\s+"
],
"custom_stop_words": [ ],
"custom_stop_words": [
"def new"
],
"coverage_options": {
"treat_no_relevant_lines_as_covered": true
},
Expand Down
23 changes: 15 additions & 8 deletions lib/eternal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule Eternal do
- `:monitor` - determines which frequency will be used to check the state of
the `heir` server. It defaults to a minute, and should be set in milliseconds.
Setting this to `false` will disable this type of monitoring.
- `:quiet` - by default, Eternal logs debug messages. Setting this to true will
disable this logging.
Expand Down Expand Up @@ -70,8 +71,6 @@ defmodule Eternal do
heir(table, pid2)
gift(table, pid1)

monitor(table, pid1, opts)

table
end

Expand Down Expand Up @@ -168,11 +167,10 @@ defmodule Eternal do
# Attempts to catch termination notifications and forwards a message back to the
# owner of the table in order to inform it that the heir has died and a new one
# should be created.
def terminate({ :shutdown, :safe_exit }, _state),
do: nil
def terminate({ :shutdown, :safe_exit }, _state), do: nil
def terminate(_reason, { table, _opts }) do
if owner(table) != :undefined do
send(owner(table), { :"ETS-HEIR-TERMINATE", table })
if (owner = owner(table)) != :undefined do
send(owner, { :"ETS-HEIR-TERMINATE", table })
end
end

Expand Down Expand Up @@ -201,6 +199,14 @@ defmodule Eternal do
end)
end

# Determines the interval for a monitor. If the provided value is a positive
# number, we return it. Otherwise if it's set to false, we return `nil` (which)
# will cause the monitor to be disabled. For any other value, we return a default
# value of a minute (1000 * 60 * 60 milliseconds).
defp interval(value) when is_number(value) and value > 0, do: value
defp interval(false), do: nil
defp interval(_vals), do: :timer.minutes(1)

# Logs a debug message with a project prefix.
defp log(msg, opts) do
unless Keyword.get(opts, :quiet) do
Expand All @@ -211,8 +217,9 @@ defmodule Eternal do
# Sets a monitor to execute after a given delay. This is used to refresh the
# heir server if anything has happened to it.
defp monitor(table, owner, opts) do
time = Keyword.get(opts, :monitor, :timer.minutes(1))
:erlang.send_after(time, owner, { :"ETS-HEIR-MONITOR", table })
if time = interval(opts[:monitor]) do
:erlang.send_after(time, owner, { :"ETS-HEIR-MONITOR", table })
end
end

# Rescues an heir server by logging out the death, starting a new server, and
Expand Down

0 comments on commit b96c7d6

Please sign in to comment.