-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.sql
253 lines (231 loc) · 6.89 KB
/
queries.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
-- name: GetJobByName :one
select * from tiny.job
where name = $1
and executor = $2
limit 1;
-- name: GetJobByID :one
select * from tiny.job
where id = $1
and executor = $2
limit 1;
-- name: LastUpdate :one
select max(updated_at)::timestamptz as last_update
from tiny.job
where executor = $1;
-- name: UpdateJobByName :one
update tiny.job
set expr = coalesce(nullif(sqlc.arg('expr'), ''), expr),
state = coalesce(nullif(sqlc.arg('state'), ''), state),
timeout = coalesce(nullif(sqlc.arg('timeout'), 0), timeout),
updated_at = now(),
-- `run_at` should always be consistent
run_at = tiny.next(
coalesce(last_run_at, created_at),
coalesce(nullif(sqlc.arg('expr'), ''), expr)
)
where name = $1
and executor = $2
returning *;
-- name: StopJob :one
update tiny.job
set status = 'PAUSED',
updated_at = now()
where id = $1
and executor = $2
-- Cannot stop a currently running task as it is outside of control for now
-- Possible to add a notification system to listen on those kind of events
and status not in ('FAILURE', 'SUCCESS', 'PENDING')
returning *;
-- name: RestartJob :one
update tiny.job
set status = 'READY',
updated_at = now()
where id = $1
and executor = $2
and status = 'PAUSED'
returning *;
-- name: UpdateJobByID :one
update tiny.job
set expr = coalesce(nullif(sqlc.arg('expr'), ''), expr),
updated_at = now(),
state = coalesce(nullif(sqlc.arg('state'), ''), state),
timeout = coalesce(nullif(sqlc.arg('timeout'), 0), timeout),
-- `run_at` should always be consistent
run_at = tiny.next(
coalesce(last_run_at, created_at),
coalesce(nullif(sqlc.arg('expr'), ''), expr)
)
where id = $1
and executor = $2
returning *;
-- name: UpdateStateByID :one
update tiny.job
set state = coalesce(nullif(sqlc.arg('state'), ''), state),
updated_at = now()
where id = $1
and executor = $2
returning *;
-- name: UpdateExprByID :one
update tiny.job
set expr = coalesce(nullif(sqlc.arg('expr'), ''), expr),
updated_at = now()
where id = $1
and executor = $2
returning *;
-- name: ValidateExprFormat :one
select (substr($1::text, 1, 6) in ('@every', '@after') and (substr($1::text, 7)::interval) is not null
or $1::text ~ '^@(annually|yearly|monthly|weekly|daily|hourly|minutely)$'
or substr($1::text, 1, 3) = '@at' and (substr($1::text, 4)::timestamptz) is not null
or tiny.crontab($1::text))::bool as valid;
-- name: DeleteJobByName :one
delete from tiny.job
where name = $1
and executor = $2
returning *;
-- name: DeleteJobByID :one
delete from tiny.job
where id = $1
and executor = $2
returning *;
-- name: CreateJob :one
insert into tiny.job(expr, name, state, status, executor, run_at, timeout, start_at, meta, owner, retries, deduplication_key)
values (
sqlc.arg('expr'),
coalesce(nullif(sqlc.arg('name'), ''), substr(md5(random()::text), 0, 25)),
sqlc.arg('state'),
'READY',
sqlc.arg('executor'),
tiny.next(greatest(sqlc.arg('start_at'), now()), sqlc.arg('expr')),
coalesce(nullif(sqlc.arg('timeout'), 0), 120),
sqlc.arg('start_at'),
sqlc.arg('meta'),
coalesce(nullif(sqlc.arg('owner'), ''), 'default'),
coalesce(nullif(sqlc.arg('retries'), 0), 5),
sqlc.arg('deduplication_key')
)
-- on conflict on constraint job_name_owner_key
-- do ...
returning *;
-- name: BatchCreateJobs :batchexec
insert into tiny.job(expr, name, state, status, executor, run_at, timeout, start_at, meta, owner, retries, deduplication_key)
values (
sqlc.arg('expr'),
coalesce(nullif(sqlc.arg('name'), ''), substr(md5(random()::text), 0, 25)),
sqlc.arg('state'),
'READY',
sqlc.arg('executor'),
tiny.next(greatest(sqlc.arg('start_at'), now()), sqlc.arg('expr')),
coalesce(nullif(sqlc.arg('timeout'), 0), 120),
sqlc.arg('start_at'),
sqlc.arg('meta'),
coalesce(nullif(sqlc.arg('owner'), ''), 'default'),
coalesce(nullif(sqlc.arg('retries'), 0), 5),
sqlc.arg('deduplication_key')
);
-- name: SearchJobs :many
select * from tiny.job
where (name like concat(sqlc.arg('query')::text, '%')
or name like concat('%', sqlc.arg('query')::text))
and executor = $3
offset $1
limit $2;
-- name: SearchJobsByMeta :many
with jobs as (
select * from tiny.job
where meta::jsonb @> (sqlc.arg('query')::text)::jsonb
and status::text = any(string_to_array(sqlc.arg('statuses')::text, ','))
and created_at > sqlc.arg('from')::timestamptz
and created_at < sqlc.arg('to')::timestamptz
and (name like concat(sqlc.arg('name')::text, '%')
or name like concat('%', sqlc.arg('name')::text))
-- Filter recurring tasks
and tiny.is_one_shot(expr) = sqlc.arg('is_one_shot')::boolean
and executor = sqlc.arg('executor')::text
),
total as (
select count(*) as total_count from jobs
)
select jobs.*, total_count from jobs, total
order by last_run_at desc
limit sqlc.arg('limit')::int
offset sqlc.arg('offset')::int;
-- name: BatchUpdateJobs :batchexec
update tiny.job
set last_run_at = now(),
state = coalesce(nullif(sqlc.arg('state')::text, ''), state),
expr = coalesce(nullif(sqlc.arg('expr')::text, ''), expr),
status = sqlc.arg('status'),
updated_at = now(),
execution_amount = execution_amount + 1,
retries = sqlc.arg('retries'),
run_at = tiny.next(
now(),
coalesce(nullif(sqlc.arg('expr')::text, ''), expr)
)
where id = sqlc.arg('id')
and executor = sqlc.arg('executor');
-- name: BatchUpdateFailedJobs :batchexec
update tiny.job
set last_run_at = now(),
state = coalesce(nullif(sqlc.arg('state')::text, ''), state),
updated_at = now(),
expr = coalesce(nullif(sqlc.arg('expr')::text, ''), expr),
status = case
when tiny.is_one_shot(expr) and retries - 1 <= 0 then 'FAILURE'::tiny.status
else 'READY'::tiny.status
end,
retries = retries - 1,
execution_amount = execution_amount + 1,
run_at = case
when tiny.is_one_shot(expr) then now() + concat(power(2, execution_amount)::text, 's')::interval
else tiny.next(
now(),
coalesce(nullif(sqlc.arg('expr')::text, ''), expr)
)
end
where id = sqlc.arg('id')
and executor = sqlc.arg('executor');
-- name: FetchDueJobs :many
with due_jobs as (
select id
from tiny.job j
where j.run_at < now()
and j.status = 'READY'
and j.executor = sqlc.arg('executor')
order by j.created_at
limit $1
for update skip locked
)
update tiny.job as updated_jobs
set status = 'PENDING',
updated_at = now(),
last_run_at = now()
from due_jobs
where due_jobs.id = updated_jobs.id
returning updated_jobs.*;
-- name: ResetTimeoutJobs :many
update tiny.job
set status = 'READY',
updated_at = now()
where timeout is not null
and timeout > 0
and now() - last_run_at > make_interval(secs => timeout)
and executor = $1
and status = 'PENDING'
returning id;
-- name: CronNextRun :one
select run_at::timestamptz
from tiny.cron_next_run(
sqlc.arg('from')::timestamptz,
sqlc.arg('expr')::text
) as run_at;
-- name: Next :one
select run_at::timestamptz
from tiny.next(
sqlc.arg('from')::timestamptz,
sqlc.arg('expr')::text
) as run_at;
-- name: CountJobsInStatus :one
select count(*) from tiny.job
where executor = $1
and status = $2;