ObjectStackObjectStack

Worker schema — System Protocol reference

Background task processing system with queues, priorities, and retry logic.

Worker System Protocol

Background task processing system with queues, priorities, and retry logic. Provides a robust foundation for async task execution similar to:

  • Sidekiq (Ruby)
  • Celery (Python)
  • Bull/BullMQ (Node.js)
  • AWS SQS/Lambda

Features:

  • Task queues with priorities
  • Task scheduling and retry logic
  • Batch processing
  • Dead letter queues
  • Task monitoring and logging

Basic task

const task: Task = {
  id: 'task-123',
  type: 'send_email',
  payload: { to: 'user@example.com', subject: 'Welcome' },
  queue: 'notifications',
  priority: 5
};

Source: packages/spec/src/system/worker.zod.ts

TypeScript Usage

import { BatchProgressSchema, QueueConfigSchema, TaskSchema, TaskExecutionResultSchema, TaskPriority, TaskRetryPolicySchema, TaskStatus, WorkerStatsSchema } from '@objectstack/spec/system';
import type { BatchProgress, QueueConfig, Task, TaskExecutionResult, TaskPriority, TaskRetryPolicy, TaskStatus, WorkerStats } from '@objectstack/spec/system';

// Validate data
const result = BatchProgressSchema.parse(data);

BatchProgress

Properties

PropertyTypeRequiredDescription
batchIdstring✅Batch job identifier
totalinteger✅Total number of items
processedintegeroptional (default: 0)Items processed
succeededintegeroptional (default: 0)Items succeeded
failedintegeroptional (default: 0)Items failed
percentagenumber✅Progress percentage
statusEnum<'pending' | 'running' | 'completed' | 'failed' | 'cancelled'>✅Batch status
startedAtstringoptionalWhen batch started
completedAtstringoptionalWhen batch completed

QueueConfig

Properties

PropertyTypeRequiredDescription
namestring✅Queue name (snake_case)
concurrencyintegeroptional (default: 5)Max concurrent task executions
rateLimit{ max: integer; durationMs: integer }optionalRate limit configuration
defaultRetryPolicy{ maxRetries?: integer; backoffStrategy?: Enum<'fixed' | 'linear' | 'exponential'>; initialDelayMs?: integer; maxDelayMs?: integer; … }optionalDefault retry policy for tasks
deadLetterQueuestringoptionalDead letter queue name
priorityintegeroptional (default: 0)Queue priority (lower = higher priority)
autoScale{ enabled?: boolean; minWorkers?: integer; maxWorkers?: integer; scaleUpThreshold?: integer; … }optionalAuto-scaling configuration

Nested Shape: QueueConfig.rateLimit

PropertyTypeRequiredDescription
maxinteger✅Maximum tasks per duration
durationMsinteger✅Duration in milliseconds
durationneveroptional[REMOVED] QueueConfig.rateLimit.duration was renamed to durationMs in @objectstack/spec 17 — the unit of a duration-shaped number lives in the key name, not only in the describe prose, and TaskResult.durationMs on this same file already spelled it that way. Rename the key to durationMs; the value (milliseconds) is unchanged.

Nested Shape: QueueConfig.defaultRetryPolicy

PropertyTypeRequiredDescription
maxRetriesintegeroptional (default: 3)Maximum retry attempts
backoffStrategyEnum<'fixed' | 'linear' | 'exponential'>optional (default: "exponential")Backoff strategy between retries
initialDelayMsintegeroptional (default: 1000)Initial retry delay in milliseconds
maxDelayMsintegeroptional (default: 60000)Maximum retry delay in milliseconds
backoffMultipliernumberoptional (default: 2)Multiplier for exponential backoff

Nested Shape: QueueConfig.autoScale

PropertyTypeRequiredDescription
enabledbooleanoptional (default: false)Enable auto-scaling
minWorkersintegeroptional (default: 1)Minimum workers
maxWorkersintegeroptional (default: 10)Maximum workers
scaleUpThresholdintegeroptional (default: 100)Queue size to scale up
scaleDownThresholdintegeroptional (default: 10)Queue size to scale down

Task

Properties

PropertyTypeRequiredDescription
idstring✅Unique task identifier
typestring✅Task type (snake_case)
payloadany✅Task payload data
queuestringoptional (default: "default")Queue name
priorityEnum<'critical' | 'high' | 'normal' | 'low' | 'background'>optional (default: "normal")Task priority level
retryPolicy{ maxRetries?: integer; backoffStrategy?: Enum<'fixed' | 'linear' | 'exponential'>; initialDelayMs?: integer; maxDelayMs?: integer; … }optionalRetry policy configuration
timeoutMsintegeroptionalTask timeout in milliseconds
scheduledAtstringoptionalISO 8601 datetime to execute task
attemptsintegeroptional (default: 0)Number of execution attempts
statusEnum<'pending' | 'queued' | 'processing' | 'completed' | 'failed' | 'cancelled' | 'timeout' | 'dead'>optional (default: "pending")Current task status
metadata{ createdAt?: string; updatedAt?: string; createdBy?: string; tags?: string[] }optionalTask metadata

Nested Shape: Task.retryPolicy

PropertyTypeRequiredDescription
maxRetriesintegeroptional (default: 3)Maximum retry attempts
backoffStrategyEnum<'fixed' | 'linear' | 'exponential'>optional (default: "exponential")Backoff strategy between retries
initialDelayMsintegeroptional (default: 1000)Initial retry delay in milliseconds
maxDelayMsintegeroptional (default: 60000)Maximum retry delay in milliseconds
backoffMultipliernumberoptional (default: 2)Multiplier for exponential backoff

Nested Shape: Task.metadata

PropertyTypeRequiredDescription
createdAtstringoptionalWhen task was created
updatedAtstringoptionalLast update time
createdBystringoptionalUser who created task
tagsstring[]optionalTask tags for filtering

TaskExecutionResult

Properties

PropertyTypeRequiredDescription
taskIdstring✅Task identifier
statusEnum<'pending' | 'queued' | 'processing' | 'completed' | 'failed' | 'cancelled' | 'timeout' | 'dead'>✅Execution status
resultanyoptionalExecution result data
error{ message: string; stack?: string; code?: string }optionalError details if failed
durationMsintegeroptionalExecution duration in milliseconds
startedAtstring✅When execution started
completedAtstringoptionalWhen execution completed
attemptinteger✅Attempt number (1-indexed)
willRetryboolean✅Whether task will be retried

Nested Shape: TaskExecutionResult.error

PropertyTypeRequiredDescription
messagestring✅Error message
stackstringoptionalError stack trace
codestringoptionalError code

TaskPriority

Allowed Values

  • critical
  • high
  • normal
  • low
  • background

TaskRetryPolicy

Properties

PropertyTypeRequiredDescription
maxRetriesintegeroptional (default: 3)Maximum retry attempts
backoffStrategyEnum<'fixed' | 'linear' | 'exponential'>optional (default: "exponential")Backoff strategy between retries
initialDelayMsintegeroptional (default: 1000)Initial retry delay in milliseconds
maxDelayMsintegeroptional (default: 60000)Maximum retry delay in milliseconds
backoffMultipliernumberoptional (default: 2)Multiplier for exponential backoff

TaskStatus

Allowed Values

  • pending
  • queued
  • processing
  • completed
  • failed
  • cancelled
  • timeout
  • dead

WorkerStats

Properties

PropertyTypeRequiredDescription
workerNamestring✅Worker name
totalProcessedinteger✅Total tasks processed
succeededinteger✅Successful tasks
failedinteger✅Failed tasks
activeinteger✅Currently active tasks
avgExecutionMsnumberoptionalAverage execution time in milliseconds
uptimeMsinteger✅Worker uptime in milliseconds
queuesRecord<string, { pending: integer; active: integer; completed: integer; failed: integer }>optionalPer-queue statistics

Nested Shape: WorkerStats.queues[string]

PropertyTypeRequiredDescription
pendinginteger✅Pending tasks
activeinteger✅Active tasks
completedinteger✅Completed tasks
failedinteger✅Failed tasks

On this page