|
| 1 | +# Sleep Workflow Sample |
| 2 | + |
| 3 | +This sample workflow demonstrates how to use the `workflow.Sleep` function in Cadence workflows. The sleep functionality allows workflows to pause execution for a specified duration before continuing with subsequent activities. |
| 4 | + |
| 5 | +## Sample Description |
| 6 | + |
| 7 | +The sample workflow: |
| 8 | +- Takes a sleep duration as input parameter |
| 9 | +- Uses `workflow.Sleep` to pause workflow execution for the specified duration |
| 10 | +- Executes a main activity after the sleep period completes |
| 11 | +- Demonstrates proper error handling for sleep operations |
| 12 | +- Shows how to configure activity options for post-sleep activities |
| 13 | + |
| 14 | +The workflow is useful for scenarios where you need to: |
| 15 | +- Implement delays or timeouts in workflow logic |
| 16 | +- Wait for external events or conditions |
| 17 | +- Implement retry mechanisms with exponential backoff |
| 18 | +- Create scheduled or periodic workflows |
| 19 | + |
| 20 | +## Key Components |
| 21 | + |
| 22 | +- **Workflow**: `sleepWorkflow` demonstrates the sleep functionality with activity execution |
| 23 | +- **Activity**: `mainSleepActivity` is executed after the sleep period |
| 24 | +- **Sleep Duration**: Configurable duration (default: 30 seconds) passed as workflow input |
| 25 | +- **Test**: Includes unit tests to verify sleep and activity execution |
| 26 | + |
| 27 | +## Steps to Run Sample |
| 28 | + |
| 29 | +1. You need a cadence service running. See details in cmd/samples/README.md |
| 30 | + |
| 31 | +2. Run the following command to start the worker: |
| 32 | + ``` |
| 33 | + ./bin/sleep -m worker |
| 34 | + ``` |
| 35 | + |
| 36 | +3. Run the following command to execute the workflow: |
| 37 | + ``` |
| 38 | + ./bin/sleep -m trigger |
| 39 | + ``` |
| 40 | + |
| 41 | +You should see logs showing: |
| 42 | +- Workflow start with sleep duration |
| 43 | +- Sleep completion message |
| 44 | +- Main activity execution |
| 45 | +- Workflow completion |
| 46 | + |
| 47 | +## Customization |
| 48 | + |
| 49 | +To modify the sleep behavior: |
| 50 | +- Change the `sleepDuration` in `main.go` to adjust the default sleep time |
| 51 | +- Modify the activity options to configure timeouts for post-sleep activities |
| 52 | +- Add additional activities or logic after the sleep period |
| 53 | +- Implement conditional sleep based on workflow state |
| 54 | + |
| 55 | +## Use Cases |
| 56 | + |
| 57 | +This pattern is useful for: |
| 58 | +- **Scheduled Tasks**: Implement workflows that need to wait before processing |
| 59 | +- **Rate Limiting**: Add delays between API calls or external service interactions |
| 60 | +- **Retry Logic**: Implement exponential backoff for failed operations |
| 61 | +- **Event-Driven Workflows**: Wait for specific time periods before checking conditions |
| 62 | +- **Batch Processing**: Add delays between batch operations to avoid overwhelming systems |
0 commit comments