Running tasks asynchronously
When you setasync: true, the API returns immediately with a confirmation that the task has started. You can then poll for execution results using the task execution history endpoint.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Run automation tasks asynchronously and poll for results.
async: true, the API returns immediately with a confirmation that the task has started. You can then poll for execution results using the task execution history endpoint.
import { TasksLegacy } from 'anchorbrowser';
// Run the task asynchronously
const execution = await TasksLegacy.runAdhocTask({
body: {
taskId: taskId,
version: '1',
async: true, // Enable async execution
inputs: {
ANCHOR_TARGET_URL: 'https://example.com',
ANCHOR_MAX_PAGES: '10'
}
}
});
console.log('Task execution started:', execution.data);
// Response: { async: true, success: true, message: 'Task execution started', taskId: '...' }
# Run the task asynchronously
execution = client.tasks_legacy.run_adhoc_task(
task_id=task_id,
version="1",
async_=True, # Enable async execution
inputs={
"ANCHOR_TARGET_URL": "https://example.com",
"ANCHOR_MAX_PAGES": "10"
}
)
print(f"Task execution started: {execution.data}")
# Response: {'async': True, 'success': True, 'message': 'Task execution started', 'taskId': '...'}
import { TasksLegacy } from 'anchorbrowser';
// Get execution results
const results = await TasksLegacy.listTaskExecutions({
path: { taskId },
query: { page: 1, limit: 1, version: 1 }
});
console.log('Execution results:', results.data);
# Get execution results
results = client.tasks_legacy.list_task_executions(
task_id,
# status="success", # Optional: filter by status (success, failure, timeout, cancelled)
page="1",
limit="1",
version="1", # Optional: filter by version
)
print(f"Execution results: {results.data}")
{
"data": {
"results": [
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"status": "success",
"executionTime": 5234,
"output": "{\"success\": true, \"message\": \"Task completed successfully\"}",
"errorMessage": null,
"startTime": "2024-01-15T10:30:00Z"
}
],
"pagination": {
"page": 1,
"limit": 1,
"total": 1,
"totalPages": 1
}
}
}
import { TasksLegacy } from 'anchorbrowser';
// Poll for task completion
async function waitForTaskCompletion(taskId: string, maxAttempts: number = 60) {
for (let i = 0; i < maxAttempts; i++) {
const results = await TasksLegacy.listTaskExecutions({
path: { taskId },
query: { page: 1, limit: 1 }
});
const latestResult = results.data?.results?.[0];
if (latestResult) {
if (latestResult.status === 'success' || latestResult.status === 'failure') {
return latestResult;
}
}
// Wait 2 seconds before next poll
await new Promise(resolve => setTimeout(resolve, 2000));
}
throw new Error('Task execution timeout');
}
// Usage
const execution = await TasksLegacy.runAdhocTask({
body: {
taskId: taskId,
version: '1',
async: true,
inputs: { ANCHOR_TARGET_URL: 'https://example.com' }
}
});
const result = await waitForTaskCompletion(taskId);
console.log('Task completed:', result);
import time
# Poll for task completion
def wait_for_task_completion(task_id: str, max_attempts: int = 60):
for _ in range(max_attempts):
results = client.tasks_legacy.list_task_executions(task_id, page="1", limit="1")
latest_result = results.data.results[0] if results.data and results.data.results else None
if latest_result:
if latest_result.status in ("success", "failure"):
return latest_result
# Wait 2 seconds before next poll
time.sleep(2)
raise Exception('Task execution timeout')
# Usage
execution = client.tasks_legacy.run_adhoc_task(
task_id=task_id,
version="1",
async_=True,
inputs={"ANCHOR_TARGET_URL": "https://example.com"}
)
result = wait_for_task_completion(task_id)
print(f"Task completed: {result}")
Was this page helpful?
