als-queue-manager
v1.0.0
Published
simple and flexible queue manager for handling task execution with customizable intervals, daily limits, and batch actions
Readme
als-queue-manager
als-queue-manager is a simple and flexible queue manager for handling task execution with customizable intervals, daily limits, and batch actions. It allows you to manage queues efficiently, ensuring you have control over when and how many actions are executed.
Installation
To install the package via NPM, use the following command:
npm install als-queue-managerUsage
Basic Example
Below is a basic example of how to use QueueManager to execute actions at specified intervals:
const QueueManager = require('als-queue-manager');
// Create a queue manager with a 100ms interval between actions
const qm = new QueueManager({ interval: 100 });
qm.add(() => console.log('Action 1 executed'));
qm.add(() => console.log('Action 2 executed'));Example with Daily Limit and Max Actions per Interval
This example demonstrates the use of a daily limit (maxCount) and setting a limit on the number of actions that can be executed in a single interval (maxActions):
const QueueManager = require('als-queue-manager');
let actionCount = 0;
// Create a queue manager with a 100ms interval, 2 actions per day limit, and 2 actions per interval
const qm = new QueueManager({ interval: 100, maxCount: 2, maxActions: 2 });
qm.add(() => { actionCount++; console.log('Action 1 executed'); });
qm.add(() => { actionCount++; console.log('Action 2 executed'); });
qm.add(() => { actionCount++; console.log('Action 3 executed'); }); // This action will not execute due to daily limitExample: Resetting Daily Limit
This example shows how the queue manager resets the daily action limit once a new day starts.
const QueueManager = require('als-queue-manager');
let actionCount = 0;
// Create a queue manager with a daily limit of 1 action
const qm = new QueueManager({ interval: 100, maxCount: 1 });
qm.add(() => { actionCount++; console.log('Action 1 executed'); });
// Simulate the next day
qm.nextDay = Date.now() - 1000 * 60 * 60 * 24;
qm.add(() => { actionCount++; console.log('Action 2 executed after day reset'); });API
QueueManager(options)
Creates a new instance of the QueueManager.
Options:
interval(number, optional): The time in milliseconds between each action execution. Default is1000.maxCount(number, optional): The maximum number of actions that can be executed per day. Default isInfinity.maxActions(number, optional): The maximum number of actions that can be executed at once in a single interval. Default is1.
Methods
add(action: Function): boolean
Adds a new action to the queue. If the action already exists in the queue, it will not be added again.
- Returns:
trueif the action was successfully added,falseif it already exists in the queue.
stop(): void
Stops the queue manager from executing any further actions. This clears any active timeouts.
Example:
const qm = new QueueManager({ interval: 200 });
qm.add(() => console.log('First action'));
qm.add(() => console.log('Second action'));
// Stop the queue manager
qm.stop();Internal Behavior
Daily Reset
The QueueManager automatically resets the action counter at midnight (the beginning of the next day). This ensures that actions can be performed again the next day even if the maxCount was reached the previous day.
Action Limiting
The QueueManager prevents the same action from being added multiple times and supports batching multiple actions into a single interval through the maxActions option.
Testing
To run tests for the package, you can use the built-in node:test and node:assert modules. Here's an example of a basic test:
const { test } = require('node:test');
const assert = require('node:assert');
const QueueManager = require('./queue');
test('QueueManager should add and run actions correctly', async () => {
const qm = new QueueManager({ interval: 100 });
let actionExecuted = false;
qm.add(() => { actionExecuted = true });
await delay(200);
assert.strictEqual(actionExecuted, true, 'Action should be executed');
});