Source file repo.ml
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
module Map = Map.Make (String)
module type Sig = sig
val lifecycles : Sihl.Container.lifecycle list
val register_migration : unit -> unit
val register_cleaner : unit -> unit
val enqueue : job_instance:Job_instance.t -> unit Lwt.t
val find_workable : unit -> Job_instance.t list Lwt.t
val update : job_instance:Job_instance.t -> unit Lwt.t
end
module InMemory : Sig = struct
let lifecycles = []
let state = ref Map.empty
let ordered_ids = ref []
let register_cleaner () =
let cleaner _ =
state := Map.empty;
ordered_ids := [];
Lwt.return ()
in
Sihl.Cleaner.register_cleaner cleaner
;;
let register_migration () = ()
let enqueue ~job_instance =
let open Job_instance in
let id = job_instance.id in
ordered_ids := List.cons id !ordered_ids;
state := Map.add id job_instance !state;
Lwt.return ()
;;
let update ~job_instance =
let open Job_instance in
let id = job_instance.id in
state := Map.add id job_instance !state;
Lwt.return ()
;;
let find_workable () =
let all_job_instances =
List.map (fun id -> Map.find_opt id !state) !ordered_ids
in
let now = Ptime_clock.now () in
let rec filter_pending all_job_instances result =
match all_job_instances with
| Some job_instance :: job_instances ->
if Job_instance.should_run ~job_instance ~now
then filter_pending job_instances (List.cons job_instance result)
else filter_pending job_instances result
| None :: job_instances -> filter_pending job_instances result
| [] -> result
in
Lwt.return @@ filter_pending all_job_instances []
;;
end
let status =
let open Job_instance in
let encode m = Ok (Status.to_string m) in
let decode = Status.of_string in
Caqti_type.(custom ~encode ~decode string)
;;
let job =
let open Job_instance in
let encode m =
Ok
( m.id
, (m.name, (m.input, (m.tries, (m.next_run_at, (m.max_tries, m.status)))))
)
in
let decode (id, (name, (input, (tries, (next_run_at, (max_tries, status)))))) =
Ok { id; name; input; tries; next_run_at; max_tries; status }
in
Caqti_type.(
custom
~encode
~decode
(tup2
string
(tup2
string
(tup2 (option string) (tup2 int (tup2 ptime (tup2 int status)))))))
;;
module MakeMariaDb (MigrationService : Sihl.Contract.Migration.Sig) : Sig =
struct
let lifecycles = [ Sihl.Database.lifecycle; MigrationService.lifecycle ]
let enqueue_request =
Caqti_request.exec
job
{sql|
INSERT INTO queue_jobs (
uuid,
name,
input,
tries,
next_run_at,
max_tries,
status
) VALUES (
UNHEX(REPLACE(?, '-', '')),
?,
?,
?,
?,
?,
?
)
|sql}
;;
let enqueue ~job_instance =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec enqueue_request job_instance
|> Lwt.map Sihl.Database.raise_error)
;;
let update_request =
Caqti_request.exec
job
{sql|
UPDATE queue_jobs
SET
name = $2,
input = $3,
tries = $4,
next_run_at = $5,
max_tries = $6,
status = $7
WHERE
queue_jobs.uuid = UNHEX(REPLACE($1, '-', ''))
|sql}
;;
let update ~job_instance =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec update_request job_instance
|> Lwt.map Sihl.Database.raise_error)
;;
let find_workable_request =
Caqti_request.collect
Caqti_type.unit
job
{sql|
SELECT
LOWER(CONCAT(
SUBSTR(HEX(uuid), 1, 8), '-',
SUBSTR(HEX(uuid), 9, 4), '-',
SUBSTR(HEX(uuid), 13, 4), '-',
SUBSTR(HEX(uuid), 17, 4), '-',
SUBSTR(HEX(uuid), 21)
)),
name,
input,
tries,
next_run_at,
max_tries,
status
FROM queue_jobs
WHERE
status = "pending"
AND next_run_at <= NOW()
AND tries < max_tries
ORDER BY id DESC
|sql}
;;
let find_workable () =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.collect_list find_workable_request ()
|> Lwt.map Sihl.Database.raise_error)
;;
let clean_request =
Caqti_request.exec Caqti_type.unit "TRUNCATE TABLE queue_jobs;"
;;
let clean () =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec clean_request () |> Lwt.map Sihl.Database.raise_error)
;;
module Migration = struct
let fix_collation =
Sihl.Database.Migration.create_step
~label:"fix collation"
"SET collation_server = 'utf8mb4_unicode_ci';"
;;
let create_jobs_table =
Sihl.Database.Migration.create_step
~label:"create jobs table"
{sql|
CREATE TABLE IF NOT EXISTS queue_jobs (
id BIGINT UNSIGNED AUTO_INCREMENT,
uuid BINARY(16) NOT NULL,
name VARCHAR(128) NOT NULL,
input TEXT NULL,
tries BIGINT UNSIGNED,
next_run_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
max_tries BIGINT UNSIGNED,
status VARCHAR(128) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT unique_uuid UNIQUE KEY (uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|sql}
;;
let migration =
Sihl.Database.Migration.(
empty "queue" |> add_step fix_collation |> add_step create_jobs_table)
;;
end
let register_cleaner () = Sihl.Cleaner.register_cleaner clean
let register_migration () =
MigrationService.register_migration Migration.migration
;;
end
module MakePostgreSql (MigrationService : Sihl.Contract.Migration.Sig) : Sig =
struct
let lifecycles = [ Sihl.Database.lifecycle; MigrationService.lifecycle ]
let enqueue_request =
Caqti_request.exec
job
{sql|
INSERT INTO queue_jobs (
uuid,
name,
input,
tries,
next_run_at,
max_tries,
status
) VALUES (
?,
?,
?,
?,
?,
?,
?
)
|sql}
;;
let enqueue ~job_instance =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec enqueue_request job_instance
|> Lwt.map Sihl.Database.raise_error)
;;
let update_request =
Caqti_request.exec
job
{sql|
UPDATE queue_jobs
SET
name = $2,
input = $3,
tries = $4,
next_run_at = $5,
max_tries = $6,
status = $7
WHERE
queue_jobs.uuid = $1::uuid
|sql}
;;
let update ~job_instance =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec update_request job_instance
|> Lwt.map Sihl.Database.raise_error)
;;
let find_workable_request =
Caqti_request.collect
Caqti_type.unit
job
{sql|
SELECT
uuid,
name,
input,
tries,
next_run_at,
max_tries,
status
FROM queue_jobs
WHERE
status = 'pending'
AND next_run_at <= NOW()
AND tries < max_tries
ORDER BY id DESC
|sql}
;;
let find_workable () =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.collect_list find_workable_request ()
|> Lwt.map Sihl.Database.raise_error)
;;
let clean_request =
Caqti_request.exec Caqti_type.unit "TRUNCATE TABLE queue_jobs;"
;;
let clean () =
Sihl.Database.query (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec clean_request () |> Lwt.map Sihl.Database.raise_error)
;;
module Migration = struct
let create_jobs_table =
Sihl.Database.Migration.create_step
~label:"create jobs table"
{sql|
CREATE TABLE IF NOT EXISTS queue_jobs (
id serial,
uuid uuid NOT NULL,
name VARCHAR(128) NOT NULL,
input TEXT NULL,
tries BIGINT,
next_run_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
max_tries BIGINT,
status VARCHAR(128) NOT NULL,
PRIMARY KEY (id),
UNIQUE (uuid)
);
|sql}
;;
let migration =
Sihl.Database.Migration.(empty "queue" |> add_step create_jobs_table)
;;
end
let register_cleaner () = Sihl.Cleaner.register_cleaner clean
let register_migration () =
MigrationService.register_migration Migration.migration
;;
end