Background task queue with priority and retry
npm install @bernierllc/task-queue-coreBackground task queue with priority and retry
``bash`
npm install @bernierllc/task-queue
- Priority-based ordering - Tasks processed by priority (LOW, NORMAL, HIGH, URGENT)
- Concurrency control - Limit concurrent task execution
- Retry logic - Automatic retry with configurable delays
- Event-driven - Subscribe to task lifecycle events
- In-memory queue - Fast, lightweight task processing
- TypeScript support - Full type safety
`typescript
import { createTaskQueue, TaskPriority } from '@bernierllc/task-queue';
const queue = createTaskQueue({ concurrency: 3 });
// Add a task
await queue.add({
id: 'task-1',
handler: async () => {
// Do work
return { success: true };
},
maxRetries: 3
}, TaskPriority.HIGH);
// Subscribe to events
queue.on('task.completed', (result) => {
console.log('Task completed:', result.id);
});
// Process tasks
await queue.process();
`
`typescript
import { TaskPriority } from '@bernierllc/task-queue';
// Priority levels (higher number = higher priority)
TaskPriority.LOW = 1
TaskPriority.NORMAL = 5
TaskPriority.HIGH = 10
TaskPriority.URGENT = 20
`
`typescript
const queue = createTaskQueue({ concurrency: 5 });
// Process up to 5 tasks simultaneously
await queue.process();
`
`typescript`
await queue.add({
id: 'task-1',
handler: async () => { / work / },
maxRetries: 3,
retryDelay: 1000 // 1 second between retries
});
`typescript`
queue.pause(); // Stop processing new tasks
queue.resume(); // Resume processing
`typescript`
const stats = queue.getStats();
console.log(stats.total); // Total tasks enqueued
console.log(stats.completed); // Tasks completed
console.log(stats.failed); // Tasks failed
console.log(stats.processing); // Currently processing
Creates a new task queue instance.
Adds a task to the queue and returns a promise that resolves when the task completes.
Processes tasks from the queue up to the concurrency limit.
Pauses task processing.
Resumes task processing.
Clears all tasks from the queue.
Returns queue statistics.
Subscribe to task events: task.completed, task.failed, task.retry.
Uses @bernierllc/logger for comprehensive task lifecycle logging including task enqueueing, processing, completion, failures, and retries.
Can emit task events to NeverHub event bus for distributed monitoring and observability. Task queue can publish events like task.enqueued, task.completed, task.failed, and task.retry to NeverHub when available.
Pattern: Optional service discovery integration - package can emit task lifecycle events to NeverHub for distributed monitoring.
Example Integration:
`typescript``
// If NeverHub is available, emit events
if (typeof detectNeverHub === 'function') {
queue.on('task.completed', (result) => {
neverhub.emit('task-queue.task.completed', result);
});
}
TypeDoc-compatible JSDoc comments are included throughout the source code. All public APIs are documented with examples and type information.
Copyright (c) 2025 Bernier LLC. All rights reserved.