Commit a5e973f
authored
Debouncing (#455)
Workflows can now be debounced. Debouncing delays workflow execution
until some time has passed since the workflow has last been called. This
is useful for preventing wasted work when a workflow may be triggered
multiple times in quick succession.
For example, if a user is editing a form, we can debounce every change
to the form and perform a synchronization workflow only after they
haven't edited the form for a certain period of time.
### Debouncer.create
```python
Debouncer.create(
workflow: Callable[P, R],
*,
debounce_key: str,
debounce_timeout_sec: Optional[float] = None,
queue: Optional[Queue] = None,
) -> Debouncer[P, R]
```
**Parameters:**
- `workflow`: The workflow to debounce.
- `debounce_key`: The **unique** debounce key for this debouncer. Used
to group workflows that will be debounced.
- `debounce_timeout_sec`: After this time elapses since the first time a
workflow is submitted from this debouncer, the workflow is started
regardless of the debounce period.
- `queue`: When starting a workflow after debouncing, enqueue it on this
queue instead of executing it directly.
### debounce
```python
debouncer.debounce(
debounce_period_sec: float,
*args: P.args,
**kwargs: P.kwargs,
) -> WorkflowHandle[R]
```
Submit a workflow for execution but delay it by `debounce_period_sec`.
Returns a handle to the workflow.
The workflow may be debounced again, which further delays its execution
(up to `debounce_timeout_sec`).
When the workflow eventually executes, it uses the **last** set of
inputs passed into `debounce`.
After the workflow begins execution, the next call to `debounce` starts
the debouncing process again for a new workflow execution.
**Example Syntax**:
```python
@DBOS.workflow()
def process_input(user_input):
...
# Each time a user submits a new input, debounce the process_input workflow.
# The workflow will wait until 60 seconds after the user stops submitting new inputs,
# then process the last input submitted.
def on_user_input_submit(user_id, user_input):
debounce_period_sec = 60
debouncer = Debouncer.create(process_input, debounce_key=user_id)
debouncer.debounce(debounce_period_sec, user_input)
```1 parent f198a7c commit a5e973f
File tree
12 files changed
+822
-31
lines changed- .github/workflows
- dbos
- tests
12 files changed
+822
-31
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
| 37 | + | |
35 | 38 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
| |||
224 | 228 | | |
225 | 229 | | |
226 | 230 | | |
227 | | - | |
| 231 | + | |
228 | 232 | | |
229 | 233 | | |
230 | 234 | | |
231 | 235 | | |
232 | 236 | | |
233 | | - | |
| 237 | + | |
234 | 238 | | |
235 | 239 | | |
236 | 240 | | |
237 | | - | |
| 241 | + | |
238 | 242 | | |
239 | 243 | | |
240 | 244 | | |
241 | 245 | | |
242 | 246 | | |
243 | | - | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
244 | 250 | | |
245 | 251 | | |
246 | 252 | | |
| |||
311 | 317 | | |
312 | 318 | | |
313 | 319 | | |
314 | | - | |
| 320 | + | |
315 | 321 | | |
316 | 322 | | |
317 | 323 | | |
318 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
319 | 327 | | |
320 | 328 | | |
321 | 329 | | |
| |||
451 | 459 | | |
452 | 460 | | |
453 | 461 | | |
454 | | - | |
| 462 | + | |
455 | 463 | | |
456 | 464 | | |
457 | 465 | | |
| |||
467 | 475 | | |
468 | 476 | | |
469 | 477 | | |
470 | | - | |
| 478 | + | |
471 | 479 | | |
472 | 480 | | |
473 | 481 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
524 | 524 | | |
525 | 525 | | |
526 | 526 | | |
| 527 | + | |
527 | 528 | | |
528 | 529 | | |
529 | 530 | | |
| |||
533 | 534 | | |
534 | 535 | | |
535 | 536 | | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
536 | 540 | | |
537 | 541 | | |
538 | 542 | | |
| |||
557 | 561 | | |
558 | 562 | | |
559 | 563 | | |
| 564 | + | |
| 565 | + | |
560 | 566 | | |
561 | 567 | | |
562 | 568 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
| 99 | + | |
98 | 100 | | |
99 | 101 | | |
100 | 102 | | |
| |||
310 | 312 | | |
311 | 313 | | |
312 | 314 | | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
317 | 331 | | |
318 | 332 | | |
319 | 333 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| |||
390 | 392 | | |
391 | 393 | | |
392 | 394 | | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
398 | 401 | | |
399 | 402 | | |
400 | 403 | | |
| |||
0 commit comments