Packages

This is a GenServer-ish implementation of a Service watcher. A process that is designed to poll service state every given number of miliseconds and perform due actions in case it is not in designated state. The process is also capable of sending notifications to the configured recipient in Slack.

Current section

Files

Jump to
service_watcher_sup lib supervisor job_supervisor.ex
Raw

lib/supervisor/job_supervisor.ex

defmodule JobSupervisor do
use DynamicSupervisor
import Service.Watcher, only: [service_name_alias: 1]
def start_link do
DynamicSupervisor.start_link(__MODULE__, [:one_for_one], name: __MODULE__)
end
def start_child(service_name, module, function, args, interval, period \\ :infinity, spawn_on_call \\ false) do
name_alias = service_name_alias(service_name)
spec =
%{
id: name_alias,
start:
{
TimerJob,
:start_link,
[
module, function, args, interval, period, spawn_on_call,
[name: name_alias]
]
},
restart: :permanent
}
# spec = Supervisor.Spec.worker(
# TimerJob,
# [
# module, function, args, interval, period, spawn_on_call,
# [name: name_alias]
# ], [restart: :permanent])
IO.puts "spec is: #{inspect spec}"
spec = spec |> Supervisor.child_spec(id: name_alias)
IO.puts "updated spec: #{inspect spec}"
child = DynamicSupervisor.start_child(__MODULE__, spec)
case child do
{:ok, pid} ->
pid |> TimerJob.run()
IO.puts "Started #{service_name} [#{inspect pid}] job right off"
end
child
end
def init([strategy]) do
DynamicSupervisor.init(strategy: strategy)
end
end