|
1 | | -import { logger, task, wait } from "@trigger.dev/sdk/v3"; |
| 1 | +import { batch, logger, task, wait } from "@trigger.dev/sdk/v3"; |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * A simple task that processes data updates. |
@@ -570,3 +570,163 @@ export const testDifferentDelays = task({ |
570 | 570 | return { triggered: true }; |
571 | 571 | }, |
572 | 572 | }); |
| 573 | + |
| 574 | +/** |
| 575 | + * Example 7: Batch Trigger with Debounce |
| 576 | + * |
| 577 | + * Demonstrates using debounce with batchTrigger. |
| 578 | + * Each item in the batch can have its own debounce key and delay. |
| 579 | + * Items with the same debounce key will be consolidated into a single run. |
| 580 | + */ |
| 581 | +export const batchItemTask = task({ |
| 582 | + id: "batch-item-task", |
| 583 | + run: async (payload: { itemId: string; data: string }) => { |
| 584 | + logger.info("Processing batch item", { payload }); |
| 585 | + |
| 586 | + await wait.for({ seconds: 1 }); |
| 587 | + |
| 588 | + logger.info("Batch item processed", { itemId: payload.itemId }); |
| 589 | + |
| 590 | + return { |
| 591 | + processed: true, |
| 592 | + itemId: payload.itemId, |
| 593 | + processedAt: new Date().toISOString(), |
| 594 | + }; |
| 595 | + }, |
| 596 | +}); |
| 597 | + |
| 598 | +/** |
| 599 | + * Demonstrates batch.trigger() with debounce options on individual items. |
| 600 | + * |
| 601 | + * This shows how you can: |
| 602 | + * - Use different debounce keys for different items |
| 603 | + * - Items with the same debounce key will be consolidated |
| 604 | + * - Items with different keys will create separate runs |
| 605 | + * |
| 606 | + * Run this task and watch: |
| 607 | + * - Items 1 and 3 share debounce key "group-a" -> ONE run |
| 608 | + * - Items 2 and 4 share debounce key "group-b" -> ONE run |
| 609 | + * - Item 5 has unique key "group-c" -> ONE run |
| 610 | + * - Total: 3 runs instead of 5 (but batch shows 5 items) |
| 611 | + * |
| 612 | + * Note: The batch itself still reports 5 items, but only 3 actual task runs |
| 613 | + * will execute due to debouncing. |
| 614 | + */ |
| 615 | +export const demonstrateBatchDebounce = task({ |
| 616 | + id: "demonstrate-batch-debounce", |
| 617 | + run: async (payload: { prefix?: string }) => { |
| 618 | + const prefix = payload.prefix ?? "batch-demo"; |
| 619 | + |
| 620 | + logger.info("Starting batch debounce demonstration"); |
| 621 | + logger.info("Will trigger 5 items with 3 different debounce keys"); |
| 622 | + logger.info( |
| 623 | + "Items 1&3 share key 'group-a', items 2&4 share key 'group-b', item 5 has key 'group-c'" |
| 624 | + ); |
| 625 | + |
| 626 | + // Use batch.trigger with debounce options on each item |
| 627 | + const result = await batch.trigger<typeof batchItemTask>([ |
| 628 | + { |
| 629 | + id: "batch-item-task", |
| 630 | + payload: { itemId: `${prefix}-1`, data: "First item in group A" }, |
| 631 | + options: { |
| 632 | + debounce: { key: `${prefix}-group-a`, delay: "5s" }, |
| 633 | + }, |
| 634 | + }, |
| 635 | + { |
| 636 | + id: "batch-item-task", |
| 637 | + payload: { itemId: `${prefix}-2`, data: "First item in group B" }, |
| 638 | + options: { |
| 639 | + debounce: { key: `${prefix}-group-b`, delay: "5s" }, |
| 640 | + }, |
| 641 | + }, |
| 642 | + { |
| 643 | + id: "batch-item-task", |
| 644 | + payload: { itemId: `${prefix}-3`, data: "Second item in group A (debounced)" }, |
| 645 | + options: { |
| 646 | + debounce: { key: `${prefix}-group-a`, delay: "5s" }, |
| 647 | + }, |
| 648 | + }, |
| 649 | + { |
| 650 | + id: "batch-item-task", |
| 651 | + payload: { itemId: `${prefix}-4`, data: "Second item in group B (debounced)" }, |
| 652 | + options: { |
| 653 | + debounce: { key: `${prefix}-group-b`, delay: "5s" }, |
| 654 | + }, |
| 655 | + }, |
| 656 | + { |
| 657 | + id: "batch-item-task", |
| 658 | + payload: { itemId: `${prefix}-5`, data: "Only item in group C" }, |
| 659 | + options: { |
| 660 | + debounce: { key: `${prefix}-group-c`, delay: "5s" }, |
| 661 | + }, |
| 662 | + }, |
| 663 | + ]); |
| 664 | + |
| 665 | + logger.info("Batch debounce demonstration complete", { |
| 666 | + batchId: result.batchId, |
| 667 | + totalItemsInBatch: result.runCount, |
| 668 | + note: "Check the dashboard - only 3 actual task runs should execute due to debouncing", |
| 669 | + }); |
| 670 | + |
| 671 | + return { |
| 672 | + batchId: result.batchId, |
| 673 | + totalItemsInBatch: result.runCount, |
| 674 | + expectedUniqueRuns: 3, |
| 675 | + message: |
| 676 | + "5 items submitted, but only 3 runs will execute: group-a (1 run), group-b (1 run), group-c (1 run)", |
| 677 | + }; |
| 678 | + }, |
| 679 | +}); |
| 680 | + |
| 681 | +/** |
| 682 | + * Demonstrates batchTrigger on a single task with debounce. |
| 683 | + * |
| 684 | + * Similar to batch.trigger but using myTask.batchTrigger() syntax. |
| 685 | + * Each item can have its own debounce configuration. |
| 686 | + * |
| 687 | + * When all items share the same debounce key, only ONE run will execute. |
| 688 | + */ |
| 689 | +export const demonstrateSingleTaskBatchDebounce = task({ |
| 690 | + id: "demonstrate-single-task-batch-debounce", |
| 691 | + run: async (payload: { debounceKey?: string }) => { |
| 692 | + const key = payload.debounceKey ?? "single-batch-demo"; |
| 693 | + |
| 694 | + logger.info("Starting single task batch debounce demonstration", { debounceKey: key }); |
| 695 | + logger.info("Triggering 4 items with the SAME debounce key - only 1 run should execute"); |
| 696 | + |
| 697 | + // All items have the same debounce key, so they should all resolve to the same run |
| 698 | + const result = await batchItemTask.batchTrigger([ |
| 699 | + { |
| 700 | + payload: { itemId: `${key}-1`, data: "Item 1" }, |
| 701 | + options: { debounce: { key, delay: "5s" } }, |
| 702 | + }, |
| 703 | + { |
| 704 | + payload: { itemId: `${key}-2`, data: "Item 2" }, |
| 705 | + options: { debounce: { key, delay: "5s" } }, |
| 706 | + }, |
| 707 | + { |
| 708 | + payload: { itemId: `${key}-3`, data: "Item 3" }, |
| 709 | + options: { debounce: { key, delay: "5s" } }, |
| 710 | + }, |
| 711 | + { |
| 712 | + payload: { itemId: `${key}-4`, data: "Item 4" }, |
| 713 | + options: { debounce: { key, delay: "5s" } }, |
| 714 | + }, |
| 715 | + ]); |
| 716 | + |
| 717 | + logger.info("Single task batch debounce complete", { |
| 718 | + batchId: result.batchId, |
| 719 | + totalItemsInBatch: result.runCount, |
| 720 | + debounceKey: key, |
| 721 | + note: "All items share the same debounce key, so only 1 task run should execute", |
| 722 | + }); |
| 723 | + |
| 724 | + return { |
| 725 | + batchId: result.batchId, |
| 726 | + totalItemsInBatch: result.runCount, |
| 727 | + debounceKey: key, |
| 728 | + expectedUniqueRuns: 1, |
| 729 | + message: "4 items submitted with same debounce key - only 1 run will execute", |
| 730 | + }; |
| 731 | + }, |
| 732 | +}); |
0 commit comments