gatsby-worker
Utility to create worker pools
Last updated 3 years ago by vladar .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install gatsby-worker 
SYNC missed versions from official npm registry.

gatsby-worker

Utility to execute tasks in forked processes. Highly inspired by jest-worker.

Example

File worker.ts:

export async function heavyTask(param: string): Promise<string> {
  // using workers is ideal for CPU intensive tasks
  return await heavyProcessing(param)
}

export async function setupStep(param: string): Promise<void> {
  await heavySetup(param)
}

File parent.ts

import { WorkerPool } from "gatsby-worker"

const workerPool = new WorkerPool<typeof import("./worker")>(
  require.resolve(`./worker`),
  {
    numWorkers: 5,
    env: {
      CUSTOM_ENV_VAR_TO_SET_IN_WORKER: `foo`,
    },
  }
)

// queue a task on all workers
const arrayOfPromises = workerPool.all.setupStep(`bar`)

// queue a task on single worker
const singlePromise = workerPool.single.heavyTask(`baz`)

API

Constructor

// TypeOfWorkerModule allows to type exposed functions ensuring type safety.
// It will convert sync methods to async and discard/disallow usage of exports
// that are not functions. Recommended to use with `<typeof import("path_to_worker_module")>`.
const workerPool = new WorkerPool<TypeOfWorkerModule>(
  // Absolute path to worker module. Recommended to use with `require.resolve`
  workerPath: string,
  // Not required options
  options?: {
    // Number of workers to spawn. Defaults to `1` if not defined.
    numWorkers?: number
    // Additional env vars to set in worker. Worker will inherit env vars of parent process
    // as well as additional `GATSBY_WORKER_ID` env var (starting with "1" for first worker)
    env?: Record<string, string>
  }
)

.single

// Exports of the worker module become available under `.single` property of `WorkerPool` instance.
// Calling those will either start executing immediately if there are any idle workers or queue them
// to be executed once a worker become idle.
const singlePromise = workerPool.single.heavyTask(`baz`)

.all

// Exports of the worker module become available under `.all` property of `WorkerPool` instance.
// Calling those will ensure a function is executed on all workers. Best usage for this is performing
// setup/bootstrap of workers.
const arrayOfPromises = workerPool.all.setupStep(`baz`)

.end

// Used to shutdown `WorkerPool`. If there are any in progress or queued tasks, promises for those will be rejected as they won't be able to complete.
const arrayOfPromises = workerPool.end()

isWorker

// Determine if current context is executed in worker context. Useful for conditional handling depending on context.
import { isWorker } from "gatsby-worker"

if (isWorker) {
  // this is executed in worker context
} else {
  // this is NOT executed in worker context
}

Usage with unit tests

If you are working with source files that need transpilation, you will need to make it possible to load untranspiled modules in child processes. This can be done with @babel/register (or similar depending on your build toolchain). Example setup:

const testWorkerPool = new WorkerPool<WorkerModuleType>(workerModule, {
  numWorkers,
  env: {
    NODE_OPTIONS: `--require ${require.resolve(`./ts-register`)}`,
  },
})

This will execute additional module before allowing adding runtime support for new JavaScript syntax or support for TypeScript. Example ts-register.js:

// spawned process won't use jest config (or other testing framework equivalent) to support TS, so we need to add support ourselves
require(`@babel/register`)({
  extensions: [`.js`, `.ts`],
  configFile: require.resolve(relativePathToYourBabelConfig),
  ignore: [/node_modules/],
})

Current Tags

  • 1.23.0-alpha-9689ff.25                                ...           alpha-9689ff (2 years ago)
  • 0.1.0-alpha-pqr-bootstrap.18664                                ...           alpha-pqr-bootstrap (3 years ago)
  • 0.1.0-alpha-pqr-debug.18666                                ...           alpha-pqr-debug (3 years ago)
  • 0.5.0-alpha-qe-sm.46                                ...           alpha-qe-sm (3 years ago)
  • 0.0.0-next.0                                ...           alpha-ssr (3 years ago)
  • 1.14.0-alpha-transformer-json.26                                ...           alpha-transformer-json (2 years ago)
  • 1.6.0-alpha-ts-jit.48                                ...           alpha-ts-jit (3 years ago)
  • 2.0.0-alpha-v5.d20221012t101120.57                                ...           alpha-v5 (2 years ago)
  • 2.0.0-alpha-drupal-proxyurl.14                                ...           drupal-proxyurl (2 years ago)
  • 1.18.0-alpha-drupal-self-reference.18                                ...           drupal-self-reference (2 years ago)
  • 1.8.0-alpha-image-service.24                                ...           image-service (3 years ago)
  • 2.13.1                                ...           latest (10 months ago)
  • 0.6.0                                ...           latest-v3 (2 years ago)
  • 1.25.0                                ...           latest-v4 (2 years ago)
  • 2.14.0-next.2                                ...           next (10 months ago)
  • 1.23.0-alpha-preview-gh-api.26                                ...           preview-gh-api (2 years ago)
  • 1.15.0-alpha-wp-image-cdn-auth.48                                ...           wp-image-cdn-auth (2 years ago)

167 Versions

  • 2.13.1                                ...           10 months ago
  • 2.14.0-next.2                                ...           10 months ago
  • 2.14.0-next.1                                ...           10 months ago
  • 2.13.0                                ...           a year ago
  • 2.14.0-next.0                                ...           a year ago
  • 2.12.0                                ...           a year ago
  • 2.13.0-next.0                                ...           a year ago
  • 2.12.0-next.1                                ...           a year ago
  • 2.11.0                                ...           a year ago
  • 2.12.0-next.0                                ...           a year ago
  • 2.11.0-next.1                                ...           a year ago
  • 2.10.0                                ...           a year ago
  • 2.11.0-next.0                                ...           a year ago
  • 2.10.0-next.2                                ...           2 years ago
  • 2.10.0-next.1                                ...           2 years ago
  • 2.9.0                                ...           2 years ago
  • 2.10.0-next.0                                ...           2 years ago
  • 2.9.0-next.1                                ...           2 years ago
  • 2.8.0                                ...           2 years ago
  • 2.9.0-next.0                                ...           2 years ago
  • 2.7.0                                ...           2 years ago
  • 2.8.0-next.0                                ...           2 years ago
  • 2.6.0                                ...           2 years ago
  • 2.7.0-next.0                                ...           2 years ago
  • 2.5.0                                ...           2 years ago
  • 2.6.0-next.0                                ...           2 years ago
  • 2.5.0-next.1                                ...           2 years ago
  • 2.4.0                                ...           2 years ago
  • 2.5.0-next.0                                ...           2 years ago
  • 2.4.0-next.1                                ...           2 years ago
  • 2.3.0                                ...           2 years ago
  • 2.4.0-next.0                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 1.25.0                                ...           2 years ago
  • 2.3.0-next.1                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.3.0-next.0                                ...           2 years ago
  • 2.0.0-alpha-drupal-proxyurl.14                                ...           2 years ago
  • 2.1.0                                ...           2 years ago
  • 2.2.0-next.0                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 2.1.0-next.0                                ...           2 years ago
  • 2.0.0-next.1                                ...           2 years ago
  • 1.14.0-alpha-transformer-json.26                                ...           2 years ago
  • 2.0.0-next.0                                ...           2 years ago
  • 2.0.0-alpha-v5.d20221012t101120.57                                ...           2 years ago
  • 2.0.0-alpha-v5.d20221010t183635.51                                ...           2 years ago
  • 2.0.0-alpha-v5.d20221007t081809.41                                ...           2 years ago
  • 2.0.0-alpha-v5.d20221005t111511.35                                ...           2 years ago
  • 2.0.0-alpha-v5.16                                ...           2 years ago
  • 1.24.0                                ...           2 years ago
  • 1.25.0-next.0                                ...           2 years ago
  • 1.24.0-next.1                                ...           2 years ago
  • 2.0.0-alpha-v5.26                                ...           2 years ago
  • 1.23.0                                ...           2 years ago
  • 1.23.0-alpha-preview-gh-api.26                                ...           2 years ago
  • 1.24.0-next.0                                ...           2 years ago
  • 1.23.0-alpha-v5.26                                ...           2 years ago
  • 1.23.0-alpha-9689ff.25                                ...           2 years ago
  • 2.0.0-alpha-v5.25                                ...           2 years ago
  • 2.0.0-alpha-v5.24                                ...           2 years ago
  • 1.22.0                                ...           2 years ago
  • 1.23.0-next.0                                ...           2 years ago
  • 1.21.0                                ...           2 years ago
  • 1.22.0-next.0                                ...           2 years ago
  • 1.20.0                                ...           2 years ago
  • 1.21.0-next.0                                ...           2 years ago
  • 1.18.0-alpha-drupal-self-reference.18                                ...           2 years ago
  • 1.19.0                                ...           2 years ago
  • 1.20.0-next.0                                ...           2 years ago
  • 1.18.0                                ...           2 years ago
  • 1.19.0-next.1                                ...           2 years ago
  • 1.19.0-next.0                                ...           2 years ago
  • 1.18.0-next.1                                ...           2 years ago
  • 1.17.0                                ...           2 years ago
  • 1.18.0-next.0                                ...           2 years ago
  • 1.16.0                                ...           2 years ago
  • 1.17.0-next.0                                ...           2 years ago
  • 1.15.0                                ...           2 years ago
  • 1.15.0-alpha-wp-image-cdn-auth.48                                ...           2 years ago
  • 1.16.0-next.0                                ...           2 years ago
  • 1.14.0                                ...           3 years ago
  • 1.15.0-next.0                                ...           3 years ago
  • 1.14.0-next.2                                ...           3 years ago
  • 1.13.0                                ...           3 years ago
  • 1.14.0-next.1                                ...           3 years ago
  • 1.14.0-next.0                                ...           3 years ago
  • 1.12.1                                ...           3 years ago
  • 1.12.0                                ...           3 years ago
  • 1.13.0-next.0                                ...           3 years ago
  • 1.11.0                                ...           3 years ago
  • 1.12.0-next.0                                ...           3 years ago
  • 1.10.1                                ...           3 years ago
  • 1.11.0-next.1                                ...           3 years ago
  • 1.10.0                                ...           3 years ago
  • 1.11.0-next.0                                ...           3 years ago
  • 1.10.0-next.1                                ...           3 years ago
  • 1.9.0                                ...           3 years ago
  • 1.10.0-next.0                                ...           3 years ago
  • 1.8.0                                ...           3 years ago
  • 1.9.0-next.0                                ...           3 years ago
  • 1.8.0-alpha-image-service.24                                ...           3 years ago
  • 1.8.0-alpha-image-service.25                                ...           3 years ago
  • 1.7.0                                ...           3 years ago
  • 1.7.0-alpha-image-service.13                                ...           3 years ago
  • 1.8.0-next.0                                ...           3 years ago
  • 1.7.0-next.1                                ...           3 years ago
  • 1.6.0                                ...           3 years ago
  • 1.6.0-alpha-ts-jit.48                                ...           3 years ago
  • 1.7.0-next.0                                ...           3 years ago
  • 1.5.0                                ...           3 years ago
  • 1.6.0-next.1                                ...           3 years ago
  • 1.6.0-next.0                                ...           3 years ago
  • 1.5.0-next.1                                ...           3 years ago
  • 1.4.0                                ...           3 years ago
  • 1.5.0-next.0                                ...           3 years ago
  • 1.4.0-next.1                                ...           3 years ago
  • 1.3.0                                ...           3 years ago
  • 1.4.0-next.0                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.3.0-next.0                                ...           3 years ago
  • 1.2.0-next.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.2.0-next.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 1.1.0-next.0                                ...           3 years ago
  • 1.0.0-zz-next.4                                ...           3 years ago
  • 1.0.0-zz-next.3                                ...           3 years ago
  • 1.0.0-zz-next.2                                ...           3 years ago
  • 0.5.0                                ...           3 years ago
  • 1.0.0-zz-next.1                                ...           3 years ago
  • 1.0.0-zz-next.8                                ...           3 years ago
  • 1.0.0-next.1                                ...           3 years ago
  • 1.0.0-next.0                                ...           3 years ago
  • 1.0.0-alpha-9689ff.13                                ...           3 years ago
  • 0.5.0-alpha-qe-sm.46                                ...           3 years ago
  • 1.0.0-alpha-9689ff.36                                ...           3 years ago
  • 1.0.0-alpha-9689ff.34                                ...           3 years ago
  • 1.0.0-alpha-9689ff.24                                ...           3 years ago
  • 1.0.0-alpha-9689ff.22                                ...           3 years ago
  • 1.0.0-alpha-9689ff.17                                ...           3 years ago
  • 1.0.0-alpha-9689ff.8                                ...           3 years ago
  • 0.5.0-next.2                                ...           3 years ago
  • 0.5.0-next.1                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 1.0.0-alpha-9689ff.5                                ...           3 years ago
  • 0.5.0-next.0                                ...           3 years ago
  • 1.0.0-alpha-9689ff.27                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.4.0-next.0                                ...           3 years ago
  • 0.3.0-next.3                                ...           3 years ago
  • 0.3.0-next.2                                ...           3 years ago
  • 0.3.0-next.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.3.0-next.0                                ...           3 years ago
  • 0.2.0-next.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
  • 0.2.0-next.0                                ...           3 years ago
  • 0.1.0-next.3                                ...           3 years ago
  • 0.1.0-next.2                                ...           3 years ago
  • 0.1.0-alpha-pqr-debug.18666                                ...           3 years ago
  • 0.1.0-alpha-pqr-debug.18665                                ...           3 years ago
  • 0.0.0                                ...           3 years ago
  • 0.1.0-alpha-pqr-bootstrap.18664                                ...           3 years ago
  • 0.1.0-next.1                                ...           3 years ago
  • 0.1.0-next.0                                ...           3 years ago
  • 0.0.0-next.0                                ...           3 years ago

© 2010 - cnpmjs.org x YWFE | Home | YWFE