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

@spongex/job-runner

v1.0.0

Published

Run multiple processes simultaneously

Downloads

4

Readme

job-runner

Run multiple processes simultaneously easily with one class!

Install to your existing project using:

npm i @spongex/job-runner

Include ECMAScript:

import { JobRunner } from '@spongex/job-runner'

Include CommonJS:

const { JobRunner } = require('@spongex/job-runner')

Usage

job-runner works by first constructing a new object and passing it a list of commands to be ran, then calling its member function runAllJobs to process.

The JobRunner constructor has the following signature:

JobRunner: (cmds: Array<string>, opts?: Array<ExecOptions>) => void

cmds is a list of the commands to be passed to exec

ExecOptions is the same format as what exec is passed. This can either be a list of options to use wich must be the same length as the commands, or one single option to be applied to each job.


The runAllJobs member function has the following signature:

runAllJobs: async (splicers?: Array<Splicer>, callback?: JobRunnerCallback) => Promise<RunResults>

splicers is an optional array of variables and values to be replaced in the commands. See the examples below for a demonstration of its usage.

The callback function is an optional function that will be called after the results of each command. The function is passed two parameters, an error object created by exec if any, and a RunResults object representing the result information of the command.

This RunResults object has the following format:

  • results: An array of PromiseSettledResult
  • runTime: Total run time for all jobs in milliseconds
  • numSuccess: Count of successful jobs
  • numFailed: Count of failed jobs

For PromiseSettledResult this contains a status property and a property that is either value or reason. value is only present if status is equal to "fulfilled" while reason will be present when status is equal to "rejected"

However, both value and reason will be an object that has the following format:

  • command: The command which was used for the job
  • duration: The duration of the job in milliseconds
  • error: The error thrown by exec if any
  • code: The exit code of the job
  • stdout: The output of the job
  • stderr: The error output of the job

See the documentation on Promise.allSettled for any additional information.

Examples

Using JobRunner with a list of options:

const myJobs = new JobRunner(
  [
    'ls',
    'ls',
    'ls',
    'ls',
    'ls'
  ],
  [
    { cwd: '/home/user/folder_a' },
    { cwd: '/home/user/folder_b' },
    { cwd: '/home/user/folder_c' },
    { cwd: '/home/user/folder_d' },
    { cwd: '/home/user/folder_e' }
  ]
)

const { results, runTime, numSuccess, numFailed } = await myJobs.runAllJobs()

Using JobRunner with a splicer:

const myJobs = new JobRunner(
  [
    'ls $PATH_A',
    'ls $PATH_B',
    'ls $PATH_C',
    'ls $PATH_D',
    'ls $PATH_E'
    ]
  )

const { results, runTime, numSuccess, numFailed } = await myJobs.runAllJobs(
  [
    { var: '$PATH_A', val: '/home/user/folder_a' },
    { var: '$PATH_B', val: '/home/user/folder_b' },
    { var: '$PATH_C', val: '/home/user/folder_c' },
    { var: '$PATH_D', val: '/home/user/folder_d' },
    { var: '$PATH_E', val: '/home/user/folder_e' }
  ]
)

Changelog

1.0.0

  • Initial release