包详细信息

cfork

node-modules99kMIT2.0.0

cluster fork and restart easy way

cluster, cluster-fork, cfork, restart

自述文件

cfork

NPM version Node.js CI Test coverage npm download Node.js Version

cluster fork and restart easy way.

  • Easy fork with worker file path
  • Handle worker restart, even it was exit unexpected.
  • Auto error log process uncaughtException event

Install

npm install cfork

Usage

ESM and TypeScript

import util from 'node:util';
import { cfork } from 'cfork';

const clusterWorker = cfork({
  exec: '/your/app/worker.js',
  // slaves: ['/your/app/slave.js'],
  // count: require('os').cpus().length,
}).on('fork', worker => {
    console.warn('[%s] [worker:%d] new worker start', Date(), worker.process.pid);
  })
  .on('disconnect', worker => {
    console.warn('[%s] [master:%s] wroker:%s disconnect, exitedAfterDisconnect: %s, state: %s.',
      Date(), process.pid, worker.process.pid, worker.exitedAfterDisconnect, worker.state);
  })
  .on('exit', (worker, code, signal) => {
    const exitCode = worker.process.exitCode;
    const err = new Error(util.format('worker %s died (code: %s, signal: %s, exitedAfterDisconnect: %s, state: %s)',
      worker.process.pid, exitCode, signal, worker.exitedAfterDisconnect, worker.state));
    err.name = 'WorkerDiedError';
    console.error('[%s] [master:%s] wroker exit: %s', Date(), process.pid, err.stack);
  })
  // if you do not listen to this event
  // cfork will output this message to stderr
  .on('unexpectedExit', (worker, code, signal) => {
    // logger what you want
  })
  // emit when reach refork times limit
  .on('reachReforkLimit', () => {
    // do what you want
  });

// if you do not listen to this event
// cfork will listen it and output the error message to stderr
process.on('uncaughtException', err => {
  // do what you want
});

// if you want to dynamically disable refork, you can call the setDisableRefork, priority over the refork parameter
cfork.setDisableRefork(clusterWorker, true);

CommonJS

const { cfork } = require('cfork');

Options

  • exec : exec file path
  • slaves : slave process config
  • args : exec arguments
  • count : fork worker nums, default is os.cpus().length
  • refork : refork when worker disconnect or unexpected exit, default is true
  • limit: limit refork times within the duration, default is 60
  • duration: default is 60000, one minute (so, the refork times < limit / duration)
  • autoCoverage: auto fork with istanbul when running_under_istanbul env set, default is false
  • env: attach some environment variable key-value pairs to the worker / slave process, default to an empty object.
  • windowsHide: Hide the forked processes console window that would normally be created on Windows systems, default to false.
  • serialization: Specify the kind of serialization used for sending messages between processes. Possible values are 'json' and 'advanced'. See Advanced serialization for child_process for more details. Default: false.

License

MIT

Contributors

Contributors

Made with contributors-img.

更新日志

Changelog

2.0.0 (2024-12-15)

⚠ BREAKING CHANGES

  • drop Node.js < 18.19.0 support

part of https://github.com/eggjs/egg/issues/3644

https://github.com/eggjs/egg/issues/5257

Summary by CodeRabbit

Release Notes

  • New Features
  • Introduced a new GitHub Actions workflow for automated package publishing.
  • Added TypeScript configuration to enhance type safety and modern JavaScript compatibility.

    • New tests for child process management functionality.
  • Updates

    • Updated ESLint configuration to utilize external presets.
    • Modified GitHub Actions CI to specify a more precise Node.js version.
  • Enhanced README with installation and usage examples for ESM and TypeScript.
  • Updated package.json to reflect new module structure and dependencies.
  • Improved .gitignore to exclude additional temporary files and directories.

  • Bug Fixes

  • Improved .gitignore to exclude additional temporary files and directories.

  • Removals

  • Removed outdated JavaScript files and configurations in favor of CommonJS and TypeScript standards.

Features

1.11.0 (2024-05-24)

Features

1.10.0 (2024-02-23)

Features

  • set CFORK_WORKER_INDEX env let worker know it's fork index (#116) (f69992c)

1.9.0 / 2023-05-06

features

1.8.0 / 2019-06-13

features

others

1.7.1 / 2017-11-07

fixes

1.7.0 / 2017-09-25

features

1.6.1 / 2017-06-11

  • fix: try to use exitedAfterDisconnect first (#109)
  • chore: update badge

1.6.0 / 2016-11-18

  • feat: Add environment variables support for workers (#100)
  • chore: fix eslint2 removed rules and spelling error (#90)

1.5.1 / 2016-06-23

  • fix: add more debug log (#74)

1.5.0 / 2016-05-06

  • feat: add execArgv support
  • test: add 5, 4 node

1.4.0 / 2015-11-04

  • test(slave): add slave die and refork test case
  • feat: add slave process support
  • chore: fix npm run autod

1.3.1 / 2015-08-31

  • fix: add options.autoCoverage to enable istanbul

1.3.0 / 2015-08-30

  • feat: support code coverage with istanbul

1.2.4 / 2015-06-03

  • fix: err maybe undefined

1.2.3 / 2015-05-09

  • test: Add iojs and node v0.12 into travis config
  • fix: when worker has terminated before disconnect, don't fork (@JacksonTian)

1.2.2 / 2014-11-06

  • refactor: ignore undefined
  • pick of meaningful options to setupMaster

1.2.1 / 2014-11-01

  • feat: support args and slient

1.2.0 / 2014-09-27

  • add limit for refork times in certain duration (@coderhaoxin)

1.1.1 / 2014-09-17

  • Fix unexpectedExit event listeners length (@JacksonTian)

1.1.0 / 2014-08-08

  • defer add listenners, add options.refork, optional unexpectedExit event (@dead-horse)

1.0.1 / 2014-08-04

  • improve disconnect and unexpected exit log

1.0.0 / 2014-08-04

  • work with graceful
  • first release