forked from wavezync/durable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.ex
More file actions
257 lines (213 loc) · 6.61 KB
/
postgres.ex
File metadata and controls
257 lines (213 loc) · 6.61 KB
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
defmodule Durable.Queue.Adapters.Postgres do
@moduledoc """
PostgreSQL-based queue adapter using the workflow_executions table.
Uses `FOR UPDATE SKIP LOCKED` for atomic job claiming without blocking.
This ensures that multiple pollers can safely claim jobs without
processing the same job twice.
"""
@behaviour Durable.Queue.Adapter
alias Durable.Config
alias Durable.Storage.Schemas.WorkflowExecution
alias Ecto.Adapters.SQL
import Ecto.Query
@impl true
def fetch_jobs(%Config{} = config, queue, limit, node_id)
when is_binary(queue) and limit > 0 do
repo = config.repo
prefix = config.prefix
# Use raw SQL for FOR UPDATE SKIP LOCKED which Ecto doesn't support directly
sql = """
WITH claimable AS (
SELECT id FROM #{prefix}.workflow_executions
WHERE status = 'pending'
AND queue = $1
AND (scheduled_at IS NULL OR scheduled_at <= NOW())
AND (locked_by IS NULL OR locked_at < NOW() - INTERVAL '#{config.stale_lock_timeout} seconds')
ORDER BY priority DESC, scheduled_at ASC NULLS FIRST, inserted_at ASC
LIMIT $2
FOR UPDATE SKIP LOCKED
)
UPDATE #{prefix}.workflow_executions
SET locked_by = $3, locked_at = NOW(), status = 'running'
WHERE id IN (SELECT id FROM claimable)
RETURNING id, workflow_module, workflow_name, queue, priority, input, context, scheduled_at, current_step;
"""
case SQL.query(repo, sql, [queue, limit, node_id], log: config.log_level) do
{:ok, %{rows: rows, columns: columns}} ->
rows
|> Enum.map(&parse_row(&1, columns))
# Re-sort in Elixir since UPDATE doesn't preserve order
|> Enum.sort_by(fn job -> {-job.priority, job.scheduled_at} end)
{:error, _reason} ->
[]
end
end
@impl true
def ack(%Config{} = config, job_id) when is_binary(job_id) do
repo = config.repo
log_opts = [log: config.log_level]
case repo.get(WorkflowExecution, job_id, log_opts) do
nil ->
{:error, :not_found}
execution ->
execution
|> WorkflowExecution.unlock_changeset()
|> repo.update(log_opts)
:ok
end
end
@impl true
def nack(%Config{} = config, job_id, reason) when is_binary(job_id) do
repo = config.repo
log_opts = [log: config.log_level]
case repo.get(WorkflowExecution, job_id, log_opts) do
nil ->
{:error, :not_found}
execution ->
error = normalize_error(reason)
execution
|> Ecto.Changeset.change(
status: :failed,
error: error,
completed_at: DateTime.utc_now(),
locked_by: nil,
locked_at: nil
)
|> repo.update(log_opts)
:ok
end
end
@impl true
def reschedule(%Config{} = config, job_id, run_at) when is_binary(job_id) do
repo = config.repo
log_opts = [log: config.log_level]
case repo.get(WorkflowExecution, job_id, log_opts) do
nil ->
{:error, :not_found}
execution ->
execution
|> Ecto.Changeset.change(
status: :pending,
scheduled_at: run_at,
locked_by: nil,
locked_at: nil
)
|> repo.update(log_opts)
:ok
end
end
@impl true
def recover_stale_locks(%Config{} = config, timeout_seconds) when timeout_seconds > 0 do
repo = config.repo
cutoff = DateTime.add(DateTime.utc_now(), -timeout_seconds, :second)
log_opts = [log: config.log_level]
{count, _} =
from(w in WorkflowExecution,
where: w.status == :running,
where: not is_nil(w.locked_by),
where: w.locked_at < ^cutoff
)
|> repo.update_all(
[
set: [
status: :pending,
locked_by: nil,
locked_at: nil
]
],
log_opts
)
{:ok, count}
rescue
e -> {:error, Exception.message(e)}
end
@impl true
def heartbeat(%Config{} = config, job_id) when is_binary(job_id) do
repo = config.repo
now = DateTime.utc_now()
log_opts = [log: config.log_level]
{count, _} =
from(w in WorkflowExecution,
where: w.id == ^job_id,
where: w.status == :running
)
|> repo.update_all([set: [locked_at: now]], log_opts)
if count == 1 do
:ok
else
{:error, :not_found}
end
end
@impl true
def get_stats(%Config{} = config, queue) when is_binary(queue) do
repo = config.repo
log_opts = [log: config.log_level]
base_query = from(w in WorkflowExecution, where: w.queue == ^queue)
pending =
from(w in base_query, where: w.status == :pending)
|> repo.aggregate(:count, log_opts)
running =
from(w in base_query, where: w.status == :running)
|> repo.aggregate(:count, log_opts)
completed =
from(w in base_query, where: w.status == :completed)
|> repo.aggregate(:count, log_opts)
failed =
from(w in base_query, where: w.status == :failed)
|> repo.aggregate(:count, log_opts)
waiting =
from(w in base_query, where: w.status == :waiting)
|> repo.aggregate(:count, log_opts)
scheduled =
from(w in base_query,
where: w.status == :pending,
where: not is_nil(w.scheduled_at),
where: w.scheduled_at > ^DateTime.utc_now()
)
|> repo.aggregate(:count, log_opts)
%{
queue: queue,
pending: pending,
running: running,
completed: completed,
failed: failed,
waiting: waiting,
scheduled: scheduled,
total: pending + running + completed + failed + waiting
}
end
# Private functions
defp parse_row(row, columns) do
columns
|> Enum.zip(row)
|> Map.new(fn {col, val} -> {String.to_atom(col), val} end)
|> decode_job()
end
defp decode_job(job) do
%{
id: decode_uuid(job.id),
workflow_module: job.workflow_module,
workflow_name: job.workflow_name,
queue: job.queue,
priority: job.priority,
input: decode_json(job.input),
context: decode_json(job.context),
scheduled_at: job.scheduled_at,
current_step: job.current_step
}
end
defp decode_uuid(<<_::128>> = binary) do
Ecto.UUID.cast!(binary)
end
defp decode_uuid(uuid) when is_binary(uuid), do: uuid
defp decode_json(nil), do: %{}
defp decode_json(value) when is_map(value), do: value
defp decode_json(value) when is_binary(value), do: Jason.decode!(value)
defp normalize_error(reason) when is_map(reason), do: reason
defp normalize_error(reason) when is_binary(reason) do
%{type: "error", message: reason}
end
defp normalize_error(reason) do
%{type: "error", message: inspect(reason)}
end
end