-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
basic.ex
446 lines (350 loc) 路 12.1 KB
/
basic.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
defmodule Oban.Engines.Basic do
@moduledoc """
The default engine for use with Postgres databases.
## Usage
This is the default engine, no additional configuration is necessary:
Oban.start_link(repo: MyApp.Repo, queues: [default: 10])
"""
@behaviour Oban.Engine
import Ecto.Query
import DateTime, only: [utc_now: 0]
alias Ecto.Changeset
alias Oban.{Config, Engine, Job, Repo}
@impl Engine
def init(%Config{} = conf, opts) do
if Keyword.has_key?(opts, :limit) do
with :ok <- Enum.reduce_while(opts, :ok, &validate_meta_opt/2) do
meta =
opts
|> Map.new()
|> Map.put_new(:paused, false)
|> Map.put_new(:refresh_interval, :timer.seconds(30))
|> Map.put_new(:uuid, Ecto.UUID.generate())
|> Map.merge(%{name: conf.name, node: conf.node})
|> Map.merge(%{started_at: utc_now(), updated_at: utc_now()})
{:ok, meta}
end
else
{:error, ArgumentError.exception("missing required option :limit")}
end
end
@impl Engine
def put_meta(_conf, %{} = meta, key, value) do
Map.put(meta, key, value)
end
@impl Engine
def check_meta(_conf, %{} = meta, %{} = running) do
jids = for {_, {_, exec}} <- running, do: exec.job.id
Map.put(meta, :running, jids)
end
@impl Engine
def refresh(_conf, %{} = meta) do
%{meta | updated_at: utc_now()}
end
@impl Engine
def shutdown(_conf, %{} = meta) do
%{meta | paused: true}
end
@impl Engine
def insert_job(%Config{} = conf, %Changeset{} = changeset, opts) do
fun = fn -> insert_unique(conf, changeset, opts) end
with {:ok, result} <- Repo.transaction(conf, fun), do: result
end
@impl Engine
def insert_all_jobs(%Config{} = conf, changesets, opts) do
jobs = Enum.map(changesets, &Job.to_map/1)
opts = Keyword.merge(opts, on_conflict: :nothing, returning: true)
with {_count, jobs} <- Repo.insert_all(conf, Job, jobs, opts), do: jobs
end
@impl Engine
def fetch_jobs(_conf, %{paused: true} = meta, _running) do
{:ok, {meta, []}}
end
def fetch_jobs(_conf, %{limit: limit} = meta, running) when map_size(running) >= limit do
{:ok, {meta, []}}
end
def fetch_jobs(%Config{} = conf, %{} = meta, running) do
demand = meta.limit - map_size(running)
subset_query =
Job
|> select([:id])
|> where([j], j.state == "available")
|> where([j], j.queue == ^meta.queue)
|> order_by(asc: :priority, asc: :scheduled_at, asc: :id)
|> limit(^demand)
|> lock("FOR UPDATE SKIP LOCKED")
# The Postgres planner may choose to generate a plan that executes a nested loop over the
# LIMITing subquery, causing more UPDATEs than LIMIT. That could cause unexpected updates,
# including attempts > max_attempts in some cases. The solution is to use a CTE as an
# "optimization fence" that forces Postgres _not_ to optimize the query.
#
# The odd "subset" fragment is required to prevent Ecto from applying the prefix to the name
# of the CTE, e.g. "public"."subset".
query =
Job
|> with_cte("subset", as: ^subset_query, materialized: true)
|> join(:inner, [j], x in fragment(~s("subset")), on: true)
|> where([j, x], j.id == x.id)
|> where([j, _], j.attempt < j.max_attempts)
|> select([j, _], j)
updates = [
set: [state: "executing", attempted_at: utc_now(), attempted_by: [meta.node, meta.uuid]],
inc: [attempt: 1]
]
Repo.transaction(conf, fn ->
{_count, jobs} = Repo.update_all(conf, query, updates)
{meta, jobs}
end)
end
@impl Engine
def stage_jobs(%Config{} = conf, queryable, opts) do
limit = Keyword.fetch!(opts, :limit)
subquery =
queryable
|> select([:id, :state])
|> where([j], j.state in ~w(scheduled retryable))
|> where([j], not is_nil(j.queue))
|> where([j], j.scheduled_at <= ^DateTime.utc_now())
|> limit(^limit)
query =
Job
|> join(:inner, [j], x in subquery(subquery), on: j.id == x.id)
|> select([j, x], %{id: j.id, queue: j.queue, state: x.state})
{_count, staged} = Repo.update_all(conf, query, set: [state: "available"])
{:ok, staged}
end
@impl Engine
def prune_jobs(%Config{} = conf, queryable, opts) do
max_age = Keyword.fetch!(opts, :max_age)
limit = Keyword.fetch!(opts, :limit)
time = DateTime.add(DateTime.utc_now(), -max_age)
subquery =
queryable
|> select([:id, :queue, :state])
|> where([j], j.state in ~w(completed cancelled discarded))
|> where([j], not is_nil(j.queue))
|> where([j], j.scheduled_at < ^time)
|> limit(^limit)
query =
Job
|> join(:inner, [j], x in subquery(subquery), on: j.id == x.id)
|> select([_, x], map(x, [:id, :queue, :state]))
{_count, pruned} = Repo.delete_all(conf, query)
{:ok, pruned}
end
@impl Engine
def complete_job(%Config{} = conf, %Job{} = job) do
Repo.update_all(
conf,
where(Job, id: ^job.id),
set: [state: "completed", completed_at: utc_now()]
)
:ok
end
@impl Engine
def discard_job(%Config{} = conf, %Job{} = job) do
updates = [
set: [state: "discarded", discarded_at: utc_now()],
push: [errors: Job.format_attempt(job)]
]
Repo.update_all(conf, where(Job, id: ^job.id), updates)
:ok
end
@impl Engine
def error_job(%Config{} = conf, %Job{} = job, seconds) when is_integer(seconds) do
updates = [
set: [state: "retryable", scheduled_at: seconds_from_now(seconds)],
push: [errors: Job.format_attempt(job)]
]
Repo.update_all(conf, where(Job, id: ^job.id), updates)
:ok
end
@impl Engine
def snooze_job(%Config{} = conf, %Job{id: id}, seconds) when is_integer(seconds) do
updates = [
set: [state: "scheduled", scheduled_at: seconds_from_now(seconds)],
inc: [max_attempts: 1]
]
Repo.update_all(conf, where(Job, id: ^id), updates)
:ok
end
@impl Engine
def cancel_job(%Config{} = conf, %Job{} = job) do
query = where(Job, id: ^job.id)
query =
if is_map(job.unsaved_error) do
update(query, [j],
set: [state: "cancelled", cancelled_at: ^utc_now()],
push: [errors: ^Job.format_attempt(job)]
)
else
query
|> where([j], j.state not in ["cancelled", "completed", "discarded"])
|> update(set: [state: "cancelled", cancelled_at: ^utc_now()])
end
Repo.update_all(conf, query, [])
:ok
end
@impl Engine
def cancel_all_jobs(%Config{} = conf, queryable) do
subquery = where(queryable, [j], j.state not in ["cancelled", "completed", "discarded"])
query =
Job
|> join(:inner, [j], x in subquery(subquery), on: j.id == x.id)
|> select([_, x], map(x, [:id, :queue, :state]))
{_, jobs} = Repo.update_all(conf, query, set: [state: "cancelled", cancelled_at: utc_now()])
{:ok, jobs}
end
@impl Engine
def retry_job(%Config{} = conf, %Job{id: id}) do
retry_all_jobs(conf, where(Job, [j], j.id == ^id))
:ok
end
@impl Engine
def retry_all_jobs(%Config{} = conf, queryable) do
subquery = where(queryable, [j], j.state not in ["available", "executing"])
query =
Job
|> join(:inner, [j], x in subquery(subquery), on: j.id == x.id)
|> select([_, x], map(x, [:id, :queue, :state]))
|> update([j],
set: [
state: "available",
max_attempts: fragment("GREATEST(?, ? + 1)", j.max_attempts, j.attempt),
scheduled_at: ^utc_now(),
completed_at: nil,
cancelled_at: nil,
discarded_at: nil
]
)
{_, jobs} = Repo.update_all(conf, query, [])
{:ok, jobs}
end
# Validation
defp validate_meta_opt(opt, _acc) do
case validate_meta_opt(opt) do
{:error, error} -> {:halt, {:error, ArgumentError.exception(error)}}
_ -> {:cont, :ok}
end
end
defp validate_meta_opt({:limit, limit}) do
unless is_integer(limit) and limit > 0 do
{:error, "expected :limit to be an integer greater than 0, got: #{inspect(limit)}"}
end
end
defp validate_meta_opt({:paused, paused}) do
unless is_boolean(paused) do
{:error, "expected :paused to be a boolean, got: #{inspect(paused)}"}
end
end
defp validate_meta_opt({:queue, queue}) do
unless is_atom(queue) or (is_binary(queue) and byte_size(queue) > 0) do
{:error, "expected :queue to be an atom or binary, got: #{inspect(queue)}"}
end
end
defp validate_meta_opt({:refresh_interval, interval}) do
unless is_integer(interval) and interval > 0 do
{:error, "expected :refresh_interval to be positive integer, got: #{inspect(interval)}"}
end
end
defp validate_meta_opt({:validate, _}), do: :ok
defp validate_meta_opt(opt), do: {:error, "unexpected option #{inspect(opt)}"}
# Insertion
defp insert_unique(conf, changeset, opts) do
opts = Keyword.put(opts, :on_conflict, :nothing)
with {:ok, query, lock_key} <- unique_query(changeset),
:ok <- acquire_lock(conf, lock_key),
{:ok, job} <- fetch_job(conf, query, opts),
{:ok, job} <- resolve_conflict(conf, job, changeset, opts) do
{:ok, %Job{job | conflict?: true}}
else
{:error, :locked} ->
Changeset.apply_action(changeset, :insert)
nil ->
Repo.insert(conf, changeset, opts)
error ->
error
end
end
defp resolve_conflict(conf, job, changeset, opts) do
case Changeset.fetch_change(changeset, :replace) do
{:ok, replace} ->
keys = Keyword.get(replace, String.to_existing_atom(job.state), [])
Repo.update(
conf,
Changeset.change(job, Map.take(changeset.changes, keys)),
opts
)
:error ->
{:ok, job}
end
rescue
error in [Ecto.StaleEntryError] ->
{:error, error}
end
defp unique_query(%{changes: %{unique: %{} = unique}} = changeset) do
%{fields: fields, keys: keys, period: period, states: states, timestamp: timestamp} = unique
keys = Enum.map(keys, &to_string/1)
states = Enum.map(states, &to_string/1)
dynamic = Enum.reduce(fields, true, &unique_field({changeset, &1, keys}, &2))
lock_key = :erlang.phash2([keys, states, dynamic])
query =
Job
|> where([j], j.state in ^states)
|> since_period(period, timestamp)
|> where(^dynamic)
|> limit(1)
{:ok, query, lock_key}
end
defp unique_query(_changeset), do: nil
defp unique_field({changeset, field, keys}, acc) when field in [:args, :meta] do
value = unique_map_values(changeset, field, keys)
cond do
value == %{} ->
dynamic([j], fragment("? <@ ?", field(j, ^field), ^value) and ^acc)
keys == [] ->
dynamic(
[j],
fragment("? @> ?", field(j, ^field), ^value) and
fragment("? <@ ?", field(j, ^field), ^value) and ^acc
)
true ->
dynamic([j], fragment("? @> ?", field(j, ^field), ^value) and ^acc)
end
end
defp unique_field({changeset, field, _}, acc) do
value = Changeset.get_field(changeset, field)
dynamic([j], field(j, ^field) == ^value and ^acc)
end
defp unique_map_values(changeset, field, keys) do
case keys do
[] ->
Changeset.get_field(changeset, field)
[_ | _] ->
changeset
|> Changeset.get_field(field)
|> Map.new(fn {key, val} -> {to_string(key), val} end)
|> Map.take(keys)
end
end
defp since_period(query, :infinity, _timestamp), do: query
defp since_period(query, period, timestamp) do
where(query, [j], field(j, ^timestamp) >= ^seconds_from_now(-period))
end
defp acquire_lock(%{testing: :disabled} = conf, base_key) do
pref_key = :erlang.phash2(conf.prefix)
lock_key = pref_key + base_key
case Repo.query(conf, "SELECT pg_try_advisory_xact_lock($1)", [lock_key]) do
{:ok, %{rows: [[true]]}} -> :ok
_ -> {:error, :locked}
end
end
defp acquire_lock(_conf, _key), do: :ok
defp fetch_job(conf, query, opts) do
case Repo.one(conf, query, Keyword.put(opts, :prepare, :unnamed)) do
nil -> nil
job -> {:ok, job}
end
end
defp seconds_from_now(seconds), do: DateTime.add(utc_now(), seconds, :second)
end