Polling Conditions ๐
Master the art of handling long-running tasks! Learn how to make your workflow wait for operations to complete.
๐ When do I need this? Use polling when your workflow needs to wait for an operation to complete or a state to be reached:
- ๐ Long-running Jobs: Wait for tasks like data processing or report generation to finish.
- โณ Resource Provisioning: Ensure resources like databases or servers are ready before proceeding.
- ๐ Progress Tracking: Monitor the status of operations like file uploads or asynchronous API calls.
- ๐ External API Operations: Wait for external services (e.g., payment processing) to confirm completion.
- ๐ก๏ธ Building Reliable Systems: Handle scenarios where you need to confirm a state before moving on.
Common Polling Examples
Here are some typical polling conditions you might use:
// Wait for a job to complete
conditions: {
pollUntil: ["{{ status = 'completed' }}"]
}
Real World Example ๐
Here's a common scenario where polling is essential - processing a large dataset:
import { GraphCompose } from '@graph-compose/client';
const workflow = new GraphCompose()
// ๐ Step 1: Start the data processing job
.node('start_job')
.post('http://api.example.com/jobs')
.withBody({
type: 'process_data'
})
.end()
// ๐ Step 2: Poll the job status until it's complete
.node('check_status')
.withDependencies(['start_job'])
.get('http://api.example.com/jobs/{{results.start_job.jobId}}')
// โฑ๏ธ Step 3: The retry policy controls how often we check
.withRetries({
initialInterval: '1 second',
maximumAttempts: 3,
maximumInterval: '1 second'
})
.withConditions({
pollUntil: ["{{ status = 'completed' }}"] // Wait until status is 'completed'
})
.end();
// ๐ก Step 4: Workflow continues automatically after 'check_status' completes
How It Works ๐ง
Polling is simple but powerful:
- Make an HTTP request
- Check if
pollUntil
condition is true - If true โ continue workflow
- If false โ retry based on retry policy
- Repeat until success or max attempts reached
โ ๏ธ Important:
- The
pollUntil
condition must evaluate to a boolean value - Use
activityConfig.retryPolicy
to control polling frequency and limits - The condition is evaluated against the response data
- Variables from previous nodes can be accessed via
results
Advanced Usage ๐
Combining with Flow Control
You can combine polling with Flow Control for complex scenarios:
{
"id": "check_status",
"type": "http",
"conditions": {
"pollUntil": ["{{ status != 'in_progress' }}"],
"terminateWhen": ["{{ status = 'failed' }}"],
"continueTo": [
{
"to": "process_results",
"when": "{{ status = 'completed' }}"
}
]
}
}
This setup:
- ๐ Polls until status changes from 'in_progress'
- ๐ Terminates if status is 'failed'
- โจ Continues to process results if completed