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

concurrency-handler

v2.0.0

Published

This Node.js and JavaScript library provides efficient queue processing with optimized handling of synchronous and asynchronous tasks.

Downloads

6

Readme

Task Runner

A simple task queue handler that runs synchronous and asynchronous tasks, optimized for system resource usage. This package allows you to queue tasks and run them concurrently while monitoring CPU and memory usage to ensure system performance remains optimal.

Features

  • Task Queue Management: Add tasks to a queue and manage their execution.
  • Concurrency Control: Set a concurrency limit to run multiple tasks in parallel.
  • System Monitoring: Monitors CPU and memory usage to ensure that the system is not overloaded.
  • Resource Management: Prevent tasks from being executed when system resource utilization exceeds a predefined threshold.

Installation

To install the concurrency-handler package, use npm:

npm install concurrency-handler

Usage

Importing the Module

You can import both the QueueHandler and SystemMonitor classes from the package.

const { QueueHandler, SystemMonitor } = require('concurrency-handler');

Example: QueueHandler

Here’s a basic example of how to use the QueueHandler to manage tasks.

const { QueueHandler } = require('concurrency-handler');

// Create a queue handler with specific concurrency and system resource limits
const queue = new QueueHandler({
    concurrency: 4, // Run up to 4 tasks in parallel
    maxCpuUtilization: 70, // Prevent new tasks if CPU usage exceeds 70%
    maxMemoryUtilization: 75 // Prevent new tasks if memory usage exceeds 75%
});

// Example tasks (can be async or sync functions)
const task1 = async () => {
    console.log('Task 1 is running');
    return new Promise((resolve) => setTimeout(resolve, 1000));
};

const task2 = async () => {
    console.log('Task 2 is running');
    return new Promise((resolve) => setTimeout(resolve, 1000));
};

// Add tasks to the queue
queue.addTasks([task1, task2]);

Example: SystemMonitor

You can also use the SystemMonitor class independently to monitor system resources.

const { SystemMonitor } = require('concurrency-handler');

// Create a system monitor instance
const monitor = new SystemMonitor({
    maxCpuUtilization: 80, // Maximum allowed CPU usage (percentage)
    maxMemoryUtilization: 80 // Maximum allowed memory usage (percentage)
});

// Check CPU and Memory usage
console.log('CPU Usage:', monitor.getCpuUsage());
console.log('Memory Usage:', monitor.getMemoryUsage());

// Use the canProceed method to check if tasks can run
const canRunTasks = await monitor.canProceed([], 4);
if (canRunTasks) {
    console.log('System resources are sufficient to proceed with tasks.');
}

API Reference

QueueHandler

Constructor

new QueueHandler({ concurrency, maxCpuUtilization, maxMemoryUtilization })
  • concurrency (optional): The maximum number of tasks that can run in parallel. If not specified, defaults to the number of available CPU cores..
  • maxCpuUtilization(optional): The maximum CPU usage percentage allowed before pausing task execution. If not specified, defaults to 80%.
  • maxMemoryUtilization(optional): The maximum memory usage percentage allowed before pausing task execution. If not specified, defaults to 80%.

Methods

  • addTasks(tasks: Array<Function>): Adds tasks to the queue and starts processing them. The tasks array should contain functions that return a Promise or synchronous functions.
  • processQueue(): Starts processing the tasks in the queue. It will continue to process tasks until the queue is empty and all active tasks are complete.
  • runTask(task: Function): Runs a single task. This method is called internally by processQueue to execute tasks.

SystemMonitor

Constructor

new SystemMonitor({ maxCpuUtilization, maxMemoryUtilization })

Methods

  • getCpuUsage(): Returns the current CPU usage as a percentage.

  • getMemoryUsage(): Returns the current memory usage as a percentage.

  • canProceed(activeTasks: Array<Promise>, concurrency: number): Promise<boolean>: Returns a boolean indicating whether system resources allow further task execution.