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

job-executor

v1.0.1

Published

A simple and lightweight library that runs a series of asynchronous tasks in parallel with adjustable concurrency limits.

Downloads

3

Readme

job-executor

A flexible job executor library that enables you to run multiple async tasks concurrency with adjustable size, while providing options for error and completion handling. It also allows termination of specific jobs or all jobs, and provides a way to wait until all jobs are completed.

Install

# using npm
npm install job-executor
# using yarn
yarn add job-executor

Usage

Import

// in ESM
import { JobExecutor } from 'job-executor';
// in CommonJS
const { JobExecutor } = require('job-executor');

Example

const jobExecutor = new JobExecutor({
  jobTask: async signal => {
    // Perform some long-running task here...
    // You can check the signal.aborted property periodically
    // to see if the job should be aborted.
  },
});

jobExecutor.execute(10).then(() => {
  console.log('10 jobs done!');
});

// Add more jobs to run
jobExecutor.execute(50).then(() => {
  console.log('50 jobs done!');
});

In this example, we create a new instance of the JobExecutor class with a jobTask function that will perform some long-running task asynchronously. We then execute 10 jobs and 50 jobs using the execute method and wait for all jobs to finish before logging a message to the console.

To terminate a job or all jobs, use the terminate method:

// Terminates the first active job
await executor.terminate(1);

// Terminates all active jobs
await executor.terminate();

To wait for all jobs to complete, use the wait method:

await executor.wait();
console.log('All jobs are done!');

API

JobExecutor

The JobExecutor class is the main class of this library and provides the following methods and properties:

constructor(options: JobExecutorOptions)

Creates a new instance of the JobExecutor class with the given options. The available options are:

| Name | Type | Description | | ---------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------- | | jobTask | (signal: AbortSignal) => Promise<T> | The task function that will be executed for each job (required) | | jobError | (error: Error) => void | An optional error callback function that will be called if the jobTask function throws an error | | jobDone | (result: T) => void | An optional callback function that will be called when the jobTask function completes successfully | | allJobDone | () => void | An optional callback function that will be called when all jobs have completed successfully | | |

size: number

A read-only property that returns the number of currently active jobs.

execute(size: number): Promise<void>

Executes the given number of jobs asynchronously using the jobTask function. The method returns a Promise that resolves when all jobs have completed either successfully or failed.

terminate(size?: number): Promise<number>

Aborts the given number of active jobs and waits for them to complete. If no argument is provided, all active jobs will be terminated. It returns the number of jobs that were successfully terminated.

wait(): Promise<void>

Waits for all currently active jobs to complete before resolving. If there are no active jobs, the Promise will resolve immediately.

Testing

This library is well tested. You can test the code as follows:

# using npm
npm test
# using yarn
yarn test

Contribute

If you have anything to contribute, or functionality that you lack - you are more than welcome to participate in this!

License

Feel free to use this library under the conditions of the MIT license.