{"id":28684921,"url":"https://github.com/node-modules/cfork","last_synced_at":"2025-06-14T03:07:58.786Z","repository":{"id":19360639,"uuid":"22600468","full_name":"node-modules/cfork","owner":"node-modules","description":"cluster fork and restart easy way","archived":false,"fork":false,"pushed_at":"2024-12-15T11:15:07.000Z","size":88,"stargazers_count":157,"open_issues_count":0,"forks_count":22,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-05-22T00:28:34.421Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-modules.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-08-04T10:34:05.000Z","updated_at":"2025-05-09T06:46:59.000Z","dependencies_parsed_at":"2024-06-02T15:23:58.403Z","dependency_job_id":"c47fc753-afe2-4b03-a72b-79c88a9a5070","html_url":"https://github.com/node-modules/cfork","commit_stats":{"total_commits":59,"total_committers":16,"mean_commits":3.6875,"dds":0.6610169491525424,"last_synced_commit":"28b2b0888e26da211f963f0905ddc529edfc2b7e"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/node-modules/cfork","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fcfork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fcfork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fcfork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fcfork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-modules","download_url":"https://codeload.github.com/node-modules/cfork/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-modules%2Fcfork/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259752078,"owners_count":22905972,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-14T03:07:48.772Z","updated_at":"2025-06-14T03:07:58.773Z","avatar_url":"https://github.com/node-modules.png","language":"TypeScript","readme":"# cfork\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/node-modules/cfork/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/cfork/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![npm download][download-image]][download-url]\n[![Node.js Version](https://img.shields.io/node/v/cfork.svg?style=flat)](https://nodejs.org/en/download/)\n\n[npm-image]: https://img.shields.io/npm/v/cfork.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/cfork\n[codecov-image]: https://codecov.io/gh/node-modules/cfork/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/node-modules/cfork\n[download-image]: https://img.shields.io/npm/dm/cfork.svg?style=flat-square\n[download-url]: https://npmjs.org/package/cfork\n\ncluster fork and restart easy way.\n\n* Easy fork with worker file path\n* Handle worker restart, even it was exit unexpected.\n* Auto error log process `uncaughtException` event\n\n## Install\n\n```bash\nnpm install cfork\n```\n\n## Usage\n\n### ESM and TypeScript\n\n```ts\nimport util from 'node:util';\nimport { cfork } from 'cfork';\n\nconst clusterWorker = cfork({\n  exec: '/your/app/worker.js',\n  // slaves: ['/your/app/slave.js'],\n  // count: require('os').cpus().length,\n}).on('fork', worker =\u003e {\n    console.warn('[%s] [worker:%d] new worker start', Date(), worker.process.pid);\n  })\n  .on('disconnect', worker =\u003e {\n    console.warn('[%s] [master:%s] wroker:%s disconnect, exitedAfterDisconnect: %s, state: %s.',\n      Date(), process.pid, worker.process.pid, worker.exitedAfterDisconnect, worker.state);\n  })\n  .on('exit', (worker, code, signal) =\u003e {\n    const exitCode = worker.process.exitCode;\n    const err = new Error(util.format('worker %s died (code: %s, signal: %s, exitedAfterDisconnect: %s, state: %s)',\n      worker.process.pid, exitCode, signal, worker.exitedAfterDisconnect, worker.state));\n    err.name = 'WorkerDiedError';\n    console.error('[%s] [master:%s] wroker exit: %s', Date(), process.pid, err.stack);\n  })\n  // if you do not listen to this event\n  // cfork will output this message to stderr\n  .on('unexpectedExit', (worker, code, signal) =\u003e {\n    // logger what you want\n  })\n  // emit when reach refork times limit\n  .on('reachReforkLimit', () =\u003e {\n    // do what you want\n  });\n\n// if you do not listen to this event\n// cfork will listen it and output the error message to stderr\nprocess.on('uncaughtException', err =\u003e {\n  // do what you want\n});\n\n// if you want to dynamically disable refork, you can call the setDisableRefork, priority over the refork parameter\ncfork.setDisableRefork(clusterWorker, true);\n```\n\n### CommonJS\n\n```js\nconst { cfork } = require('cfork');\n```\n\n### Options\n\n* **exec** : exec file path\n* **slaves** : slave process config\n* **args** : exec arguments\n* **count** : fork worker nums, default is `os.cpus().length`\n* **refork** : refork when worker disconnect or unexpected exit, default is `true`\n* **limit**: limit refork times within the `duration`, default is `60`\n* **duration**: default is `60000`, one minute (so, the `refork times` \u003c `limit / duration`)\n* **autoCoverage**: auto fork with istanbul when `running_under_istanbul` env set, default is `false`\n* **env**: attach some environment variable key-value pairs to the worker / slave process, default to an empty object.\n* **windowsHide**: Hide the forked processes console window that would normally be created on Windows systems, default to false.\n* **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.\n\n## License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=node-modules/cfork)](https://github.com/node-modules/cfork/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fcfork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-modules%2Fcfork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-modules%2Fcfork/lists"}