forked from quantum-elixir/quantum-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Node Selection into own Stage
- Loading branch information
Showing
13 changed files
with
300 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
defmodule Quantum.NodeSelectorBroadcaster do | ||
@moduledoc false | ||
|
||
# Receives Added / Removed Jobs, Broadcasts Executions of Jobs | ||
|
||
use GenStage | ||
|
||
require Logger | ||
|
||
alias Quantum.ExecutionBroadcaster.Event, as: ExecuteEvent | ||
alias Quantum.Job | ||
alias Quantum.RunStrategy.NodeList | ||
|
||
alias __MODULE__.{Event, InitOpts, StartOpts, State} | ||
|
||
@type event :: {:add, Job.t()} | {:execute, Job.t()} | ||
|
||
@doc """ | ||
Start Stage | ||
""" | ||
@spec start_link(StartOpts.t()) :: GenServer.on_start() | ||
def start_link(%StartOpts{name: name} = opts) do | ||
GenStage.start_link( | ||
__MODULE__, | ||
struct!( | ||
InitOpts, | ||
Map.take(opts, [ | ||
:execution_broadcaster_reference, | ||
:task_supervisor_reference | ||
]) | ||
), | ||
name: name | ||
) | ||
end | ||
|
||
@doc false | ||
def init(%InitOpts{ | ||
execution_broadcaster_reference: execution_broadcaster, | ||
task_supervisor_reference: task_supervisor_reference | ||
}) do | ||
{:producer_consumer, | ||
%State{ | ||
task_supervisor_reference: task_supervisor_reference | ||
}, subscribe_to: [execution_broadcaster]} | ||
end | ||
|
||
def handle_events(events, _, %{task_supervisor_reference: task_supervisor_reference} = state) do | ||
{:noreply, | ||
Enum.flat_map(events, fn %ExecuteEvent{job: job} -> | ||
job | ||
|> select_nodes(task_supervisor_reference) | ||
|> Enum.map(fn node -> | ||
%Event{job: job, node: node} | ||
end) | ||
end), state} | ||
end | ||
|
||
defp select_nodes(%Job{run_strategy: run_strategy} = job, task_supervisor) do | ||
run_strategy | ||
|> NodeList.nodes(job) | ||
|> Enum.filter(&check_node(&1, task_supervisor, job)) | ||
end | ||
|
||
@spec check_node(Node.t(), GenServer.server(), Job.t()) :: boolean | ||
defp check_node(node, task_supervisor, %{name: job_name}) do | ||
if running_node?(node, task_supervisor) do | ||
true | ||
else | ||
Logger.warn( | ||
"Node #{inspect(node)} is not running. Job #{inspect(job_name)} could not be executed." | ||
) | ||
|
||
false | ||
end | ||
end | ||
|
||
# Check if the task supervisor runs on a given node | ||
@spec running_node?(Node.t(), GenServer.server()) :: boolean | ||
defp running_node?(node, _) when node == node(), do: true | ||
|
||
defp running_node?(node, task_supervisor) do | ||
node | ||
|> :rpc.call(:erlang, :whereis, [task_supervisor]) | ||
|> is_pid() | ||
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,16 @@ | ||
defmodule Quantum.NodeSelectorBroadcaster.Event do | ||
@moduledoc false | ||
|
||
# Execute Event | ||
|
||
alias Quantum.Job | ||
|
||
@type t :: %__MODULE__{ | ||
job: Job.t(), | ||
node: Node.t() | ||
} | ||
|
||
@enforce_keys [:job, :node] | ||
|
||
defstruct @enforce_keys | ||
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,16 @@ | ||
defmodule Quantum.NodeSelectorBroadcaster.InitOpts do | ||
@moduledoc false | ||
|
||
# Init Options for Quantum.NodeSelectorBroadcaster | ||
|
||
@type t :: %__MODULE__{ | ||
execution_broadcaster_reference: GenServer.server(), | ||
task_supervisor_reference: GenServer.server() | ||
} | ||
|
||
@enforce_keys [ | ||
:execution_broadcaster_reference, | ||
:task_supervisor_reference | ||
] | ||
defstruct @enforce_keys | ||
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,18 @@ | ||
defmodule Quantum.NodeSelectorBroadcaster.StartOpts do | ||
@moduledoc false | ||
|
||
# Start Options for Quantum.NodeSelectorBroadcaster | ||
|
||
@type t :: %__MODULE__{ | ||
name: GenServer.server(), | ||
execution_broadcaster_reference: GenServer.server(), | ||
task_supervisor_reference: GenServer.server() | ||
} | ||
|
||
@enforce_keys [ | ||
:name, | ||
:execution_broadcaster_reference, | ||
:task_supervisor_reference | ||
] | ||
defstruct @enforce_keys | ||
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,15 @@ | ||
defmodule Quantum.NodeSelectorBroadcaster.State do | ||
@moduledoc false | ||
|
||
# Internal State | ||
|
||
@type t :: %__MODULE__{ | ||
task_supervisor_reference: GenServer.server() | ||
} | ||
|
||
@enforce_keys [ | ||
:task_supervisor_reference | ||
] | ||
|
||
defstruct @enforce_keys | ||
end |
Oops, something went wrong.