Overview
Activities are the way you execute side effects in Durable Workflow. Unlike workflows, which must be deterministic, activities can perform non-deterministic operations like:- Making HTTP requests
- Reading from databases
- Sending emails
- Calling third-party APIs
- Generating random values
- Any operation with external dependencies
The Activity Base Class
Every activity extends theActivity class and implements an execute() method:
Activity Constructor
Activities are constructed with context information from the workflow:src/Activity.php
$index: The position of this activity in the workflow execution$now: Deterministic timestamp from the workflow$storedWorkflow: Reference to the workflow that spawned this activity$arguments: Parameters passed when the activity was yielded
Executing Activities
Theexecute() method contains your activity’s logic and is automatically called when the activity job is processed:
src/Activity.php
Idempotency
Activities are idempotent—if an activity has already executed (checked viahasLogByIndex), it won’t run again. This ensures activities execute exactly once, even if retried.
Creating Activities
Here’s a complete example of an activity:Using Activities in Workflows
You invoke activities from workflows usingActivityStub::make():
- The workflow pauses and transitions to “waiting” state
- The activity is dispatched as a queue job
- The activity executes independently
- The workflow resumes with the activity’s return value
Retry Behavior
Activities automatically retry on failure with exponential backoff:src/Activity.php
Customizing Retry Behavior
You can customize retry behavior in your activity:Non-Retryable Exceptions
Some exceptions shouldn’t trigger retries. ImplementNonRetryableExceptionContract to fail immediately:
src/Activity.php
Activity Timeouts
You can configure timeouts to prevent activities from running too long:Accessing Workflow Context
Activities can access information about the workflow that spawned them:Available Context Methods
src/Activity.php
Dependency Injection
Activities support Laravel’s dependency injection:Error Handling
When an activity fails permanently, the exception is stored and the workflow is notified:src/Activity.php
Activity Uniqueness
Each activity has a unique identifier based on the workflow and execution index:src/Activity.php
Best Practices
Keep activities focused
Keep activities focused
Each activity should do one thing well. If an activity is doing multiple unrelated operations, split it into separate activities.
Make activities idempotent
Make activities idempotent
Design your activities so they can be safely retried. Check if work has already been done before performing side effects.
Return meaningful data
Return meaningful data
Return data that the workflow needs for decision making. Don’t return large objects unnecessarily.
Handle expected failures gracefully
Handle expected failures gracefully
Catch expected exceptions and return error states instead of throwing, allowing the workflow to handle them.
Next Steps
Workflows
Learn how workflows orchestrate activities
State Management
Understand how workflow state is managed