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

@lzptec/job-system

v0.5.0-0

Published

A Job System for node

Downloads

4

Readme

@lzptec/job-system

A library to implement a Job System in nodejs using worker_threads.

Installation

npm

npm i @lzptec/job-system

pnpm

pnpm i @lzptec/job-system

Usage

The following examples uses typescript

⚠ Version 1.0.0 will be the first Stable Release and the Usage will change drastically.

Basic

import { ThreadPool } from '@lzptec/job-system';

const threadPool = new ThreadPool();

const job = ({ a, b }) => a * b;

// When we schedule a Job the return will be a JobHandle<T>.
const jobHandle = await threadPool.schedule(job, { a: 2, b: 5 });
// OR
const jobHandle = await threadPool.schedule(({ a, b }) => a * b, { a: 2, b: 5 });

// If we want to get the result, we need to call complete()
const result = await jobHandle.complete();
console.log(result); // 10

Advanced

import { ThreadPool } from '@lzptec/job-system';

const threadPool = new ThreadPool();

// When we schedule a Job the return will be a JobHandle<T>.
const jobHandle = threadPool.schedule(({ a, b }) => a * b, { a: 2, b: 5 });

// If we want to get the result, we need to call complete()
const result = await jobHandle.complete();
console.log(result); // 10

// We can use the JobHandle<T> as Dependency to another job, this will ensure that a job run only after the dependency job.
const job1Handle = threadPool.schedule((data) => console.log(`Hello from Job n${data}`), 1);
const job2Handle = threadPool.schedule((data) => console.log(`Hello from Job n${data}`), 2, [job1Handle]);

await job2Handle.complete();
// Console
// -> Hello from Job n1
// -> Hello from Job n2

API

ThreadPool(settings?)

Returns a new ThreadPool.

settings.maxWorkers

Optional Type: number Default: 0

Defines the maximum number of workers the Job System can instantiate. If it is 0 the number of workers will be automatically defined using the following logic:

const cpuSize = os.cpus().length;
const maxWorkers = Math.max(1, cpuSize >= 6 ? (cpuSize / 2) : cpuSize - 1);

Important: If a number smaller than 0 is set, an error will occur!

settings.minWorkers

Optional Type: number Default: 0

Defines the minimum number of workers the Job System will instantiate on startup.

Important: If the value is bigger than maxWorkers it will use maxWorkers instead.

settings.idleTimeout

Optional Type: number Default: 0

Defines the maximum idle time of a worker inside the pool, after this time the worker will be terminated. The timer resets if a new work is schedule to that worker.

Important: If the value is set to 0 the Worker stays alive until the shutdown() method is called.

settings.useMainThread

Optional Type: boolean Default: false

Use the main thread if no worker is available.

setSettings(settings)

Set new settings on this pool.

settings

Required Type: ThreadPoolSettings

New settings.

getSettings()

Return: ThreadPoolSettings

Return the current settings of this pool.

schedule(job, data?, dependencies?, transferList?)

Returns: JobHandle

Add a job to the execution queue

job

Required Type: Function

The function that must be executed in one of the threads.

data

Optional Type: SerializableValue | undefined Default: undefined

The data that will be sent to the Job.

dependencies

Optional Type: JobHandle[] | undefined Default: undefined

A list of depedencies, use it to ensure that a job executes after all the dependencies has completed execution.

transferList

Optional Type: Transferable[] | undefined Default: undefined

A list of transferable objects like ArrayBuffers to be transferred to the receiving worker thread.

shutdown()

Shutdown the Thread Pool, it will wait for all jobs to complete.

Important: If the schedule method is called after shutdown, an error will occur!

JobHandle extends EventEmitter

JobHandle.

JobHandle.complete()

Returns a Promise that resolves when the job completes.

JobHandle.state

Returns the current job state.

Events

success | error | complete

Notes

Documentation will be updated over time.