-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.py
More file actions
719 lines (555 loc) · 21.8 KB
/
client.py
File metadata and controls
719 lines (555 loc) · 21.8 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
from dataclasses import dataclass, field
from datetime import datetime, timezone
from hashlib import sha256
import re
from typing import (
Optional,
Protocol,
List,
runtime_checkable,
)
import json
from riverqueue.insert_opts import InsertOpts, UniqueOpts
from .driver import (
JobInsertParams,
DriverProtocol,
)
from .driver.driver_protocol import AsyncDriverProtocol, ExecutorProtocol
from .job import Job, JobState
JOB_STATE_BIT_POSITIONS = {
JobState.AVAILABLE: 7,
JobState.CANCELLED: 6,
JobState.COMPLETED: 5,
JobState.DISCARDED: 4,
JobState.PENDING: 3,
JobState.RETRYABLE: 2,
JobState.RUNNING: 1,
JobState.SCHEDULED: 0,
}
"""
Maps job states to bit positions in a unique bitmask.
"""
MAX_ATTEMPTS_DEFAULT: int = 25
"""
Default number of maximum attempts for a job.
"""
PRIORITY_DEFAULT: int = 1
"""
Default priority for a job.
"""
QUEUE_DEFAULT: str = "default"
"""
Default queue for a job.
"""
UNIQUE_STATES_DEFAULT: list[JobState] = [
JobState.AVAILABLE,
JobState.COMPLETED,
JobState.PENDING,
JobState.RUNNING,
JobState.RETRYABLE,
JobState.SCHEDULED,
]
"""
Default job states included during a unique job insertion.
"""
UNIQUE_STATES_REQUIRED: list[JobState] = [
JobState.AVAILABLE,
JobState.PENDING,
JobState.RUNNING,
JobState.SCHEDULED,
]
"""
Job states required when customizing the state list for unique job insertion.
"""
@dataclass
class InsertResult:
job: "Job"
"""
Inserted job row, or an existing job row if insert was skipped due to a
previously existing unique job.
"""
unique_skipped_as_duplicated: bool = field(default=False)
"""
True if for a unique job, the insertion was skipped due to an equivalent job
matching unique property already being present.
"""
class JobArgs(Protocol):
"""
Protocol that should be implemented by all job args.
"""
kind: str
def to_json(self) -> str:
pass
@runtime_checkable
class JobArgsWithInsertOpts(Protocol):
"""
Protocol that's optionally implemented by a JobArgs implementation so that
every inserted instance of them provides the same custom `InsertOpts`.
`InsertOpts` passed to insert functions will take precedence of one returned
by `JobArgsWithInsertOpts`.
"""
def insert_opts(self) -> InsertOpts:
pass
@dataclass
class InsertManyParams:
"""
A single job to insert that's part of an `insert_many()` batch insert.
Unlike sending raw job args, supports an `InsertOpts` to pair with the job.
"""
args: JobArgs
"""
Job args to insert.
"""
insert_opts: Optional[InsertOpts] = None
"""
Insertion options to use with the insert.
"""
class AsyncClient:
"""
Provides a client for River that inserts jobs. Unlike the Go version of the
River client, this one can insert jobs only. Jobs can only be worked from Go
code, so job arg kinds and JSON encoding details must be shared between Ruby
and Go code.
Used in conjunction with a River driver like:
```
import riverqueue
from riverqueue.driver import riversqlalchemy
engine = sqlalchemy.ext.asyncio.create_async_engine("postgresql+asyncpg://...")
client = riverqueue.AsyncClient(riversqlalchemy.AsyncDriver(engine))
```
This variant is for use with Python's asyncio (asynchronous I/O).
"""
def __init__(self, driver: AsyncDriverProtocol):
self.driver = driver
async def insert(
self, args: JobArgs, insert_opts: Optional[InsertOpts] = None
) -> InsertResult:
"""
Inserts a new job for work given a job args implementation and insertion
options (which may be omitted).
With job args only:
```
insert_res = await client.insert(
SortArgs(strings=["whale", "tiger", "bear"]),
)
insert_res.job # inserted job row
```
With insert opts:
```
insert_res = await client.insert(
SortArgs(strings=["whale", "tiger", "bear"]),
insert_opts=riverqueue.InsertOpts(
max_attempts=17,
priority=3,
queue: "my_queue",
tags: ["custom"]
),
)
insert_res.job # inserted job row
```
Job arg implementations are expected to respond to:
* `kind` is a unique string that identifies them the job in the
database, and which a Go worker will recognize.
* `to_json()` defines how the job will serialize to JSON, which of
course will have to be parseable as an object in Go.
They may also respond to `insert_opts()` which is expected to return an
`InsertOpts` that contains options that will apply to all jobs of this
kind. Insertion options provided as an argument to `insert()` override
those returned by job args.
For example:
```
@dataclass
class SortArgs:
strings: list[str]
kind: str = "sort"
def to_json(self) -> str:
return json.dumps({"strings": self.strings})
```
We recommend using `@dataclass` for job args since they should ideally
be minimal sets of primitive properties with little other embellishment,
and `@dataclass` provides a succinct way of accomplishing this.
Returns an instance of `InsertResult`.
"""
if not insert_opts:
insert_opts = InsertOpts()
return (await self.insert_many([InsertManyParams(args, insert_opts)]))[0]
async def insert_tx(
self, tx, args: JobArgs, insert_opts: Optional[InsertOpts] = None
) -> InsertResult:
"""
Inserts a new job for work given a job args implementation and insertion
options (which may be omitted).
This variant inserts a job in an open transaction. For example:
```
with engine.begin() as session:
insert_res = await client.insert_tx(
session,
SortArgs(strings=["whale", "tiger", "bear"]),
)
```
With insert opts:
```
with engine.begin() as session:
insert_res = await client.insert_tx(
session,
SortArgs(strings=["whale", "tiger", "bear"]),
insert_opts=riverqueue.InsertOpts(
max_attempts=17,
priority=3,
queue: "my_queue",
tags: ["custom"]
),
)
insert_res.job # inserted job row
```
"""
if not insert_opts:
insert_opts = InsertOpts()
return (await self.insert_many_tx(tx, [InsertManyParams(args, insert_opts)]))[0]
async def insert_many(
self, args: List[JobArgs | InsertManyParams]
) -> list[InsertResult]:
"""
Inserts many new jobs as part of a single batch operation for improved
efficiency.
Takes an array of job args or `InsertManyParams` which encapsulate job
args and a paired `InsertOpts`.
With job args:
```
num_inserted = await client.insert_many([
SimpleArgs(job_num: 1),
SimpleArgs(job_num: 2)
])
```
With `InsertManyParams`:
```
num_inserted = await client.insert_many([
InsertManyParams(args=SimpleArgs.new(job_num: 1), insert_opts=riverqueue.InsertOpts.new(max_attempts=5)),
InsertManyParams(args=SimpleArgs.new(job_num: 2), insert_opts=riverqueue.InsertOpts.new(queue="high_priority"))
])
```
Unique job insertion isn't supported with bulk insertion because it'd
run the risk of major lock contention.
Returns the number of jobs inserted.
"""
async with self.driver.executor() as exec:
res = await exec.job_insert_many(_make_driver_insert_params_many(args))
return _to_insert_results(res)
async def insert_many_tx(
self, tx, args: List[JobArgs | InsertManyParams]
) -> list[InsertResult]:
"""
Inserts many new jobs as part of a single batch operation for improved
efficiency.
This variant inserts a job in an open transaction. For example:
```
with engine.begin() as session:
num_inserted = await client.insert_many_tx(session, [
SimpleArgs(job_num: 1),
SimpleArgs(job_num: 2)
])
```
With `InsertManyParams`:
```
with engine.begin() as session:
num_inserted = await client.insert_many_tx(session, [
InsertManyParams(args=SimpleArgs.new(job_num: 1), insert_opts=riverqueue.InsertOpts.new(max_attempts=5)),
InsertManyParams(args=SimpleArgs.new(job_num: 2), insert_opts=riverqueue.InsertOpts.new(queue="high_priority"))
])
```
Unique job insertion isn't supported with bulk insertion because it'd
run the risk of major lock contention.
Returns the number of jobs inserted.
"""
exec = self.driver.unwrap_executor(tx)
res = await exec.job_insert_many(_make_driver_insert_params_many(args))
return _to_insert_results(res)
class Client:
"""
Provides a client for River that inserts jobs. Unlike the Go version of the
River client, this one can insert jobs only. Jobs can only be worked from Go
code, so job arg kinds and JSON encoding details must be shared between Ruby
and Go code.
Used in conjunction with a River driver like:
```
import riverqueue
from riverqueue.driver import riversqlalchemy
engine = sqlalchemy.create_engine("postgresql://...")
client = riverqueue.Client(riversqlalchemy.Driver(engine))
```
"""
def __init__(self, driver: DriverProtocol):
self.driver = driver
def insert(
self, args: JobArgs, insert_opts: Optional[InsertOpts] = None
) -> InsertResult:
"""
Inserts a new job for work given a job args implementation and insertion
options (which may be omitted).
With job args only:
```
insert_res = client.insert(
SortArgs(strings=["whale", "tiger", "bear"]),
)
insert_res.job # inserted job row
```
With insert opts:
```
insert_res = client.insert(
SortArgs(strings=["whale", "tiger", "bear"]),
insert_opts=riverqueue.InsertOpts(
max_attempts=17,
priority=3,
queue: "my_queue",
tags: ["custom"]
),
)
insert_res.job # inserted job row
```
Job arg implementations are expected to respond to:
* `kind` is a unique string that identifies them the job in the
database, and which a Go worker will recognize.
* `to_json()` defines how the job will serialize to JSON, which of
course will have to be parseable as an object in Go.
They may also respond to `insert_opts()` which is expected to return an
`InsertOpts` that contains options that will apply to all jobs of this
kind. Insertion options provided as an argument to `insert()` override
those returned by job args.
For example:
```
@dataclass
class SortArgs:
strings: list[str]
kind: str = "sort"
def to_json(self) -> str:
return json.dumps({"strings": self.strings})
```
We recommend using `@dataclass` for job args since they should ideally
be minimal sets of primitive properties with little other embellishment,
and `@dataclass` provides a succinct way of accomplishing this.
Returns an instance of `InsertResult`.
"""
if not insert_opts:
insert_opts = InsertOpts()
return self.insert_many([InsertManyParams(args, insert_opts)])[0]
def insert_tx(
self, tx, args: JobArgs, insert_opts: Optional[InsertOpts] = None
) -> InsertResult:
"""
Inserts a new job for work given a job args implementation and insertion
options (which may be omitted).
This variant inserts a job in an open transaction. For example:
```
with engine.begin() as session:
insert_res = client.insert_tx(
session,
SortArgs(strings=["whale", "tiger", "bear"]),
)
```
With insert opts:
```
with engine.begin() as session:
insert_res = client.insert_tx(
session,
SortArgs(strings=["whale", "tiger", "bear"]),
insert_opts=riverqueue.InsertOpts(
max_attempts=17,
priority=3,
queue: "my_queue",
tags: ["custom"]
),
)
insert_res.job # inserted job row
```
"""
if not insert_opts:
insert_opts = InsertOpts()
return self.insert_many_tx(tx, [InsertManyParams(args, insert_opts)])[0]
def insert_many(self, args: List[JobArgs | InsertManyParams]) -> list[InsertResult]:
"""
Inserts many new jobs as part of a single batch operation for improved
efficiency.
Takes an array of job args or `InsertManyParams` which encapsulate job
args and a paired `InsertOpts`.
With job args:
```
num_inserted = client.insert_many([
SimpleArgs(job_num: 1),
SimpleArgs(job_num: 2)
])
```
With `InsertManyParams`:
```
num_inserted = client.insert_many([
InsertManyParams(args=SimpleArgs.new(job_num: 1), insert_opts=riverqueue.InsertOpts.new(max_attempts=5)),
InsertManyParams(args=SimpleArgs.new(job_num: 2), insert_opts=riverqueue.InsertOpts.new(queue="high_priority"))
])
```
Unique job insertion isn't supported with bulk insertion because it'd
run the risk of major lock contention.
Returns the number of jobs inserted.
"""
with self.driver.executor() as exec:
return self._insert_many_exec(exec, args)
def insert_many_tx(
self, tx, args: List[JobArgs | InsertManyParams]
) -> list[InsertResult]:
"""
Inserts many new jobs as part of a single batch operation for improved
efficiency.
This variant inserts a job in an open transaction. For example:
```
with engine.begin() as session:
num_inserted = client.insert_many_tx(session, [
SimpleArgs(job_num: 1),
SimpleArgs(job_num: 2)
])
```
With `InsertManyParams`:
```
with engine.begin() as session:
num_inserted = client.insert_many_tx(session, [
InsertManyParams(args=SimpleArgs.new(job_num: 1), insert_opts=riverqueue.InsertOpts.new(max_attempts=5)),
InsertManyParams(args=SimpleArgs.new(job_num: 2), insert_opts=riverqueue.InsertOpts.new(queue="high_priority"))
])
```
Unique job insertion isn't supported with bulk insertion because it'd
run the risk of major lock contention.
Returns the number of jobs inserted.
"""
return self._insert_many_exec(self.driver.unwrap_executor(tx), args)
def _insert_many_exec(
self, exec: ExecutorProtocol, args: List[JobArgs | InsertManyParams]
) -> list[InsertResult]:
res = exec.job_insert_many(_make_driver_insert_params_many(args))
return _to_insert_results(res)
def _build_unique_key_and_bitmask(
insert_params: JobInsertParams,
unique_opts: UniqueOpts,
) -> tuple[Optional[bytes], Optional[bytes]]:
"""
Builds driver get params and a unique key from insert params and unique
options for use during a job insertion.
"""
any_unique_opts = False
unique_key = ""
if not unique_opts.exclude_kind:
unique_key += f"&kind={insert_params.kind}"
if unique_opts.by_args:
any_unique_opts = True
# Re-parse the args JSON for sorting and potentially filtering:
args_dict = json.loads(insert_params.args)
args_to_include = args_dict
if unique_opts.by_args is not True:
# Filter to include only the specified keys:
args_to_include = {
key: args_dict[key] for key in unique_opts.by_args if key in args_dict
}
# Serialize with sorted keys and append to unique key. Remove whitespace
# from the JSON to match other implementations:
sorted_args = json.dumps(args_to_include, sort_keys=True, separators=(",", ":"))
unique_key += f"&args={sorted_args}"
if unique_opts.by_period:
lower_period_bound = _truncate_time(
datetime.now(timezone.utc), unique_opts.by_period
)
any_unique_opts = True
unique_key += f"&period={lower_period_bound.strftime('%FT%TZ')}"
if unique_opts.by_queue:
any_unique_opts = True
unique_key += f"&queue={insert_params.queue}"
if unique_opts.by_state:
any_unique_opts = True
unique_key += f"&state={','.join(unique_opts.by_state)}"
else:
unique_key += f"&state={','.join(UNIQUE_STATES_DEFAULT)}"
if not any_unique_opts:
return (None, None)
unique_key_hash = sha256(unique_key.encode("utf-8")).digest()
unique_states = _validate_unique_states(
unique_opts.by_state or UNIQUE_STATES_DEFAULT
)
return unique_key_hash, unique_bitmask_from_states(unique_states)
def _make_driver_insert_params(
args: JobArgs, insert_opts: InsertOpts
) -> JobInsertParams:
"""
Converts user-land job args and insert options to insert params for an
underlying driver.
"""
args.kind # fail fast in case args don't respond to kind
args_json = args.to_json()
assert args_json is not None, "args should return non-nil from `to_json`"
args_insert_opts = InsertOpts()
if isinstance(args, JobArgsWithInsertOpts):
args_insert_opts = args.insert_opts()
scheduled_at = insert_opts.scheduled_at or args_insert_opts.scheduled_at
unique_opts = insert_opts.unique_opts or args_insert_opts.unique_opts
insert_params = JobInsertParams(
args=args_json,
kind=args.kind,
max_attempts=insert_opts.max_attempts
or args_insert_opts.max_attempts
or MAX_ATTEMPTS_DEFAULT,
priority=insert_opts.priority or args_insert_opts.priority or PRIORITY_DEFAULT,
queue=insert_opts.queue or args_insert_opts.queue or QUEUE_DEFAULT,
scheduled_at=scheduled_at and scheduled_at.astimezone(timezone.utc),
state="scheduled" if scheduled_at else "available",
tags=_validate_tags(insert_opts.tags or args_insert_opts.tags or []),
)
unique_opts = insert_opts.unique_opts or args_insert_opts.unique_opts
if unique_opts:
unique_key, unique_states = _build_unique_key_and_bitmask(
insert_params, unique_opts
)
insert_params.unique_key = unique_key
insert_params.unique_states = unique_states
return insert_params
def _make_driver_insert_params_many(
args: List[JobArgs | InsertManyParams],
) -> List[JobInsertParams]:
return [
_make_driver_insert_params(arg.args, arg.insert_opts or InsertOpts())
if isinstance(arg, InsertManyParams)
else _make_driver_insert_params(arg, InsertOpts())
for arg in args
]
def _truncate_time(time, interval_seconds) -> datetime:
return datetime.fromtimestamp(
(time.timestamp() // interval_seconds) * interval_seconds, tz=timezone.utc
)
def _to_insert_results(results: list[tuple[Job, bool]]) -> list[InsertResult]:
return [
InsertResult(job, unique_skipped_as_duplicated)
for job, unique_skipped_as_duplicated in results
]
def unique_bitmask_from_states(states: list[JobState]) -> bytes:
val = 0
for state in states:
bit_index = JOB_STATE_BIT_POSITIONS[state]
bit_position = 7 - (bit_index % 8)
val |= 1 << bit_position
return val.to_bytes(1, "big") # Returns bytes like b'\xf5'
def unique_bitmask_to_states(mask: str) -> list[JobState]:
states = []
# This logic differs a bit from the above because we're working with a string
# of Postgres' bit(8) representation where the bit numbering is reversed
# (MSB on the right).
for state, bit_index in JOB_STATE_BIT_POSITIONS.items():
if mask[bit_index] == "1":
states.append(state)
return sorted(states)
tag_re = re.compile(r"\A[\w][\w\-]+[\w]\Z")
def _validate_tags(tags: list[str]) -> list[str]:
for tag in tags:
assert len(tag) <= 255 and tag_re.match(tag), (
f"tags should be less than 255 characters in length and match regex {tag_re.pattern}"
)
return tags
def _validate_unique_states(states: list[JobState]) -> list[JobState]:
for required_state in UNIQUE_STATES_REQUIRED:
if required_state not in states:
raise ValueError(
f"by_state should include required state '{required_state}'"
)
return states