npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

als-queue-manager

v1.0.0

Published

simple and flexible queue manager for handling task execution with customizable intervals, daily limits, and batch actions

Downloads

73

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-manager

Usage

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 limit

Example: 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 is 1000.
  • maxCount (number, optional): The maximum number of actions that can be executed per day. Default is Infinity.
  • maxActions (number, optional): The maximum number of actions that can be executed at once in a single interval. Default is 1.

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: true if the action was successfully added, false if 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');
});