-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathexecutor.ex
153 lines (129 loc) · 3.99 KB
/
executor.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
defmodule Quantum.Executor do
@moduledoc false
# Task to actually execute a Task
use Task
require Logger
alias Quantum.{
Job,
NodeSelectorBroadcaster.Event,
TaskRegistry
}
alias __MODULE__.StartOpts
@spec start_link(StartOpts.t(), Event.t()) :: {:ok, pid}
def start_link(opts, %Event{job: job, node: node}) do
Task.start_link(fn ->
execute(opts, job, node)
end)
end
@spec execute(StartOpts.t(), Job.t(), Node.t()) :: :ok
# Execute task on all given nodes without checking for overlap
defp execute(
%StartOpts{
task_supervisor_reference: task_supervisor,
debug_logging: debug_logging,
scheduler: scheduler
},
%Job{overlap: true} = job,
node
) do
run(node, job, task_supervisor, debug_logging, scheduler)
:ok
end
# Execute task on all given nodes with checking for overlap
defp execute(
%StartOpts{
task_supervisor_reference: task_supervisor,
task_registry_reference: task_registry,
debug_logging: debug_logging,
scheduler: scheduler
},
%Job{overlap: false, name: job_name} = job,
node
) do
debug_logging &&
Logger.debug(fn ->
{"Start execution of job", node: Node.self(), name: job_name}
end)
case TaskRegistry.mark_running(task_registry, job_name, node) do
:marked_running ->
%Task{ref: ref} = run(node, job, task_supervisor, debug_logging, scheduler)
receive do
{^ref, _} ->
TaskRegistry.mark_finished(task_registry, job_name, node)
{:DOWN, ^ref, _, _, _} ->
TaskRegistry.mark_finished(task_registry, job_name, node)
:ok
end
_ ->
:ok
end
end
# Ececute the given function on a given node via the task supervisor
@spec run(Node.t(), Job.t(), GenServer.server(), boolean(), atom()) :: Task.t()
defp run(
node,
%Job{name: job_name, task: task} = job,
task_supervisor,
debug_logging,
scheduler
) do
debug_logging &&
Logger.debug(fn ->
{"Task for job started on node", node: Node.self(), name: job_name, started_on: node}
end)
Task.Supervisor.async_nolink({task_supervisor, node}, fn ->
debug_logging &&
Logger.debug(fn ->
{"Execute started for job", node: Node.self(), name: job_name}
end)
try do
:telemetry.span([:quantum, :job], %{job: job, node: node, scheduler: scheduler}, fn ->
result = execute_task(task)
{result, %{job: job, node: node, scheduler: scheduler, result: result}}
end)
catch
type, value ->
debug_logging &&
Logger.debug(fn ->
{
"Execution failed for job",
node: Node.self(), name: job_name, type: type, value: value
}
end)
log_exception(type, value, __STACKTRACE__)
else
result ->
debug_logging &&
Logger.debug(fn ->
{"Execution ended for job", node: Node.self(), name: job_name, result: result}
end)
end
:ok
end)
end
# Run function
@spec execute_task(Quantum.Job.task()) :: any
defp execute_task({mod, fun, args}) do
:erlang.apply(mod, fun, args)
end
defp execute_task(fun) when is_function(fun, 0) do
fun.()
end
def log_exception(kind, reason, stacktrace) do
reason = Exception.normalize(kind, reason, stacktrace)
# TODO: Remove in a future version and make elixir 1.10 minimum requirement
if Version.match?(System.version(), "< 1.10.0") do
Logger.error(Exception.format(kind, reason, stacktrace))
else
crash_reason =
case kind do
:throw -> {{:nocatch, reason}, stacktrace}
_ -> {reason, stacktrace}
end
Logger.error(
Exception.format(kind, reason, stacktrace),
crash_reason: crash_reason
)
end
end
end