The Hercules job system is a powerful background task processing system designed to handle asynchronous tasks with features like prioritization, throttling, and scheduling. This document outlines the design for a Node.js/TypeScript implementation that can be open-sourced.
interface Task {
id: string;
name: string;
moduleName: string;
functionName: string;
args: any[];
kwargs: Record<string, any>;
state: TaskState;
priority: Priority;
runAt: Date;
createdAt: Date;
updatedAt: Date;
completedAt?: Date;
throttleKey?: string;
uniqueKey?: string;
retries: number;
maxRetries: number;
executionNamespace: string;
timeoutLimit: number;
isRecurring: boolean;
exception?: string;
exceptionTrace?: string;
tags: Record<string, string>;
criticality: Criticality;
}interface Throttle {
key: string;
concurrent: number;
executionNamespace: string;
}interface Callback {
id: string;
moduleName: string;
functionName: string;
args: any[];
kwargs: Record<string, any>;
}enum TaskState {
WAITING = 'W',
READY = 'R',
EXECUTING = 'X',
COMPLETE = 'C',
FAILED = 'F',
THROTTLED = 'T'
}enum Priority {
VERY_LOW_PRIORITY = -10000,
NICE_TO_HAVE = -1000,
DEFAULT = 0,
HIGH_PRIORITY = 100,
ALMOST_LIVE = 900,
LIVE = 1000
}enum Criticality {
CRITICAL = 'CRITICAL',
HIGH = 'HIGH',
MEDIUM = 'MEDIUM',
LOW = 'LOW',
INFO = 'INFO'
}Responsible for creating, updating, and retrieving tasks.
interface TaskService {
createTask(task: Partial<Task>): Promise<Task>;
updateTask(id: string, updates: Partial<Task>): Promise<Task>;
getTask(id: string): Promise<Task | null>;
getTasks(filter: Partial<Task>, limit?: number, sort?: Record<string, 1 | -1>): Promise<Task[]>;
deleteTask(id: string): Promise<boolean>;
}Executes tasks from the queue.
interface RunnerService {
start(): Promise<void>;
stop(): Promise<void>;
runOne(): Promise<boolean>;
popTask(): Promise<Task | null>;
executeTask(task: Task): Promise<any>;
}Schedules recurring tasks.
interface SchedulerService {
start(): Promise<void>;
stop(): Promise<void>;
scheduleTask(task: Task, schedule: Schedule): Promise<Task>;
unscheduleTask(taskId: string): Promise<boolean>;
}Manages task state transitions.
interface TaskControllerService {
start(): Promise<void>;
stop(): Promise<void>;
moveWaitingToReady(): Promise<number>;
moveThrottledToWaiting(): Promise<number>;
moveStuckExecutingToWaiting(): Promise<number>;
}Controls concurrency of task execution.
interface ThrottleService {
shouldThrottle(task: Task): Promise<boolean>;
throttleTask(task: Task): Promise<Task>;
unthrottleTask(task: Task): Promise<Task>;
getThrottleKey(task: Task): string;
setThrottleLimit(key: string, concurrent: number): Promise<void>;
getThrottleLimit(key: string): Promise<number>;
}function herculesTask(options?: {
priority?: Priority;
timeout?: number;
maxRetries?: number;
throttle?: { concurrent: number, throttleKey?: string };
uniqueKey?: string | ((args: any[]) => string);
executionNamespace?: string;
criticality?: Criticality;
completedCallback?: Function;
failedCallback?: Function;
}): MethodDecorator;function recurringTask(options: {
schedule: {
cron?: string;
daily?: string[];
hourly?: string[];
};
uniqueKey: string;
priority?: Priority;
timeout?: number;
maxRetries?: number;
executionNamespace?: string;
criticality?: Criticality;
}): MethodDecorator;- PostgreSQL for task storage
- Redis for throttling and distributed locking
- Redis-based queue for task prioritization
- SQS-like interface for task distribution
- Multiple worker processes/threads for task execution
- Graceful shutdown and restart capabilities
- REST API for task management
- WebSocket for real-time task status updates
- Implement Task, Throttle, and Callback models
- Set up PostgreSQL connection and repositories
- Implement basic CRUD operations
- Implement Redis-based queue
- Implement worker process for task execution
- Implement basic throttling mechanism
- Implement scheduler for recurring tasks
- Implement task controller for state transitions
- Implement heartbeat mechanism
- Implement REST API for task management
- Implement WebSocket for real-time updates
- Implement monitoring and metrics
- Implement task decorators
- Implement client SDK for easy integration
- Write documentation and examples