This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run a separate Batcher for each project
- Loading branch information
Showing
7 changed files
with
158 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
defmodule Aelita2.Batcher.Registry do | ||
@moduledoc """ | ||
The "Batcher" manages the backlog of batches that each project has. | ||
This is the registry of each individual batcher. | ||
It starts the batcher if it doesn't exist, | ||
restarts it if it crashes, | ||
and logs the crashes because that's needed sometimes. | ||
Note that the batcher and registry are always on the same node. | ||
Sharding between them will be done by directing which registry to go to. | ||
""" | ||
|
||
use GenServer | ||
|
||
alias Aelita2.Batcher | ||
alias Aelita2.Project | ||
alias Aelita2.Repo | ||
|
||
@name Aelita2.Batcher.Registry | ||
|
||
# Public API | ||
|
||
def start_link do | ||
GenServer.start_link(__MODULE__, :ok, name: @name) | ||
end | ||
|
||
def get(project_id) when is_integer(project_id) do | ||
GenServer.call(@name, {:get, project_id}) | ||
end | ||
|
||
# Server callbacks | ||
|
||
def init(:ok) do | ||
names = Project | ||
|> Repo.all() | ||
|> Enum.map(&{&1.id, do_start(&1.id)}) | ||
|> Map.new() | ||
refs = names | ||
|> Enum.map(&{Process.monitor(elem(&1, 1)), elem(&1, 0)}) | ||
|> Map.new() | ||
{:ok, {names, refs}} | ||
end | ||
|
||
def do_start(project_id) do | ||
{:ok, pid} = Batcher.Supervisor.start(project_id) | ||
pid | ||
end | ||
|
||
def handle_call({:get, project_id}, _from, {names, refs}) do | ||
{pid, names, refs} = case names[project_id] do | ||
nil -> | ||
pid = do_start(project_id) | ||
names = Map.put(names, project_id, pid) | ||
ref = Process.monitor(pid) | ||
refs = Map.put(names, ref, project_id) | ||
{pid, names, refs} | ||
pid -> | ||
{pid, names, refs} | ||
end | ||
{:reply, pid, {names, refs}} | ||
end | ||
|
||
def handle_info({:DOWN, ref, :process, _pid, _reason}, {names, refs}) do | ||
{project_id, refs} = Map.pop(refs, ref) | ||
names = Map.delete(names, project_id) | ||
{:noreply, {names, refs}} | ||
end | ||
|
||
def handle_info(_msg, state) do | ||
{:noreply, state} | ||
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,24 @@ | ||
defmodule Aelita2.Batcher.Supervisor do | ||
@moduledoc """ | ||
The supervisor of all of the batchers. | ||
""" | ||
use Supervisor | ||
|
||
@name Aelita2.Batcher.Supervisor | ||
|
||
def start_link do | ||
Supervisor.start_link(__MODULE__, :ok, name: @name) | ||
end | ||
|
||
def start(project_id) do | ||
Supervisor.start_child(@name, [project_id]) | ||
end | ||
|
||
def init(:ok) do | ||
children = [ | ||
worker(Aelita2.Batcher, [], restart: :temporary) | ||
] | ||
|
||
supervise(children, strategy: :simple_one_for_one) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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