-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathschema.sql
398 lines (398 loc) · 14.4 KB
/
schema.sql
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
SELECT pg_catalog.set_config('search_path', '', false);
CREATE SCHEMA graphile_worker;
CREATE TYPE graphile_worker.job_spec AS (
identifier text,
payload json,
queue_name text,
run_at timestamp with time zone,
max_attempts smallint,
job_key text,
priority smallint,
flags text[]
);
CREATE TABLE graphile_worker._private_jobs (
id bigint NOT NULL,
job_queue_id integer,
task_id integer NOT NULL,
payload json DEFAULT '{}'::json NOT NULL,
priority smallint DEFAULT 0 NOT NULL,
run_at timestamp with time zone DEFAULT now() NOT NULL,
attempts smallint DEFAULT 0 NOT NULL,
max_attempts smallint DEFAULT 25 NOT NULL,
last_error text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
key text,
locked_at timestamp with time zone,
locked_by text,
revision integer DEFAULT 0 NOT NULL,
flags jsonb,
is_available boolean GENERATED ALWAYS AS (((locked_at IS NULL) AND (attempts < max_attempts))) STORED NOT NULL,
CONSTRAINT jobs_key_check CHECK (((length(key) > 0) AND (length(key) <= 512))),
CONSTRAINT jobs_max_attempts_check CHECK ((max_attempts >= 1))
);
CREATE FUNCTION graphile_worker.add_job(identifier text, payload json DEFAULT NULL::json, queue_name text DEFAULT NULL::text, run_at timestamp with time zone DEFAULT NULL::timestamp with time zone, max_attempts integer DEFAULT NULL::integer, job_key text DEFAULT NULL::text, priority integer DEFAULT NULL::integer, flags text[] DEFAULT NULL::text[], job_key_mode text DEFAULT 'replace'::text) RETURNS graphile_worker._private_jobs
LANGUAGE plpgsql
AS $$
declare
v_job "graphile_worker"._private_jobs;
begin
if (job_key is null or job_key_mode is null or job_key_mode in ('replace', 'preserve_run_at')) then
select * into v_job
from "graphile_worker".add_jobs(
ARRAY[(
identifier,
payload,
queue_name,
run_at,
max_attempts::smallint,
job_key,
priority::smallint,
flags
)::"graphile_worker".job_spec],
(job_key_mode = 'preserve_run_at')
)
limit 1;
return v_job;
elsif job_key_mode = 'unsafe_dedupe' then
-- Ensure all the tasks exist
insert into "graphile_worker"._private_tasks as tasks (identifier)
values (add_job.identifier)
on conflict do nothing;
-- Ensure all the queues exist
if add_job.queue_name is not null then
insert into "graphile_worker"._private_job_queues as job_queues (queue_name)
values (add_job.queue_name)
on conflict do nothing;
end if;
-- Insert job, but if one already exists then do nothing, even if the
-- existing job has already started (and thus represents an out-of-date
-- world state). This is dangerous because it means that whatever state
-- change triggered this add_job may not be acted upon (since it happened
-- after the existing job started executing, but no further job is being
-- scheduled), but it is useful in very rare circumstances for
-- de-duplication. If in doubt, DO NOT USE THIS.
insert into "graphile_worker"._private_jobs as jobs (
job_queue_id,
task_id,
payload,
run_at,
max_attempts,
key,
priority,
flags
)
select
job_queues.id,
tasks.id,
coalesce(add_job.payload, '{}'::json),
coalesce(add_job.run_at, now()),
coalesce(add_job.max_attempts::smallint, 25::smallint),
add_job.job_key,
coalesce(add_job.priority::smallint, 0::smallint),
(
select jsonb_object_agg(flag, true)
from unnest(add_job.flags) as item(flag)
)
from "graphile_worker"._private_tasks as tasks
left join "graphile_worker"._private_job_queues as job_queues
on job_queues.queue_name = add_job.queue_name
where tasks.identifier = add_job.identifier
on conflict (key)
-- Bump the updated_at so that there's something to return
do update set
revision = jobs.revision + 1,
updated_at = now()
returning *
into v_job;
if v_job.revision = 0 then
perform pg_notify('jobs:insert', '{"r":' || random()::text || ',"count":1}');
end if;
return v_job;
else
raise exception 'Invalid job_key_mode value, expected ''replace'', ''preserve_run_at'' or ''unsafe_dedupe''.' using errcode = 'GWBKM';
end if;
end;
$$;
CREATE FUNCTION graphile_worker.add_jobs(specs graphile_worker.job_spec[], job_key_preserve_run_at boolean DEFAULT false) RETURNS SETOF graphile_worker._private_jobs
LANGUAGE plpgsql
AS $$
begin
-- Ensure all the tasks exist
insert into "graphile_worker"._private_tasks as tasks (identifier)
select distinct spec.identifier
from unnest(specs) spec
on conflict do nothing;
-- Ensure all the queues exist
insert into "graphile_worker"._private_job_queues as job_queues (queue_name)
select distinct spec.queue_name
from unnest(specs) spec
where spec.queue_name is not null
on conflict do nothing;
-- Ensure any locked jobs have their key cleared - in the case of locked
-- existing job create a new job instead as it must have already started
-- executing (i.e. it's world state is out of date, and the fact add_job
-- has been called again implies there's new information that needs to be
-- acted upon).
update "graphile_worker"._private_jobs as jobs
set
key = null,
attempts = jobs.max_attempts,
updated_at = now()
from unnest(specs) spec
where spec.job_key is not null
and jobs.key = spec.job_key
and is_available is not true;
-- WARNING: this count is not 100% accurate; 'on conflict' clause will cause it to be an overestimate
perform pg_notify('jobs:insert', '{"r":' || random()::text || ',"count":' || array_length(specs, 1)::text || '}');
-- TODO: is there a risk that a conflict could occur depending on the
-- isolation level?
return query insert into "graphile_worker"._private_jobs as jobs (
job_queue_id,
task_id,
payload,
run_at,
max_attempts,
key,
priority,
flags
)
select
job_queues.id,
tasks.id,
coalesce(spec.payload, '{}'::json),
coalesce(spec.run_at, now()),
coalesce(spec.max_attempts, 25),
spec.job_key,
coalesce(spec.priority, 0),
(
select jsonb_object_agg(flag, true)
from unnest(spec.flags) as item(flag)
)
from unnest(specs) spec
inner join "graphile_worker"._private_tasks as tasks
on tasks.identifier = spec.identifier
left join "graphile_worker"._private_job_queues as job_queues
on job_queues.queue_name = spec.queue_name
on conflict (key) do update set
job_queue_id = excluded.job_queue_id,
task_id = excluded.task_id,
payload =
case
when json_typeof(jobs.payload) = 'array' and json_typeof(excluded.payload) = 'array' then
(jobs.payload::jsonb || excluded.payload::jsonb)::json
else
excluded.payload
end,
max_attempts = excluded.max_attempts,
run_at = (case
when job_key_preserve_run_at is true and jobs.attempts = 0 then jobs.run_at
else excluded.run_at
end),
priority = excluded.priority,
revision = jobs.revision + 1,
flags = excluded.flags,
-- always reset error/retry state
attempts = 0,
last_error = null,
updated_at = now()
where jobs.locked_at is null
returning *;
end;
$$;
CREATE FUNCTION graphile_worker.complete_jobs(job_ids bigint[]) RETURNS SETOF graphile_worker._private_jobs
LANGUAGE sql
AS $$
delete from "graphile_worker"._private_jobs as jobs
where id = any(job_ids)
and (
locked_at is null
or
locked_at < now() - interval '4 hours'
)
returning *;
$$;
CREATE FUNCTION graphile_worker.force_unlock_workers(worker_ids text[]) RETURNS void
LANGUAGE sql
AS $$
update "graphile_worker"._private_jobs as jobs
set locked_at = null, locked_by = null
where locked_by = any(worker_ids);
update "graphile_worker"._private_job_queues as job_queues
set locked_at = null, locked_by = null
where locked_by = any(worker_ids);
$$;
CREATE FUNCTION graphile_worker.permanently_fail_jobs(job_ids bigint[], error_message text DEFAULT NULL::text) RETURNS SETOF graphile_worker._private_jobs
LANGUAGE sql
AS $$
update "graphile_worker"._private_jobs as jobs
set
last_error = coalesce(error_message, 'Manually marked as failed'),
attempts = max_attempts,
updated_at = now()
where id = any(job_ids)
and (
locked_at is null
or
locked_at < NOW() - interval '4 hours'
)
returning *;
$$;
CREATE FUNCTION graphile_worker.remove_job(job_key text) RETURNS graphile_worker._private_jobs
LANGUAGE plpgsql STRICT
AS $$
declare
v_job "graphile_worker"._private_jobs;
begin
-- Delete job if not locked
delete from "graphile_worker"._private_jobs as jobs
where key = job_key
and (
locked_at is null
or
locked_at < NOW() - interval '4 hours'
)
returning * into v_job;
if not (v_job is null) then
perform pg_notify('jobs:insert', '{"r":' || random()::text || ',"count":-1}');
return v_job;
end if;
-- Otherwise prevent job from retrying, and clear the key
update "graphile_worker"._private_jobs as jobs
set
key = null,
attempts = jobs.max_attempts,
updated_at = now()
where key = job_key
returning * into v_job;
return v_job;
end;
$$;
CREATE FUNCTION graphile_worker.reschedule_jobs(job_ids bigint[], run_at timestamp with time zone DEFAULT NULL::timestamp with time zone, priority integer DEFAULT NULL::integer, attempts integer DEFAULT NULL::integer, max_attempts integer DEFAULT NULL::integer) RETURNS SETOF graphile_worker._private_jobs
LANGUAGE sql
AS $$
update "graphile_worker"._private_jobs as jobs
set
run_at = coalesce(reschedule_jobs.run_at, jobs.run_at),
priority = coalesce(reschedule_jobs.priority::smallint, jobs.priority),
attempts = coalesce(reschedule_jobs.attempts::smallint, jobs.attempts),
max_attempts = coalesce(reschedule_jobs.max_attempts::smallint, jobs.max_attempts),
updated_at = now()
where id = any(job_ids)
and (
locked_at is null
or
locked_at < NOW() - interval '4 hours'
)
returning *;
$$;
CREATE TABLE graphile_worker._private_job_queues (
id integer NOT NULL,
queue_name text NOT NULL,
locked_at timestamp with time zone,
locked_by text,
is_available boolean GENERATED ALWAYS AS ((locked_at IS NULL)) STORED NOT NULL,
CONSTRAINT job_queues_queue_name_check CHECK ((length(queue_name) <= 128))
);
CREATE TABLE graphile_worker._private_known_crontabs (
identifier text NOT NULL,
known_since timestamp with time zone NOT NULL,
last_execution timestamp with time zone
);
CREATE TABLE graphile_worker._private_tasks (
id integer NOT NULL,
identifier text NOT NULL,
CONSTRAINT tasks_identifier_check CHECK ((length(identifier) <= 128))
);
ALTER TABLE graphile_worker._private_job_queues ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME graphile_worker.job_queues_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE VIEW graphile_worker.jobs AS
SELECT jobs.id,
job_queues.queue_name,
tasks.identifier AS task_identifier,
jobs.priority,
jobs.run_at,
jobs.attempts,
jobs.max_attempts,
jobs.last_error,
jobs.created_at,
jobs.updated_at,
jobs.key,
jobs.locked_at,
jobs.locked_by,
jobs.revision,
jobs.flags
FROM ((graphile_worker._private_jobs jobs
JOIN graphile_worker._private_tasks tasks ON ((tasks.id = jobs.task_id)))
LEFT JOIN graphile_worker._private_job_queues job_queues ON ((job_queues.id = jobs.job_queue_id)));
ALTER TABLE graphile_worker._private_jobs ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME graphile_worker.jobs_id_seq1
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE graphile_worker.migrations (
id integer NOT NULL,
ts timestamp with time zone DEFAULT now() NOT NULL,
breaking boolean DEFAULT false NOT NULL
);
ALTER TABLE graphile_worker._private_tasks ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME graphile_worker.tasks_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
ALTER TABLE ONLY graphile_worker._private_job_queues
ADD CONSTRAINT job_queues_pkey1 PRIMARY KEY (id);
ALTER TABLE ONLY graphile_worker._private_job_queues
ADD CONSTRAINT job_queues_queue_name_key UNIQUE (queue_name);
ALTER TABLE ONLY graphile_worker._private_jobs
ADD CONSTRAINT jobs_key_key1 UNIQUE (key);
ALTER TABLE ONLY graphile_worker._private_jobs
ADD CONSTRAINT jobs_pkey1 PRIMARY KEY (id);
ALTER TABLE ONLY graphile_worker._private_known_crontabs
ADD CONSTRAINT known_crontabs_pkey PRIMARY KEY (identifier);
ALTER TABLE ONLY graphile_worker.migrations
ADD CONSTRAINT migrations_pkey PRIMARY KEY (id);
ALTER TABLE ONLY graphile_worker._private_tasks
ADD CONSTRAINT tasks_identifier_key UNIQUE (identifier);
ALTER TABLE ONLY graphile_worker._private_tasks
ADD CONSTRAINT tasks_pkey PRIMARY KEY (id);
CREATE INDEX jobs_main_index ON graphile_worker._private_jobs USING btree (priority, run_at) INCLUDE (id, task_id, job_queue_id) WHERE (is_available = true);
CREATE INDEX jobs_no_queue_index ON graphile_worker._private_jobs USING btree (priority, run_at) INCLUDE (id, task_id) WHERE ((is_available = true) AND (job_queue_id IS NULL));
ALTER TABLE graphile_worker._private_job_queues ENABLE ROW LEVEL SECURITY;
ALTER TABLE graphile_worker._private_jobs ENABLE ROW LEVEL SECURITY;
ALTER TABLE graphile_worker._private_known_crontabs ENABLE ROW LEVEL SECURITY;
ALTER TABLE graphile_worker._private_tasks ENABLE ROW LEVEL SECURITY;
SELECT pg_catalog.set_config('search_path', '', false);
COPY graphile_worker.migrations (id, ts, breaking) FROM stdin;
1 1970-01-01 00:00:00.000000+00 t
2 1970-01-01 00:00:00.000000+00 f
3 1970-01-01 00:00:00.000000+00 t
4 1970-01-01 00:00:00.000000+00 f
5 1970-01-01 00:00:00.000000+00 f
6 1970-01-01 00:00:00.000000+00 f
7 1970-01-01 00:00:00.000000+00 f
8 1970-01-01 00:00:00.000000+00 f
9 1970-01-01 00:00:00.000000+00 f
10 1970-01-01 00:00:00.000000+00 f
11 1970-01-01 00:00:00.000000+00 t
12 1970-01-01 00:00:00.000000+00 f
13 1970-01-01 00:00:00.000000+00 t
14 1970-01-01 00:00:00.000000+00 t
15 1970-01-01 00:00:00.000000+00 f
16 1970-01-01 00:00:00.000000+00 t
17 1970-01-01 00:00:00.000000+00 f
18 1970-01-01 00:00:00.000000+00 f
19 1970-01-01 00:00:00.000000+00 t
\.