Task Workers

Background task processing infrastructure for AI agents. Process tasks asynchronously with retries, timeouts, and event handling.

Creating Workers

typescript
import { createWorker } from '@sekuire/sdk'; const worker = createWorker({ name: 'data-processor', concurrency: 5, timeout: 60_000, // 1 minute per task retries: 3, handler: async (task, context) => { const { data_url } = task.payload; // Process the task const result = await processData(data_url); return { processed: true, result }; } }); await worker.start();

TaskContext

The context provides access to worker utilities:

typescript
const worker = createWorker({ handler: async (task, context) => { // Access task metadata console.log(context.taskId); console.log(context.attempt); // Current retry attempt // Report progress await context.progress(50, 'Halfway done'); // Log with correlation context.logger.info('Processing', { step: 1 }); // Extend timeout if needed await context.extendTimeout(30_000); return result; } });

Task Events

typescript
worker.on('task:started', (event) => { console.log('Task started:', event.taskId); }); worker.on('task:completed', (event) => { console.log('Task completed:', event.taskId, event.result); }); worker.on('task:failed', (event) => { console.log('Task failed:', event.taskId, event.error); }); worker.on('task:retrying', (event) => { console.log('Retrying task:', event.taskId, 'attempt', event.attempt); });

Enqueueing Tasks

typescript
import { TaskQueue } from '@sekuire/sdk'; const queue = new TaskQueue({ worker }); // Enqueue a task const taskId = await queue.enqueue({ payload: { data_url: 'https://example.com/data.csv' }, priority: 'high', delay: 5000 // Start after 5 seconds }); // Wait for result const result = await queue.waitForResult(taskId);

Next Steps