🚀 Nano-Queue
A lightweight, cross-platform task manager inspired by Celery for JavaScript environments. Nano-Queue provides asynchronous task execution, worker management, and queue processing for both Node.js and browser environments.
✨ Features
- 🔄 Asynchronous Task Execution - Execute tasks in the background
- 👷 Worker Management - Multiple workers processing different queues
- 🔁 Retry Logic - Automatic retry on task failure
- ⏱️ Delayed Execution - Schedule tasks with countdown delays
- 📊 Queue Monitoring - Track queue status and task results
- 🌐 Cross-Platform - Works in Node.js and browsers
- 📝 Comprehensive Logging - Built-in logger with configurable levels
- 🎯 TypeScript-Ready - Full JSDoc documentation for type hints
📦 Installation
npm install @yaro.page/nano-queue
🚀 Quick Start
Basic Usage
import Queue from '@yaro.page/nano-queue'
const queue = new Queue()
const addTask = queue.task('add')((x, y) => x + y)
await queue.startWorkers(2)
const result = addTask.delay(5, 3)
setTimeout(() => {
if (result.ready()) {
console.log('Result:', result.get())
}
}, 1000)
Advanced Usage
import Queue from '@yaro.page/nano-queue'
const queue = new Queue({ logLevel: 'debug' })
const processDataTask = queue.task('process_data', {
retry: 3,
countdown: 2
})(async (data) => {
await new Promise(resolve => setTimeout(resolve, 1000))
return `Processed: ${data}`
});
await queue.startWorkers(3, ['data_processing', 'default'])
const result = queue.sendTask('process_data', ['important_data'], {}, {
queue: 'data_processing',
countdown: 5,
retry: true
})
console.log(queue.getQueueStatus())
📚 API Reference
Queue
Main class for managing tasks and workers.
Constructor
new Queue(options)
options.logLevel - Logging level ('debug', 'info', 'warn', 'error')
Methods
task(name, options) - Create a task decorator
sendTask(taskName, args, kwargs, options) - Send task to queue
startWorkers(count, queues) - Start worker processes
stopAllWorkers() - Stop all workers
getQueueStatus() - Get queue status information
getResult(taskId) - Get task result by ID
Task
Represents an executable task.
Methods
delay(...args) - Execute task with arguments
applyAsync(args, kwargs, options) - Execute with detailed options
execute(args, kwargs) - Direct task execution
TaskResult
Represents the result of task execution.
Methods
ready() - Check if task is completed
successful() - Check if task succeeded
failed() - Check if task failed
get() - Get task result (throws if failed/not ready)
Worker
Processes tasks from queues.
Methods
start() - Start processing tasks
stop() - Stop the worker
processTask(taskMessage) - Process a single task
🔧 Configuration
Task Options
const task = queue.task('my_task', {
retry: 3,
countdown: 5,
expires: 3600
});
Execution Options
const result = queue.sendTask('task_name', [arg1, arg2], {}, {
queue: 'priority',
countdown: 10,
retry: true,
maxRetries: 5
});
🌐 Browser Usage
Via Bundler
import Queue from '@yaro.page/nano-queue'
import { Queue } from '@yaro.page/nano-queue'
📊 Examples
Retry Logic
const unstableTask = queue.task('unstable', { retry: 3 })(() => {
if (Math.random() < 0.7) {
throw new Error('Random failure')
}
return 'Success!'
})
const result = unstableTask.delay()
Multiple Queues
const urgentTask = queue.task('urgent_task')((data) => {
return `Urgent: ${data}`
})
const normalTask = queue.task('normal_task')((data) => {
return `Normal: ${data}`
})
await queue.startWorkers(2, ['urgent'])
await queue.startWorkers(1, ['normal'])
queue.sendTask('urgent_task', ['critical_data'], {}, { queue: 'urgent' })
queue.sendTask('normal_task', ['regular_data'], {}, { queue: 'normal' })
Delayed Execution???
const scheduledTask = queue.task('scheduled')((message) => {
console.log(`Executed at: ${new Date()}`)
return message
})
const result = scheduledTask.applyAsync(['Hello World'], {}, { countdown: 30 })
🧪 Testing
npm test
npm run test:watch
npm run test:coverage
🔍 Debugging
Enable debug logging:
const queue = new Queue({ logLevel: 'debug' })
queue.setLogLevel('debug')
🤝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Inspired by Celery for Python
- Built for modern JavaScript environments
- Designed for simplicity and performance
📈 Roadmap
Made with ❤️