-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathexecution_broadcaster.ex
332 lines (274 loc) · 8.86 KB
/
execution_broadcaster.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
defmodule Quantum.ExecutionBroadcaster do
@moduledoc """
Receives Added / Removed Jobs, Broadcasts Executions of Jobs
"""
use GenStage
require Logger
alias Quantum.{Job, Util, DateLibrary}
alias Quantum.DateLibrary.{InvalidDateTimeForTimezoneError, InvalidTimezoneError}
alias Crontab.Scheduler, as: CrontabScheduler
alias Crontab.CronExpression
alias Quantum.Storage.Adapter
alias Quantum.Scheduler
defmodule JobInPastError do
defexception message:
"The job was scheduled in the past. This must not happen to prevent infinite loops!"
end
@doc """
Start Stage
### Arguments
* `name` - The name of the stage
* `job_broadcaster` - The name of the stage to listen to
"""
@spec start_link(GenServer.server(), GenServer.server(), Adapter, Scheduler, boolean()) ::
GenServer.on_start()
def start_link(name, job_broadcaster, storage, scheduler, debug_logging) do
__MODULE__
|> GenStage.start_link({job_broadcaster, storage, scheduler, debug_logging}, name: name)
|> Util.start_or_link()
end
@doc false
@spec child_spec({GenServer.server(), GenServer.server(), Adapter, Scheduler, boolean()}) ::
Supervisor.child_spec()
def child_spec({name, job_broadcaster, storage, scheduler, debug_logging}) do
%{
super([])
| start:
{__MODULE__, :start_link, [name, job_broadcaster, storage, scheduler, debug_logging]}
}
end
@doc false
def init({job_broadcaster, storage, scheduler, debug_logging}) do
last_execution_date =
case storage.last_execution_date(scheduler) do
%NaiveDateTime{} = date ->
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Using last known execution time #{
NaiveDateTime.to_iso8601(date)
}"
end)
date
:unknown ->
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Unknown last execution time, using now"
end)
NaiveDateTime.utc_now()
end
state = %{
jobs: [],
time: last_execution_date,
timer: nil,
storage: storage,
scheduler: scheduler,
debug_logging: debug_logging
}
{:producer_consumer, state, subscribe_to: [job_broadcaster]}
end
def handle_events(events, _, %{debug_logging: debug_logging} = state) do
reboot_add_events =
events
|> Enum.filter(&add_reboot_event?/1)
|> Enum.map(fn {:add, job} -> {:execute, job} end)
for {_, %{name: job_name}} <- reboot_add_events do
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Scheduling job for single reboot execution: #{
inspect(job_name)
}"
end)
end
state =
events
|> Enum.reject(&add_reboot_event?/1)
|> Enum.reduce(state, &handle_event/2)
|> sort_state
|> reset_timer
{:noreply, reboot_add_events, state}
end
def handle_info(
:execute,
%{
jobs: [{time_to_execute, jobs_to_execute} | tail],
storage: storage,
scheduler: scheduler,
debug_logging: debug_logging
} = state
) do
:ok = storage.update_last_execution_date(scheduler, time_to_execute)
state =
state
|> Map.put(:timer, nil)
|> Map.put(:jobs, tail)
|> Map.put(:time, NaiveDateTime.add(time_to_execute, 1, :second))
state =
jobs_to_execute
|> (fn jobs ->
for %{name: job_name} <- jobs do
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Scheduling job for execution #{
inspect(job_name)
}"
end)
end
jobs
end).()
|> Enum.reduce(state, &add_job_to_state/2)
|> sort_state
|> reset_timer
{:noreply, Enum.map(jobs_to_execute, fn job -> {:execute, job} end), state}
end
defp handle_event({:add, %{name: job_name} = job}, %{debug_logging: debug_logging} = state) do
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Adding job #{inspect(job_name)}"
end)
add_job_to_state(job, state)
end
defp handle_event({:remove, name}, %{jobs: jobs, debug_logging: debug_logging} = state) do
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Removing job #{inspect(name)}"
end)
jobs =
jobs
|> Enum.map(fn {date, job_list} ->
{date, Enum.reject(job_list, &match?(%{name: ^name}, &1))}
end)
|> Enum.reject(fn
{_, []} -> true
{_, _} -> false
end)
%{state | jobs: jobs}
|> sort_state
|> reset_timer
end
defp add_job_to_state(
%Job{schedule: schedule, timezone: timezone, name: name} = job,
%{time: time} = state
) do
job
|> get_next_execution_time(time)
|> case do
{:ok, date} ->
add_to_state(state, date, job)
{:error, _} ->
Logger.warn(fn ->
"""
Invalid Schedule #{inspect(schedule)} provided for job #{inspect(name)}.
No matching dates found. The job was removed.
"""
end)
state
end
rescue
e in InvalidTimezoneError ->
Logger.error(
"Invalid Timezone #{inspect(timezone)} provided for job #{inspect(name)}.",
job: job,
error: e
)
end
defp get_next_execution_time(
%Job{schedule: schedule, timezone: timezone, name: name} = job,
time
) do
schedule
|> CrontabScheduler.get_next_run_date(DateLibrary.to_tz!(time, timezone))
|> case do
{:ok, date} ->
{:ok, DateLibrary.to_utc!(date, timezone)}
{:error, _} = error ->
error
end
rescue
_ in InvalidDateTimeForTimezoneError ->
next_time = NaiveDateTime.add(time, 60, :second)
Logger.warn(fn ->
"""
Next execution time for job #{inspect(name)} is not a valid time.
Retrying with #{inspect(next_time)}
"""
end)
get_next_execution_time(job, next_time)
end
defp sort_state(%{jobs: jobs} = state) do
%{state | jobs: Enum.sort_by(jobs, fn {date, _} -> NaiveDateTime.to_erl(date) end)}
end
defp add_to_state(%{jobs: jobs, time: time} = state, date, job) do
unless NaiveDateTime.compare(time, date) in [:lt, :eq] do
raise JobInPastError
end
%{state | jobs: add_job_at_date(jobs, date, job)}
end
defp add_job_at_date(jobs, date, job) do
case find_date_and_put_job(jobs, date, job) do
{:found, list} -> list
{:not_found, list} -> [{date, [job]} | list]
end
end
defp find_date_and_put_job([{date, jobs} | rest], date, job) do
{:found, [{date, [job | jobs]} | rest]}
end
defp find_date_and_put_job([], _, _) do
{:not_found, []}
end
defp find_date_and_put_job([head | rest], date, job) do
{state, new_rest} = find_date_and_put_job(rest, date, job)
{state, [head | new_rest]}
end
defp reset_timer(%{timer: nil, jobs: []} = state) do
state
end
defp reset_timer(%{timer: {timer, _}, jobs: []} = state) do
Process.cancel_timer(timer)
Map.put(state, :timer, nil)
end
defp reset_timer(%{timer: nil, jobs: jobs, debug_logging: debug_logging} = state) do
run_date = next_run_date(jobs)
timer =
case NaiveDateTime.compare(run_date, NaiveDateTime.utc_now()) do
:gt ->
monotonic_time =
run_date
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.to_unix(:millisecond)
|> Kernel.-(System.time_offset(:millisecond))
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Continuing Execution Broadcasting at #{
inspect(monotonic_time)
} (#{NaiveDateTime.to_iso8601(run_date)})"
end)
Process.send_after(self(), :execute, monotonic_time, abs: true)
_ ->
debug_logging &&
Logger.debug(fn ->
"[#{inspect(Node.self())}][#{__MODULE__}] Continuing Execution Broadcasting ASAP (#{
NaiveDateTime.to_iso8601(run_date)
})"
end)
send(self(), :execute)
nil
end
Map.put(state, :timer, {timer, run_date})
end
defp reset_timer(%{timer: {timer, old_date}, jobs: jobs} = state) do
run_date = next_run_date(jobs)
case NaiveDateTime.compare(run_date, old_date) do
:eq ->
state
_ ->
Process.cancel_timer(timer)
reset_timer(Map.put(state, :timer, nil))
end
end
defp next_run_date(jobs) do
[{run_date, _} | _rest] = jobs
run_date
end
defp add_reboot_event?({:add, %Job{schedule: %CronExpression{reboot: true}}}), do: true
defp add_reboot_event?(_), do: false
end