-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move observer crons into dedicated agents for parallelism
- Loading branch information
1 parent
f097886
commit ab3c7b8
Showing
12 changed files
with
105 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Console.Deployments.Observer.Discovery do | ||
alias Console.Deployments.Observer.Supervisor | ||
alias Console.Schema.Observer | ||
|
||
def runner(%Observer{} = observer), do: maybe_rpc(observer.id, Supervisor, :start_child, [observer]) | ||
|
||
defp maybe_rpc(id, module, func, args) do | ||
me = node() | ||
case worker_node(id) do | ||
^me -> apply(module, func, args) | ||
node -> :rpc.call(node, module, func, args) | ||
end | ||
end | ||
|
||
def worker_node(id), do: HashRing.Managed.key_to_node(:cluster, id) | ||
|
||
def local?(id), do: worker_node(id) == node() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Console.Deployments.Observer.Supervisor do | ||
use DynamicSupervisor | ||
alias Console.Deployments.Observer.Worker | ||
|
||
def start_link(init_arg \\ :ok) do | ||
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__) | ||
end | ||
|
||
def start_child(run) do | ||
DynamicSupervisor.start_child(__MODULE__, {Worker, run}) | ||
end | ||
|
||
@impl true | ||
def init(_init_arg) do | ||
DynamicSupervisor.init(strategy: :one_for_one) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Console.Deployments.Observer.Worker do | ||
use GenServer, restart: :transient | ||
alias Console.Schema.Observer | ||
alias Console.Deployments.Observer.{Runner, Discovery} | ||
|
||
require Logger | ||
|
||
@poll :timer.minutes(1) | ||
|
||
defmodule State, do: defstruct [:observer] | ||
|
||
def registry(), do: __MODULE__ | ||
|
||
def start_link([%Observer{} = obs]), do: start_link(obs) | ||
def start_link(%Observer{} = obs) do | ||
GenServer.start_link(__MODULE__, obs, name: via(obs)) | ||
end | ||
|
||
def init(observer) do | ||
:timer.send_interval(@poll, :poll) | ||
:timer.send_interval(@poll, :move) | ||
send self(), :poll | ||
{:ok, %State{observer: observer}} | ||
end | ||
|
||
def ping(pid), do: GenServer.call(pid, :ping) | ||
|
||
defp via(%Observer{id: id}), do: {:via, Registry, {registry(), {:observer, id}}} | ||
|
||
def handle_call(:ping, _, state), do: {:reply, :pong, state} | ||
|
||
def handle_info(:poll, %State{observer: %Observer{next_run_at: at} = observer} = state) do | ||
Logger.info "running observer #{observer.name}" | ||
with {:at, true} <- {:at, Timex.after?(Timex.now(), at)}, | ||
{:ok, observer} <- Runner.run(refetch(observer)) do | ||
Logger.info "ran observer #{observer.name}" | ||
{:noreply, %{state | observer: observer}} | ||
else | ||
{:at, _} -> | ||
Logger.info "cannot run observer #{observer.name} yet, next run at #{inspect(observer.next_run_at)}" | ||
{:noreply, state} | ||
{:error, err} -> | ||
Logger.warn "failed to run observer #{observer.name}, error: #{inspect(err)}" | ||
{:noreply, state} | ||
end | ||
end | ||
|
||
def handle_info(:move, %State{observer: observer} = state) do | ||
case Discovery.local?(observer) do | ||
true -> {:noreply, state} | ||
false -> {:stop, {:shutdown, :moved}, state} | ||
end | ||
end | ||
|
||
defp refetch(%Observer{id: id}), do: Console.Repo.get(Observer, id) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters