11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Cluster | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/cluster.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <style>@media(max-width:542px){.with-40-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-cluster"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster active">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="cluster" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 1041cb0ef41Sopenharmony_ci <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#cluster">Cluster</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#how-it-works">How it works</a></li> 1261cb0ef41Sopenharmony_ci<li><a href="#class-worker">Class: <code>Worker</code></a> 1271cb0ef41Sopenharmony_ci<ul> 1281cb0ef41Sopenharmony_ci<li><a href="#event-disconnect">Event: <code>'disconnect'</code></a></li> 1291cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li> 1301cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li> 1311cb0ef41Sopenharmony_ci<li><a href="#event-listening">Event: <code>'listening'</code></a></li> 1321cb0ef41Sopenharmony_ci<li><a href="#event-message">Event: <code>'message'</code></a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#event-online">Event: <code>'online'</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#workerdisconnect"><code>worker.disconnect()</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#workerexitedafterdisconnect"><code>worker.exitedAfterDisconnect</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#workerid"><code>worker.id</code></a></li> 1371cb0ef41Sopenharmony_ci<li><a href="#workerisconnected"><code>worker.isConnected()</code></a></li> 1381cb0ef41Sopenharmony_ci<li><a href="#workerisdead"><code>worker.isDead()</code></a></li> 1391cb0ef41Sopenharmony_ci<li><a href="#workerkillsignal"><code>worker.kill([signal])</code></a></li> 1401cb0ef41Sopenharmony_ci<li><a href="#workerprocess"><code>worker.process</code></a></li> 1411cb0ef41Sopenharmony_ci<li><a href="#workersendmessage-sendhandle-options-callback"><code>worker.send(message[, sendHandle[, options]][, callback])</code></a></li> 1421cb0ef41Sopenharmony_ci</ul> 1431cb0ef41Sopenharmony_ci</li> 1441cb0ef41Sopenharmony_ci<li><a href="#event-disconnect_1">Event: <code>'disconnect'</code></a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#event-exit_1">Event: <code>'exit'</code></a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#event-fork">Event: <code>'fork'</code></a></li> 1471cb0ef41Sopenharmony_ci<li><a href="#event-listening_1">Event: <code>'listening'</code></a></li> 1481cb0ef41Sopenharmony_ci<li><a href="#event-message_1">Event: <code>'message'</code></a></li> 1491cb0ef41Sopenharmony_ci<li><a href="#event-online_1">Event: <code>'online'</code></a></li> 1501cb0ef41Sopenharmony_ci<li><a href="#event-setup">Event: <code>'setup'</code></a></li> 1511cb0ef41Sopenharmony_ci<li><a href="#clusterdisconnectcallback"><code>cluster.disconnect([callback])</code></a></li> 1521cb0ef41Sopenharmony_ci<li><a href="#clusterforkenv"><code>cluster.fork([env])</code></a></li> 1531cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#clusterismaster"><code>cluster.isMaster</code></a></span></li> 1541cb0ef41Sopenharmony_ci<li><a href="#clusterisprimary"><code>cluster.isPrimary</code></a></li> 1551cb0ef41Sopenharmony_ci<li><a href="#clusterisworker"><code>cluster.isWorker</code></a></li> 1561cb0ef41Sopenharmony_ci<li><a href="#clusterschedulingpolicy"><code>cluster.schedulingPolicy</code></a></li> 1571cb0ef41Sopenharmony_ci<li><a href="#clustersettings"><code>cluster.settings</code></a></li> 1581cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#clustersetupmastersettings"><code>cluster.setupMaster([settings])</code></a></span></li> 1591cb0ef41Sopenharmony_ci<li><a href="#clustersetupprimarysettings"><code>cluster.setupPrimary([settings])</code></a></li> 1601cb0ef41Sopenharmony_ci<li><a href="#clusterworker"><code>cluster.worker</code></a></li> 1611cb0ef41Sopenharmony_ci<li><a href="#clusterworkers"><code>cluster.workers</code></a></li> 1621cb0ef41Sopenharmony_ci</ul> 1631cb0ef41Sopenharmony_ci</li> 1641cb0ef41Sopenharmony_ci</ul></div></div> 1651cb0ef41Sopenharmony_ci </li> 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci <li class="picker-header"> 1691cb0ef41Sopenharmony_ci <a href="#"> 1701cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1711cb0ef41Sopenharmony_ci Index 1721cb0ef41Sopenharmony_ci </a> 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci <div class="picker"><ul> 1751cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 1761cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci <li> 1791cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 1801cb0ef41Sopenharmony_ci </li> 1811cb0ef41Sopenharmony_ci </ul> 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_ci<hr class="line"> 1841cb0ef41Sopenharmony_ci<ul> 1851cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 1861cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 1871cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 1881cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 1891cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 1901cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 1911cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 1921cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 1931cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster active">Cluster</a></li> 1941cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 1951cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 1961cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 1971cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 1981cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 1991cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2001cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2011cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2021cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2031cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2041cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2051cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2061cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2071cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2081cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2091cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2101cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2111cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2121cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2131cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2141cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2151cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2161cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2171cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2181cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2191cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2201cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2211cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2221cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2231cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2241cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li> 2251cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2261cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2271cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2281cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2291cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 2301cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 2311cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 2321cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 2331cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 2341cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 2351cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 2361cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 2371cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 2381cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 2391cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 2401cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 2411cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 2421cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 2431cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 2441cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 2451cb0ef41Sopenharmony_ci</ul> 2461cb0ef41Sopenharmony_ci<hr class="line"> 2471cb0ef41Sopenharmony_ci<ul> 2481cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 2491cb0ef41Sopenharmony_ci</ul></div> 2501cb0ef41Sopenharmony_ci </li> 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci <li class="picker-header"> 2541cb0ef41Sopenharmony_ci <a href="#"> 2551cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2561cb0ef41Sopenharmony_ci Other versions 2571cb0ef41Sopenharmony_ci </a> 2581cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/cluster.html">21.x</a></li> 2591cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/cluster.html">20.x <b>LTS</b></a></li> 2601cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/cluster.html">19.x</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/cluster.html">18.x <b>LTS</b></a></li> 2621cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/cluster.html">17.x</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/cluster.html">16.x</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/cluster.html">15.x</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/cluster.html">14.x</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/cluster.html">13.x</a></li> 2671cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/cluster.html">12.x</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/cluster.html">11.x</a></li> 2691cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/cluster.html">10.x</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/cluster.html">9.x</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/cluster.html">8.x</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/cluster.html">7.x</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/cluster.html">6.x</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/cluster.html">5.x</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/cluster.html">4.x</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/cluster.html">0.12.x</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/cluster.html">0.10.x</a></li></ol></div> 2781cb0ef41Sopenharmony_ci </li> 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci <li class="picker-header"> 2811cb0ef41Sopenharmony_ci <a href="#"> 2821cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2831cb0ef41Sopenharmony_ci Options 2841cb0ef41Sopenharmony_ci </a> 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci <div class="picker"> 2871cb0ef41Sopenharmony_ci <ul> 2881cb0ef41Sopenharmony_ci <li> 2891cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 2901cb0ef41Sopenharmony_ci </li> 2911cb0ef41Sopenharmony_ci <li> 2921cb0ef41Sopenharmony_ci <a href="cluster.json">View as JSON</a> 2931cb0ef41Sopenharmony_ci </li> 2941cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/cluster.md">Edit on GitHub</a></li> 2951cb0ef41Sopenharmony_ci </ul> 2961cb0ef41Sopenharmony_ci </div> 2971cb0ef41Sopenharmony_ci </li> 2981cb0ef41Sopenharmony_ci </ul> 2991cb0ef41Sopenharmony_ci </div> 3001cb0ef41Sopenharmony_ci <hr> 3011cb0ef41Sopenharmony_ci </header> 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3041cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#cluster">Cluster</a></span> 3051cb0ef41Sopenharmony_ci<ul> 3061cb0ef41Sopenharmony_ci<li><a href="#how-it-works">How it works</a></li> 3071cb0ef41Sopenharmony_ci<li><a href="#class-worker">Class: <code>Worker</code></a> 3081cb0ef41Sopenharmony_ci<ul> 3091cb0ef41Sopenharmony_ci<li><a href="#event-disconnect">Event: <code>'disconnect'</code></a></li> 3101cb0ef41Sopenharmony_ci<li><a href="#event-error">Event: <code>'error'</code></a></li> 3111cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li> 3121cb0ef41Sopenharmony_ci<li><a href="#event-listening">Event: <code>'listening'</code></a></li> 3131cb0ef41Sopenharmony_ci<li><a href="#event-message">Event: <code>'message'</code></a></li> 3141cb0ef41Sopenharmony_ci<li><a href="#event-online">Event: <code>'online'</code></a></li> 3151cb0ef41Sopenharmony_ci<li><a href="#workerdisconnect"><code>worker.disconnect()</code></a></li> 3161cb0ef41Sopenharmony_ci<li><a href="#workerexitedafterdisconnect"><code>worker.exitedAfterDisconnect</code></a></li> 3171cb0ef41Sopenharmony_ci<li><a href="#workerid"><code>worker.id</code></a></li> 3181cb0ef41Sopenharmony_ci<li><a href="#workerisconnected"><code>worker.isConnected()</code></a></li> 3191cb0ef41Sopenharmony_ci<li><a href="#workerisdead"><code>worker.isDead()</code></a></li> 3201cb0ef41Sopenharmony_ci<li><a href="#workerkillsignal"><code>worker.kill([signal])</code></a></li> 3211cb0ef41Sopenharmony_ci<li><a href="#workerprocess"><code>worker.process</code></a></li> 3221cb0ef41Sopenharmony_ci<li><a href="#workersendmessage-sendhandle-options-callback"><code>worker.send(message[, sendHandle[, options]][, callback])</code></a></li> 3231cb0ef41Sopenharmony_ci</ul> 3241cb0ef41Sopenharmony_ci</li> 3251cb0ef41Sopenharmony_ci<li><a href="#event-disconnect_1">Event: <code>'disconnect'</code></a></li> 3261cb0ef41Sopenharmony_ci<li><a href="#event-exit_1">Event: <code>'exit'</code></a></li> 3271cb0ef41Sopenharmony_ci<li><a href="#event-fork">Event: <code>'fork'</code></a></li> 3281cb0ef41Sopenharmony_ci<li><a href="#event-listening_1">Event: <code>'listening'</code></a></li> 3291cb0ef41Sopenharmony_ci<li><a href="#event-message_1">Event: <code>'message'</code></a></li> 3301cb0ef41Sopenharmony_ci<li><a href="#event-online_1">Event: <code>'online'</code></a></li> 3311cb0ef41Sopenharmony_ci<li><a href="#event-setup">Event: <code>'setup'</code></a></li> 3321cb0ef41Sopenharmony_ci<li><a href="#clusterdisconnectcallback"><code>cluster.disconnect([callback])</code></a></li> 3331cb0ef41Sopenharmony_ci<li><a href="#clusterforkenv"><code>cluster.fork([env])</code></a></li> 3341cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#clusterismaster"><code>cluster.isMaster</code></a></span></li> 3351cb0ef41Sopenharmony_ci<li><a href="#clusterisprimary"><code>cluster.isPrimary</code></a></li> 3361cb0ef41Sopenharmony_ci<li><a href="#clusterisworker"><code>cluster.isWorker</code></a></li> 3371cb0ef41Sopenharmony_ci<li><a href="#clusterschedulingpolicy"><code>cluster.schedulingPolicy</code></a></li> 3381cb0ef41Sopenharmony_ci<li><a href="#clustersettings"><code>cluster.settings</code></a></li> 3391cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#clustersetupmastersettings"><code>cluster.setupMaster([settings])</code></a></span></li> 3401cb0ef41Sopenharmony_ci<li><a href="#clustersetupprimarysettings"><code>cluster.setupPrimary([settings])</code></a></li> 3411cb0ef41Sopenharmony_ci<li><a href="#clusterworker"><code>cluster.worker</code></a></li> 3421cb0ef41Sopenharmony_ci<li><a href="#clusterworkers"><code>cluster.workers</code></a></li> 3431cb0ef41Sopenharmony_ci</ul> 3441cb0ef41Sopenharmony_ci</li> 3451cb0ef41Sopenharmony_ci</ul></details> 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ci <div id="apicontent"> 3481cb0ef41Sopenharmony_ci <h2>Cluster<span><a class="mark" href="#cluster" id="cluster">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster"></a></h2> 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 3511cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/cluster.js">lib/cluster.js</a></p> 3521cb0ef41Sopenharmony_ci<p>Clusters of Node.js processes can be used to run multiple instances of Node.js 3531cb0ef41Sopenharmony_cithat can distribute workloads among their application threads. When process 3541cb0ef41Sopenharmony_ciisolation is not needed, use the <a href="worker_threads.html"><code>worker_threads</code></a> module instead, which 3551cb0ef41Sopenharmony_ciallows running multiple application threads within a single Node.js instance.</p> 3561cb0ef41Sopenharmony_ci<p>The cluster module allows easy creation of child processes that all share 3571cb0ef41Sopenharmony_ciserver ports.</p> 3581cb0ef41Sopenharmony_ci 3591cb0ef41Sopenharmony_ci<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> cluster <span class="hljs-keyword">from</span> <span class="hljs-string">'node:cluster'</span>; 3601cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>; 3611cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { availableParallelism } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 3621cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 3631cb0ef41Sopenharmony_ci 3641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> numCPUs = <span class="hljs-title function_">availableParallelism</span>(); 3651cb0ef41Sopenharmony_ci 3661cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 3671cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Primary <span class="hljs-subst">${process.pid}</span> is running`</span>); 3681cb0ef41Sopenharmony_ci 3691cb0ef41Sopenharmony_ci <span class="hljs-comment">// Fork workers.</span> 3701cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < numCPUs; i++) { 3711cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 3721cb0ef41Sopenharmony_ci } 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">worker, code, signal</span>) =></span> { 3751cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`worker <span class="hljs-subst">${worker.process.pid}</span> died`</span>); 3761cb0ef41Sopenharmony_ci }); 3771cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 3781cb0ef41Sopenharmony_ci <span class="hljs-comment">// Workers can share any TCP connection</span> 3791cb0ef41Sopenharmony_ci <span class="hljs-comment">// In this case it is an HTTP server</span> 3801cb0ef41Sopenharmony_ci http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 3811cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 3821cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 3831cb0ef41Sopenharmony_ci }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>); 3841cb0ef41Sopenharmony_ci 3851cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Worker <span class="hljs-subst">${process.pid}</span> started`</span>); 3861cb0ef41Sopenharmony_ci}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:cluster'</span>); 3871cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 3881cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:os'</span>).<span class="hljs-title function_">availableParallelism</span>(); 3891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> process = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 3901cb0ef41Sopenharmony_ci 3911cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 3921cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Primary <span class="hljs-subst">${process.pid}</span> is running`</span>); 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci <span class="hljs-comment">// Fork workers.</span> 3951cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < numCPUs; i++) { 3961cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 3971cb0ef41Sopenharmony_ci } 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">worker, code, signal</span>) =></span> { 4001cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`worker <span class="hljs-subst">${worker.process.pid}</span> died`</span>); 4011cb0ef41Sopenharmony_ci }); 4021cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 4031cb0ef41Sopenharmony_ci <span class="hljs-comment">// Workers can share any TCP connection</span> 4041cb0ef41Sopenharmony_ci <span class="hljs-comment">// In this case it is an HTTP server</span> 4051cb0ef41Sopenharmony_ci http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 4061cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 4071cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 4081cb0ef41Sopenharmony_ci }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>); 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Worker <span class="hljs-subst">${process.pid}</span> started`</span>); 4111cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 4121cb0ef41Sopenharmony_ci<p>Running Node.js will now share port 8000 between the workers:</p> 4131cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node server.js</span> 4141cb0ef41Sopenharmony_ciPrimary 3596 is running 4151cb0ef41Sopenharmony_ciWorker 4324 started 4161cb0ef41Sopenharmony_ciWorker 4520 started 4171cb0ef41Sopenharmony_ciWorker 6056 started 4181cb0ef41Sopenharmony_ciWorker 5644 started</code> <button class="copy-button">copy</button></pre> 4191cb0ef41Sopenharmony_ci<p>On Windows, it is not yet possible to set up a named pipe server in a worker.</p> 4201cb0ef41Sopenharmony_ci<section><h3>How it works<span><a class="mark" href="#how-it-works" id="how-it-works">#</a></span><a aria-hidden="true" class="legacy" id="cluster_how_it_works"></a></h3> 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_ci<p>The worker processes are spawned using the <a href="child_process.html#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a> method, 4231cb0ef41Sopenharmony_ciso that they can communicate with the parent via IPC and pass server 4241cb0ef41Sopenharmony_cihandles back and forth.</p> 4251cb0ef41Sopenharmony_ci<p>The cluster module supports two methods of distributing incoming 4261cb0ef41Sopenharmony_ciconnections.</p> 4271cb0ef41Sopenharmony_ci<p>The first one (and the default one on all platforms except Windows) 4281cb0ef41Sopenharmony_ciis the round-robin approach, where the primary process listens on a 4291cb0ef41Sopenharmony_ciport, accepts new connections and distributes them across the workers 4301cb0ef41Sopenharmony_ciin a round-robin fashion, with some built-in smarts to avoid 4311cb0ef41Sopenharmony_cioverloading a worker process.</p> 4321cb0ef41Sopenharmony_ci<p>The second approach is where the primary process creates the listen 4331cb0ef41Sopenharmony_cisocket and sends it to interested workers. The workers then accept 4341cb0ef41Sopenharmony_ciincoming connections directly.</p> 4351cb0ef41Sopenharmony_ci<p>The second approach should, in theory, give the best performance. 4361cb0ef41Sopenharmony_ciIn practice however, distribution tends to be very unbalanced due 4371cb0ef41Sopenharmony_cito operating system scheduler vagaries. Loads have been observed 4381cb0ef41Sopenharmony_ciwhere over 70% of all connections ended up in just two processes, 4391cb0ef41Sopenharmony_ciout of a total of eight.</p> 4401cb0ef41Sopenharmony_ci<p>Because <code>server.listen()</code> hands off most of the work to the primary 4411cb0ef41Sopenharmony_ciprocess, there are three cases where the behavior between a normal 4421cb0ef41Sopenharmony_ciNode.js process and a cluster worker differs:</p> 4431cb0ef41Sopenharmony_ci<ol> 4441cb0ef41Sopenharmony_ci<li><code>server.listen({fd: 7})</code> Because the message is passed to the primary, 4451cb0ef41Sopenharmony_cifile descriptor 7 <strong>in the parent</strong> will be listened on, and the 4461cb0ef41Sopenharmony_cihandle passed to the worker, rather than listening to the worker's 4471cb0ef41Sopenharmony_ciidea of what the number 7 file descriptor references.</li> 4481cb0ef41Sopenharmony_ci<li><code>server.listen(handle)</code> Listening on handles explicitly will cause 4491cb0ef41Sopenharmony_cithe worker to use the supplied handle, rather than talk to the primary 4501cb0ef41Sopenharmony_ciprocess.</li> 4511cb0ef41Sopenharmony_ci<li><code>server.listen(0)</code> Normally, this will cause servers to listen on a 4521cb0ef41Sopenharmony_cirandom port. However, in a cluster, each worker will receive the 4531cb0ef41Sopenharmony_cisame "random" port each time they do <code>listen(0)</code>. In essence, the 4541cb0ef41Sopenharmony_ciport is random the first time, but predictable thereafter. To listen 4551cb0ef41Sopenharmony_cion a unique port, generate a port number based on the cluster worker ID.</li> 4561cb0ef41Sopenharmony_ci</ol> 4571cb0ef41Sopenharmony_ci<p>Node.js does not provide routing logic. It is therefore important to design an 4581cb0ef41Sopenharmony_ciapplication such that it does not rely too heavily on in-memory data objects for 4591cb0ef41Sopenharmony_cithings like sessions and login.</p> 4601cb0ef41Sopenharmony_ci<p>Because workers are all separate processes, they can be killed or 4611cb0ef41Sopenharmony_cire-spawned depending on a program's needs, without affecting other 4621cb0ef41Sopenharmony_ciworkers. As long as there are some workers still alive, the server will 4631cb0ef41Sopenharmony_cicontinue to accept connections. If no workers are alive, existing connections 4641cb0ef41Sopenharmony_ciwill be dropped and new connections will be refused. Node.js does not 4651cb0ef41Sopenharmony_ciautomatically manage the number of workers, however. It is the application's 4661cb0ef41Sopenharmony_ciresponsibility to manage the worker pool based on its own needs.</p> 4671cb0ef41Sopenharmony_ci<p>Although a primary use case for the <code>node:cluster</code> module is networking, it can 4681cb0ef41Sopenharmony_cialso be used for other use cases requiring worker processes.</p> 4691cb0ef41Sopenharmony_ci</section><section><h3>Class: <code>Worker</code><span><a class="mark" href="#class-worker" id="class-worker">#</a></span><a aria-hidden="true" class="legacy" id="cluster_class_worker"></a></h3> 4701cb0ef41Sopenharmony_ci<div class="api_metadata"> 4711cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 4721cb0ef41Sopenharmony_ci</div> 4731cb0ef41Sopenharmony_ci<ul> 4741cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 4751cb0ef41Sopenharmony_ci</ul> 4761cb0ef41Sopenharmony_ci<p>A <code>Worker</code> object contains all public information and method about a worker. 4771cb0ef41Sopenharmony_ciIn the primary it can be obtained using <code>cluster.workers</code>. In a worker 4781cb0ef41Sopenharmony_ciit can be obtained using <code>cluster.worker</code>.</p> 4791cb0ef41Sopenharmony_ci<h4>Event: <code>'disconnect'</code><span><a class="mark" href="#event-disconnect" id="event-disconnect">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_disconnect"></a></h4> 4801cb0ef41Sopenharmony_ci<div class="api_metadata"> 4811cb0ef41Sopenharmony_ci<span>Added in: v0.7.7</span> 4821cb0ef41Sopenharmony_ci</div> 4831cb0ef41Sopenharmony_ci<p>Similar to the <code>cluster.on('disconnect')</code> event, but specific to this worker.</p> 4841cb0ef41Sopenharmony_ci<pre><code class="language-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>, <span class="hljs-function">() =></span> { 4851cb0ef41Sopenharmony_ci <span class="hljs-comment">// Worker has disconnected</span> 4861cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 4871cb0ef41Sopenharmony_ci<h4>Event: <code>'error'</code><span><a class="mark" href="#event-error" id="event-error">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_error"></a></h4> 4881cb0ef41Sopenharmony_ci<div class="api_metadata"> 4891cb0ef41Sopenharmony_ci<span>Added in: v0.7.3</span> 4901cb0ef41Sopenharmony_ci</div> 4911cb0ef41Sopenharmony_ci<p>This event is the same as the one provided by <a href="child_process.html#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>.</p> 4921cb0ef41Sopenharmony_ci<p>Within a worker, <code>process.on('error')</code> may also be used.</p> 4931cb0ef41Sopenharmony_ci<h4>Event: <code>'exit'</code><span><a class="mark" href="#event-exit" id="event-exit">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_exit"></a></h4> 4941cb0ef41Sopenharmony_ci<div class="api_metadata"> 4951cb0ef41Sopenharmony_ci<span>Added in: v0.11.2</span> 4961cb0ef41Sopenharmony_ci</div> 4971cb0ef41Sopenharmony_ci<ul> 4981cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The exit code, if it exited normally.</li> 4991cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the signal (e.g. <code>'SIGHUP'</code>) that caused 5001cb0ef41Sopenharmony_cithe process to be killed.</li> 5011cb0ef41Sopenharmony_ci</ul> 5021cb0ef41Sopenharmony_ci<p>Similar to the <code>cluster.on('exit')</code> event, but specific to this worker.</p> 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_ci<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> cluster <span class="hljs-keyword">from</span> <span class="hljs-string">'node:cluster'</span>; 5051cb0ef41Sopenharmony_ci 5061cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 5071cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</span>(); 5081cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code, signal</span>) =></span> { 5091cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (signal) { 5101cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`worker was killed by signal: <span class="hljs-subst">${signal}</span>`</span>); 5111cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) { 5121cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`worker exited with error code: <span class="hljs-subst">${code}</span>`</span>); 5131cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 5141cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'worker success!'</span>); 5151cb0ef41Sopenharmony_ci } 5161cb0ef41Sopenharmony_ci }); 5171cb0ef41Sopenharmony_ci}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:cluster'</span>); 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 5201cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</span>(); 5211cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">code, signal</span>) =></span> { 5221cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (signal) { 5231cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`worker was killed by signal: <span class="hljs-subst">${signal}</span>`</span>); 5241cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (code !== <span class="hljs-number">0</span>) { 5251cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`worker exited with error code: <span class="hljs-subst">${code}</span>`</span>); 5261cb0ef41Sopenharmony_ci } <span class="hljs-keyword">else</span> { 5271cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'worker success!'</span>); 5281cb0ef41Sopenharmony_ci } 5291cb0ef41Sopenharmony_ci }); 5301cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 5311cb0ef41Sopenharmony_ci<h4>Event: <code>'listening'</code><span><a class="mark" href="#event-listening" id="event-listening">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_listening"></a></h4> 5321cb0ef41Sopenharmony_ci<div class="api_metadata"> 5331cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 5341cb0ef41Sopenharmony_ci</div> 5351cb0ef41Sopenharmony_ci<ul> 5361cb0ef41Sopenharmony_ci<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 5371cb0ef41Sopenharmony_ci</ul> 5381cb0ef41Sopenharmony_ci<p>Similar to the <code>cluster.on('listening')</code> event, but specific to this worker.</p> 5391cb0ef41Sopenharmony_ci 5401cb0ef41Sopenharmony_ci<pre class="with-45-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span class="hljs-function">(<span class="hljs-params">address</span>) =></span> { 5411cb0ef41Sopenharmony_ci <span class="hljs-comment">// Worker is listening</span> 5421cb0ef41Sopenharmony_ci});</code><code class="language-js cjs">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span class="hljs-function">(<span class="hljs-params">address</span>) =></span> { 5431cb0ef41Sopenharmony_ci <span class="hljs-comment">// Worker is listening</span> 5441cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 5451cb0ef41Sopenharmony_ci<p>It is not emitted in the worker.</p> 5461cb0ef41Sopenharmony_ci<h4>Event: <code>'message'</code><span><a class="mark" href="#event-message" id="event-message">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_message"></a></h4> 5471cb0ef41Sopenharmony_ci<div class="api_metadata"> 5481cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 5491cb0ef41Sopenharmony_ci</div> 5501cb0ef41Sopenharmony_ci<ul> 5511cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 5521cb0ef41Sopenharmony_ci<li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 5531cb0ef41Sopenharmony_ci</ul> 5541cb0ef41Sopenharmony_ci<p>Similar to the <code>'message'</code> event of <code>cluster</code>, but specific to this worker.</p> 5551cb0ef41Sopenharmony_ci<p>Within a worker, <code>process.on('message')</code> may also be used.</p> 5561cb0ef41Sopenharmony_ci<p>See <a href="process.html#event-message"><code>process</code> event: <code>'message'</code></a>.</p> 5571cb0ef41Sopenharmony_ci<p>Here is an example using the message system. It keeps a count in the primary 5581cb0ef41Sopenharmony_ciprocess of the number of HTTP requests received by the workers:</p> 5591cb0ef41Sopenharmony_ci 5601cb0ef41Sopenharmony_ci<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> cluster <span class="hljs-keyword">from</span> <span class="hljs-string">'node:cluster'</span>; 5611cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>; 5621cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { availableParallelism } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 5631cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 5641cb0ef41Sopenharmony_ci 5651cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 5661cb0ef41Sopenharmony_ci 5671cb0ef41Sopenharmony_ci <span class="hljs-comment">// Keep track of http requests</span> 5681cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> numReqs = <span class="hljs-number">0</span>; 5691cb0ef41Sopenharmony_ci <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> { 5701cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`numReqs = <span class="hljs-subst">${numReqs}</span>`</span>); 5711cb0ef41Sopenharmony_ci }, <span class="hljs-number">1000</span>); 5721cb0ef41Sopenharmony_ci 5731cb0ef41Sopenharmony_ci <span class="hljs-comment">// Count requests</span> 5741cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">messageHandler</span>(<span class="hljs-params">msg</span>) { 5751cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (msg.<span class="hljs-property">cmd</span> && msg.<span class="hljs-property">cmd</span> === <span class="hljs-string">'notifyRequest'</span>) { 5761cb0ef41Sopenharmony_ci numReqs += <span class="hljs-number">1</span>; 5771cb0ef41Sopenharmony_ci } 5781cb0ef41Sopenharmony_ci } 5791cb0ef41Sopenharmony_ci 5801cb0ef41Sopenharmony_ci <span class="hljs-comment">// Start workers and listen for messages containing notifyRequest</span> 5811cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> numCPUs = <span class="hljs-title function_">availableParallelism</span>(); 5821cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < numCPUs; i++) { 5831cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 5841cb0ef41Sopenharmony_ci } 5851cb0ef41Sopenharmony_ci 5861cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-keyword">in</span> cluster.<span class="hljs-property">workers</span>) { 5871cb0ef41Sopenharmony_ci cluster.<span class="hljs-property">workers</span>[id].<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, messageHandler); 5881cb0ef41Sopenharmony_ci } 5891cb0ef41Sopenharmony_ci 5901cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 5911cb0ef41Sopenharmony_ci 5921cb0ef41Sopenharmony_ci <span class="hljs-comment">// Worker processes have a http server.</span> 5931cb0ef41Sopenharmony_ci http.<span class="hljs-title class_">Server</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 5941cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 5951cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 5961cb0ef41Sopenharmony_ci 5971cb0ef41Sopenharmony_ci <span class="hljs-comment">// Notify primary about the request</span> 5981cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">cmd</span>: <span class="hljs-string">'notifyRequest'</span> }); 5991cb0ef41Sopenharmony_ci }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>); 6001cb0ef41Sopenharmony_ci}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:cluster'</span>); 6011cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 6021cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> process = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 6051cb0ef41Sopenharmony_ci 6061cb0ef41Sopenharmony_ci <span class="hljs-comment">// Keep track of http requests</span> 6071cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> numReqs = <span class="hljs-number">0</span>; 6081cb0ef41Sopenharmony_ci <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =></span> { 6091cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`numReqs = <span class="hljs-subst">${numReqs}</span>`</span>); 6101cb0ef41Sopenharmony_ci }, <span class="hljs-number">1000</span>); 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci <span class="hljs-comment">// Count requests</span> 6131cb0ef41Sopenharmony_ci <span class="hljs-keyword">function</span> <span class="hljs-title function_">messageHandler</span>(<span class="hljs-params">msg</span>) { 6141cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (msg.<span class="hljs-property">cmd</span> && msg.<span class="hljs-property">cmd</span> === <span class="hljs-string">'notifyRequest'</span>) { 6151cb0ef41Sopenharmony_ci numReqs += <span class="hljs-number">1</span>; 6161cb0ef41Sopenharmony_ci } 6171cb0ef41Sopenharmony_ci } 6181cb0ef41Sopenharmony_ci 6191cb0ef41Sopenharmony_ci <span class="hljs-comment">// Start workers and listen for messages containing notifyRequest</span> 6201cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:os'</span>).<span class="hljs-title function_">availableParallelism</span>(); 6211cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < numCPUs; i++) { 6221cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 6231cb0ef41Sopenharmony_ci } 6241cb0ef41Sopenharmony_ci 6251cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> id <span class="hljs-keyword">in</span> cluster.<span class="hljs-property">workers</span>) { 6261cb0ef41Sopenharmony_ci cluster.<span class="hljs-property">workers</span>[id].<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, messageHandler); 6271cb0ef41Sopenharmony_ci } 6281cb0ef41Sopenharmony_ci 6291cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 6301cb0ef41Sopenharmony_ci 6311cb0ef41Sopenharmony_ci <span class="hljs-comment">// Worker processes have a http server.</span> 6321cb0ef41Sopenharmony_ci http.<span class="hljs-title class_">Server</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 6331cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 6341cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ci <span class="hljs-comment">// Notify primary about the request</span> 6371cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">send</span>({ <span class="hljs-attr">cmd</span>: <span class="hljs-string">'notifyRequest'</span> }); 6381cb0ef41Sopenharmony_ci }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>); 6391cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 6401cb0ef41Sopenharmony_ci<h4>Event: <code>'online'</code><span><a class="mark" href="#event-online" id="event-online">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_online"></a></h4> 6411cb0ef41Sopenharmony_ci<div class="api_metadata"> 6421cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 6431cb0ef41Sopenharmony_ci</div> 6441cb0ef41Sopenharmony_ci<p>Similar to the <code>cluster.on('online')</code> event, but specific to this worker.</p> 6451cb0ef41Sopenharmony_ci<pre><code class="language-js">cluster.<span class="hljs-title function_">fork</span>().<span class="hljs-title function_">on</span>(<span class="hljs-string">'online'</span>, <span class="hljs-function">() =></span> { 6461cb0ef41Sopenharmony_ci <span class="hljs-comment">// Worker is online</span> 6471cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 6481cb0ef41Sopenharmony_ci<p>It is not emitted in the worker.</p> 6491cb0ef41Sopenharmony_ci<h4><code>worker.disconnect()</code><span><a class="mark" href="#workerdisconnect" id="workerdisconnect">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_disconnect"></a></h4> 6501cb0ef41Sopenharmony_ci<div class="api_metadata"> 6511cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 6521cb0ef41Sopenharmony_ci<table> 6531cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 6541cb0ef41Sopenharmony_ci<tr><td>v7.3.0</td> 6551cb0ef41Sopenharmony_ci<td><p>This method now returns a reference to <code>worker</code>.</p></td></tr> 6561cb0ef41Sopenharmony_ci<tr><td>v0.7.7</td> 6571cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.7</span></p></td></tr> 6581cb0ef41Sopenharmony_ci</tbody></table> 6591cb0ef41Sopenharmony_ci</details> 6601cb0ef41Sopenharmony_ci</div> 6611cb0ef41Sopenharmony_ci<ul> 6621cb0ef41Sopenharmony_ci<li>Returns: <a href="cluster.html#class-worker" class="type"><cluster.Worker></a> A reference to <code>worker</code>.</li> 6631cb0ef41Sopenharmony_ci</ul> 6641cb0ef41Sopenharmony_ci<p>In a worker, this function will close all servers, wait for the <code>'close'</code> event 6651cb0ef41Sopenharmony_cion those servers, and then disconnect the IPC channel.</p> 6661cb0ef41Sopenharmony_ci<p>In the primary, an internal message is sent to the worker causing it to call 6671cb0ef41Sopenharmony_ci<code>.disconnect()</code> on itself.</p> 6681cb0ef41Sopenharmony_ci<p>Causes <code>.exitedAfterDisconnect</code> to be set.</p> 6691cb0ef41Sopenharmony_ci<p>After a server is closed, it will no longer accept new connections, 6701cb0ef41Sopenharmony_cibut connections may be accepted by any other listening worker. Existing 6711cb0ef41Sopenharmony_ciconnections will be allowed to close as usual. When no more connections exist, 6721cb0ef41Sopenharmony_cisee <a href="net.html#event-close"><code>server.close()</code></a>, the IPC channel to the worker will close allowing it 6731cb0ef41Sopenharmony_cito die gracefully.</p> 6741cb0ef41Sopenharmony_ci<p>The above applies <em>only</em> to server connections, client connections are not 6751cb0ef41Sopenharmony_ciautomatically closed by workers, and disconnect does not wait for them to close 6761cb0ef41Sopenharmony_cibefore exiting.</p> 6771cb0ef41Sopenharmony_ci<p>In a worker, <code>process.disconnect</code> exists, but it is not this function; 6781cb0ef41Sopenharmony_ciit is <a href="child_process.html#subprocessdisconnect"><code>disconnect()</code></a>.</p> 6791cb0ef41Sopenharmony_ci<p>Because long living server connections may block workers from disconnecting, it 6801cb0ef41Sopenharmony_cimay be useful to send a message, so application specific actions may be taken to 6811cb0ef41Sopenharmony_ciclose them. It also may be useful to implement a timeout, killing a worker if 6821cb0ef41Sopenharmony_cithe <code>'disconnect'</code> event has not been emitted after some time.</p> 6831cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 6841cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</span>(); 6851cb0ef41Sopenharmony_ci <span class="hljs-keyword">let</span> timeout; 6861cb0ef41Sopenharmony_ci 6871cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span class="hljs-function">(<span class="hljs-params">address</span>) =></span> { 6881cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'shutdown'</span>); 6891cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">disconnect</span>(); 6901cb0ef41Sopenharmony_ci timeout = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 6911cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">kill</span>(); 6921cb0ef41Sopenharmony_ci }, <span class="hljs-number">2000</span>); 6931cb0ef41Sopenharmony_ci }); 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>, <span class="hljs-function">() =></span> { 6961cb0ef41Sopenharmony_ci <span class="hljs-built_in">clearTimeout</span>(timeout); 6971cb0ef41Sopenharmony_ci }); 6981cb0ef41Sopenharmony_ci 6991cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isWorker</span>) { 7001cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); 7011cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> server = net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { 7021cb0ef41Sopenharmony_ci <span class="hljs-comment">// Connections never end</span> 7031cb0ef41Sopenharmony_ci }); 7041cb0ef41Sopenharmony_ci 7051cb0ef41Sopenharmony_ci server.<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>); 7061cb0ef41Sopenharmony_ci 7071cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">msg</span>) =></span> { 7081cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (msg === <span class="hljs-string">'shutdown'</span>) { 7091cb0ef41Sopenharmony_ci <span class="hljs-comment">// Initiate graceful close of any connections to server</span> 7101cb0ef41Sopenharmony_ci } 7111cb0ef41Sopenharmony_ci }); 7121cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 7131cb0ef41Sopenharmony_ci<h4><code>worker.exitedAfterDisconnect</code><span><a class="mark" href="#workerexitedafterdisconnect" id="workerexitedafterdisconnect">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_exitedafterdisconnect"></a></h4> 7141cb0ef41Sopenharmony_ci<div class="api_metadata"> 7151cb0ef41Sopenharmony_ci<span>Added in: v6.0.0</span> 7161cb0ef41Sopenharmony_ci</div> 7171cb0ef41Sopenharmony_ci<ul> 7181cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 7191cb0ef41Sopenharmony_ci</ul> 7201cb0ef41Sopenharmony_ci<p>This property is <code>true</code> if the worker exited due to <code>.disconnect()</code>. 7211cb0ef41Sopenharmony_ciIf the worker exited any other way, it is <code>false</code>. If the 7221cb0ef41Sopenharmony_ciworker has not exited, it is <code>undefined</code>.</p> 7231cb0ef41Sopenharmony_ci<p>The boolean <a href="#workerexitedafterdisconnect"><code>worker.exitedAfterDisconnect</code></a> allows distinguishing between 7241cb0ef41Sopenharmony_civoluntary and accidental exit, the primary may choose not to respawn a worker 7251cb0ef41Sopenharmony_cibased on this value.</p> 7261cb0ef41Sopenharmony_ci<pre><code class="language-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">worker, code, signal</span>) =></span> { 7271cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (worker.<span class="hljs-property">exitedAfterDisconnect</span> === <span class="hljs-literal">true</span>) { 7281cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Oh, it was just voluntary – no need to worry'</span>); 7291cb0ef41Sopenharmony_ci } 7301cb0ef41Sopenharmony_ci}); 7311cb0ef41Sopenharmony_ci 7321cb0ef41Sopenharmony_ci<span class="hljs-comment">// kill worker</span> 7331cb0ef41Sopenharmony_ciworker.<span class="hljs-title function_">kill</span>();</code> <button class="copy-button">copy</button></pre> 7341cb0ef41Sopenharmony_ci<h4><code>worker.id</code><span><a class="mark" href="#workerid" id="workerid">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_id"></a></h4> 7351cb0ef41Sopenharmony_ci<div class="api_metadata"> 7361cb0ef41Sopenharmony_ci<span>Added in: v0.8.0</span> 7371cb0ef41Sopenharmony_ci</div> 7381cb0ef41Sopenharmony_ci<ul> 7391cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 7401cb0ef41Sopenharmony_ci</ul> 7411cb0ef41Sopenharmony_ci<p>Each new worker is given its own unique id, this id is stored in the 7421cb0ef41Sopenharmony_ci<code>id</code>.</p> 7431cb0ef41Sopenharmony_ci<p>While a worker is alive, this is the key that indexes it in 7441cb0ef41Sopenharmony_ci<code>cluster.workers</code>.</p> 7451cb0ef41Sopenharmony_ci<h4><code>worker.isConnected()</code><span><a class="mark" href="#workerisconnected" id="workerisconnected">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_isconnected"></a></h4> 7461cb0ef41Sopenharmony_ci<div class="api_metadata"> 7471cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 7481cb0ef41Sopenharmony_ci</div> 7491cb0ef41Sopenharmony_ci<p>This function returns <code>true</code> if the worker is connected to its primary via its 7501cb0ef41Sopenharmony_ciIPC channel, <code>false</code> otherwise. A worker is connected to its primary after it 7511cb0ef41Sopenharmony_cihas been created. It is disconnected after the <code>'disconnect'</code> event is emitted.</p> 7521cb0ef41Sopenharmony_ci<h4><code>worker.isDead()</code><span><a class="mark" href="#workerisdead" id="workerisdead">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_isdead"></a></h4> 7531cb0ef41Sopenharmony_ci<div class="api_metadata"> 7541cb0ef41Sopenharmony_ci<span>Added in: v0.11.14</span> 7551cb0ef41Sopenharmony_ci</div> 7561cb0ef41Sopenharmony_ci<p>This function returns <code>true</code> if the worker's process has terminated (either 7571cb0ef41Sopenharmony_cibecause of exiting or being signaled). Otherwise, it returns <code>false</code>.</p> 7581cb0ef41Sopenharmony_ci 7591cb0ef41Sopenharmony_ci<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> cluster <span class="hljs-keyword">from</span> <span class="hljs-string">'node:cluster'</span>; 7601cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> http <span class="hljs-keyword">from</span> <span class="hljs-string">'node:http'</span>; 7611cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { availableParallelism } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>; 7621cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> process <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 7631cb0ef41Sopenharmony_ci 7641cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> numCPUs = <span class="hljs-title function_">availableParallelism</span>(); 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 7671cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Primary <span class="hljs-subst">${process.pid}</span> is running`</span>); 7681cb0ef41Sopenharmony_ci 7691cb0ef41Sopenharmony_ci <span class="hljs-comment">// Fork workers.</span> 7701cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < numCPUs; i++) { 7711cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 7721cb0ef41Sopenharmony_ci } 7731cb0ef41Sopenharmony_ci 7741cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class="hljs-function">(<span class="hljs-params">worker</span>) =></span> { 7751cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'worker is dead:'</span>, worker.<span class="hljs-title function_">isDead</span>()); 7761cb0ef41Sopenharmony_ci }); 7771cb0ef41Sopenharmony_ci 7781cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">worker, code, signal</span>) =></span> { 7791cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'worker is dead:'</span>, worker.<span class="hljs-title function_">isDead</span>()); 7801cb0ef41Sopenharmony_ci }); 7811cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 7821cb0ef41Sopenharmony_ci <span class="hljs-comment">// Workers can share any TCP connection. In this case, it is an HTTP server.</span> 7831cb0ef41Sopenharmony_ci http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 7841cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 7851cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Current process\n <span class="hljs-subst">${process.pid}</span>`</span>); 7861cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</span>); 7871cb0ef41Sopenharmony_ci }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>); 7881cb0ef41Sopenharmony_ci}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:cluster'</span>); 7891cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:http'</span>); 7901cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> numCPUs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:os'</span>).<span class="hljs-title function_">availableParallelism</span>(); 7911cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> process = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 7921cb0ef41Sopenharmony_ci 7931cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 7941cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Primary <span class="hljs-subst">${process.pid}</span> is running`</span>); 7951cb0ef41Sopenharmony_ci 7961cb0ef41Sopenharmony_ci <span class="hljs-comment">// Fork workers.</span> 7971cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < numCPUs; i++) { 7981cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 7991cb0ef41Sopenharmony_ci } 8001cb0ef41Sopenharmony_ci 8011cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class="hljs-function">(<span class="hljs-params">worker</span>) =></span> { 8021cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'worker is dead:'</span>, worker.<span class="hljs-title function_">isDead</span>()); 8031cb0ef41Sopenharmony_ci }); 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">worker, code, signal</span>) =></span> { 8061cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'worker is dead:'</span>, worker.<span class="hljs-title function_">isDead</span>()); 8071cb0ef41Sopenharmony_ci }); 8081cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> { 8091cb0ef41Sopenharmony_ci <span class="hljs-comment">// Workers can share any TCP connection. In this case, it is an HTTP server.</span> 8101cb0ef41Sopenharmony_ci http.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 8111cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 8121cb0ef41Sopenharmony_ci res.<span class="hljs-title function_">end</span>(<span class="hljs-string">`Current process\n <span class="hljs-subst">${process.pid}</span>`</span>); 8131cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">kill</span>(process.<span class="hljs-property">pid</span>); 8141cb0ef41Sopenharmony_ci }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>); 8151cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 8161cb0ef41Sopenharmony_ci<h4><code>worker.kill([signal])</code><span><a class="mark" href="#workerkillsignal" id="workerkillsignal">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_kill_signal"></a></h4> 8171cb0ef41Sopenharmony_ci<div class="api_metadata"> 8181cb0ef41Sopenharmony_ci<span>Added in: v0.9.12</span> 8191cb0ef41Sopenharmony_ci</div> 8201cb0ef41Sopenharmony_ci<ul> 8211cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Name of the kill signal to send to the worker 8221cb0ef41Sopenharmony_ciprocess. <strong>Default:</strong> <code>'SIGTERM'</code></li> 8231cb0ef41Sopenharmony_ci</ul> 8241cb0ef41Sopenharmony_ci<p>This function will kill the worker. In the primary worker, it does this by 8251cb0ef41Sopenharmony_cidisconnecting the <code>worker.process</code>, and once disconnected, killing with 8261cb0ef41Sopenharmony_ci<code>signal</code>. In the worker, it does it by killing the process with <code>signal</code>.</p> 8271cb0ef41Sopenharmony_ci<p>The <code>kill()</code> function kills the worker process without waiting for a graceful 8281cb0ef41Sopenharmony_cidisconnect, it has the same behavior as <code>worker.process.kill()</code>.</p> 8291cb0ef41Sopenharmony_ci<p>This method is aliased as <code>worker.destroy()</code> for backwards compatibility.</p> 8301cb0ef41Sopenharmony_ci<p>In a worker, <code>process.kill()</code> exists, but it is not this function; 8311cb0ef41Sopenharmony_ciit is <a href="process.html#processkillpid-signal"><code>kill()</code></a>.</p> 8321cb0ef41Sopenharmony_ci<h4><code>worker.process</code><span><a class="mark" href="#workerprocess" id="workerprocess">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_process"></a></h4> 8331cb0ef41Sopenharmony_ci<div class="api_metadata"> 8341cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 8351cb0ef41Sopenharmony_ci</div> 8361cb0ef41Sopenharmony_ci<ul> 8371cb0ef41Sopenharmony_ci<li><a href="child_process.html#class-childprocess" class="type"><ChildProcess></a></li> 8381cb0ef41Sopenharmony_ci</ul> 8391cb0ef41Sopenharmony_ci<p>All workers are created using <a href="child_process.html#child_processforkmodulepath-args-options"><code>child_process.fork()</code></a>, the returned object 8401cb0ef41Sopenharmony_cifrom this function is stored as <code>.process</code>. In a worker, the global <code>process</code> 8411cb0ef41Sopenharmony_ciis stored.</p> 8421cb0ef41Sopenharmony_ci<p>See: <a href="child_process.html#child_processforkmodulepath-args-options">Child Process module</a>.</p> 8431cb0ef41Sopenharmony_ci<p>Workers will call <code>process.exit(0)</code> if the <code>'disconnect'</code> event occurs 8441cb0ef41Sopenharmony_cion <code>process</code> and <code>.exitedAfterDisconnect</code> is not <code>true</code>. This protects against 8451cb0ef41Sopenharmony_ciaccidental disconnection.</p> 8461cb0ef41Sopenharmony_ci<h4><code>worker.send(message[, sendHandle[, options]][, callback])</code><span><a class="mark" href="#workersendmessage-sendhandle-options-callback" id="workersendmessage-sendhandle-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="cluster_worker_send_message_sendhandle_options_callback"></a></h4> 8471cb0ef41Sopenharmony_ci<div class="api_metadata"> 8481cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 8491cb0ef41Sopenharmony_ci<table> 8501cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 8511cb0ef41Sopenharmony_ci<tr><td>v4.0.0</td> 8521cb0ef41Sopenharmony_ci<td><p>The <code>callback</code> parameter is supported now.</p></td></tr> 8531cb0ef41Sopenharmony_ci<tr><td>v0.7.0</td> 8541cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.0</span></p></td></tr> 8551cb0ef41Sopenharmony_ci</tbody></table> 8561cb0ef41Sopenharmony_ci</details> 8571cb0ef41Sopenharmony_ci</div> 8581cb0ef41Sopenharmony_ci<ul> 8591cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 8601cb0ef41Sopenharmony_ci<li><code>sendHandle</code> <a href="net.html#serverlistenhandle-backlog-callback" class="type"><Handle></a></li> 8611cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The <code>options</code> argument, if present, is an object used to 8621cb0ef41Sopenharmony_ciparameterize the sending of certain types of handles. <code>options</code> supports 8631cb0ef41Sopenharmony_cithe following properties: 8641cb0ef41Sopenharmony_ci<ul> 8651cb0ef41Sopenharmony_ci<li><code>keepOpen</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> A value that can be used when passing instances of 8661cb0ef41Sopenharmony_ci<code>net.Socket</code>. When <code>true</code>, the socket is kept open in the sending process. 8671cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 8681cb0ef41Sopenharmony_ci</ul> 8691cb0ef41Sopenharmony_ci</li> 8701cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 8711cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 8721cb0ef41Sopenharmony_ci</ul> 8731cb0ef41Sopenharmony_ci<p>Send a message to a worker or primary, optionally with a handle.</p> 8741cb0ef41Sopenharmony_ci<p>In the primary, this sends a message to a specific worker. It is identical to 8751cb0ef41Sopenharmony_ci<a href="child_process.html#subprocesssendmessage-sendhandle-options-callback"><code>ChildProcess.send()</code></a>.</p> 8761cb0ef41Sopenharmony_ci<p>In a worker, this sends a message to the primary. It is identical to 8771cb0ef41Sopenharmony_ci<code>process.send()</code>.</p> 8781cb0ef41Sopenharmony_ci<p>This example will echo back all messages from the primary:</p> 8791cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 8801cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> worker = cluster.<span class="hljs-title function_">fork</span>(); 8811cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'hi there'</span>); 8821cb0ef41Sopenharmony_ci 8831cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isWorker</span>) { 8841cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'message'</span>, <span class="hljs-function">(<span class="hljs-params">msg</span>) =></span> { 8851cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">send</span>(msg); 8861cb0ef41Sopenharmony_ci }); 8871cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 8881cb0ef41Sopenharmony_ci</section><section><h3>Event: <code>'disconnect'</code><span><a class="mark" href="#event-disconnect_1" id="event-disconnect_1">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_disconnect_1"></a></h3> 8891cb0ef41Sopenharmony_ci<div class="api_metadata"> 8901cb0ef41Sopenharmony_ci<span>Added in: v0.7.9</span> 8911cb0ef41Sopenharmony_ci</div> 8921cb0ef41Sopenharmony_ci<ul> 8931cb0ef41Sopenharmony_ci<li><code>worker</code> <a href="cluster.html#class-worker" class="type"><cluster.Worker></a></li> 8941cb0ef41Sopenharmony_ci</ul> 8951cb0ef41Sopenharmony_ci<p>Emitted after the worker IPC channel has disconnected. This can occur when a 8961cb0ef41Sopenharmony_ciworker exits gracefully, is killed, or is disconnected manually (such as with 8971cb0ef41Sopenharmony_ci<code>worker.disconnect()</code>).</p> 8981cb0ef41Sopenharmony_ci<p>There may be a delay between the <code>'disconnect'</code> and <code>'exit'</code> events. These 8991cb0ef41Sopenharmony_cievents can be used to detect if the process is stuck in a cleanup or if there 9001cb0ef41Sopenharmony_ciare long-living connections.</p> 9011cb0ef41Sopenharmony_ci<pre><code class="language-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'disconnect'</span>, <span class="hljs-function">(<span class="hljs-params">worker</span>) =></span> { 9021cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`The worker #<span class="hljs-subst">${worker.id}</span> has disconnected`</span>); 9031cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9041cb0ef41Sopenharmony_ci</section><section><h3>Event: <code>'exit'</code><span><a class="mark" href="#event-exit_1" id="event-exit_1">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_exit_1"></a></h3> 9051cb0ef41Sopenharmony_ci<div class="api_metadata"> 9061cb0ef41Sopenharmony_ci<span>Added in: v0.7.9</span> 9071cb0ef41Sopenharmony_ci</div> 9081cb0ef41Sopenharmony_ci<ul> 9091cb0ef41Sopenharmony_ci<li><code>worker</code> <a href="cluster.html#class-worker" class="type"><cluster.Worker></a></li> 9101cb0ef41Sopenharmony_ci<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The exit code, if it exited normally.</li> 9111cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the signal (e.g. <code>'SIGHUP'</code>) that caused 9121cb0ef41Sopenharmony_cithe process to be killed.</li> 9131cb0ef41Sopenharmony_ci</ul> 9141cb0ef41Sopenharmony_ci<p>When any of the workers die the cluster module will emit the <code>'exit'</code> event.</p> 9151cb0ef41Sopenharmony_ci<p>This can be used to restart the worker by calling <a href="#clusterforkenv"><code>.fork()</code></a> again.</p> 9161cb0ef41Sopenharmony_ci<pre><code class="language-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">worker, code, signal</span>) =></span> { 9171cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'worker %d died (%s). restarting...'</span>, 9181cb0ef41Sopenharmony_ci worker.<span class="hljs-property">process</span>.<span class="hljs-property">pid</span>, signal || code); 9191cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 9201cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9211cb0ef41Sopenharmony_ci<p>See <a href="child_process.html#event-exit"><code>child_process</code> event: <code>'exit'</code></a>.</p> 9221cb0ef41Sopenharmony_ci</section><section><h3>Event: <code>'fork'</code><span><a class="mark" href="#event-fork" id="event-fork">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_fork"></a></h3> 9231cb0ef41Sopenharmony_ci<div class="api_metadata"> 9241cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 9251cb0ef41Sopenharmony_ci</div> 9261cb0ef41Sopenharmony_ci<ul> 9271cb0ef41Sopenharmony_ci<li><code>worker</code> <a href="cluster.html#class-worker" class="type"><cluster.Worker></a></li> 9281cb0ef41Sopenharmony_ci</ul> 9291cb0ef41Sopenharmony_ci<p>When a new worker is forked the cluster module will emit a <code>'fork'</code> event. 9301cb0ef41Sopenharmony_ciThis can be used to log worker activity, and create a custom timeout.</p> 9311cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> timeouts = []; 9321cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">errorMsg</span>(<span class="hljs-params"></span>) { 9331cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Something must be wrong with the connection ...'</span>); 9341cb0ef41Sopenharmony_ci} 9351cb0ef41Sopenharmony_ci 9361cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'fork'</span>, <span class="hljs-function">(<span class="hljs-params">worker</span>) =></span> { 9371cb0ef41Sopenharmony_ci timeouts[worker.<span class="hljs-property">id</span>] = <span class="hljs-built_in">setTimeout</span>(errorMsg, <span class="hljs-number">2000</span>); 9381cb0ef41Sopenharmony_ci}); 9391cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span class="hljs-function">(<span class="hljs-params">worker, address</span>) =></span> { 9401cb0ef41Sopenharmony_ci <span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</span>]); 9411cb0ef41Sopenharmony_ci}); 9421cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">(<span class="hljs-params">worker, code, signal</span>) =></span> { 9431cb0ef41Sopenharmony_ci <span class="hljs-built_in">clearTimeout</span>(timeouts[worker.<span class="hljs-property">id</span>]); 9441cb0ef41Sopenharmony_ci <span class="hljs-title function_">errorMsg</span>(); 9451cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9461cb0ef41Sopenharmony_ci</section><section><h3>Event: <code>'listening'</code><span><a class="mark" href="#event-listening_1" id="event-listening_1">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_listening_1"></a></h3> 9471cb0ef41Sopenharmony_ci<div class="api_metadata"> 9481cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 9491cb0ef41Sopenharmony_ci</div> 9501cb0ef41Sopenharmony_ci<ul> 9511cb0ef41Sopenharmony_ci<li><code>worker</code> <a href="cluster.html#class-worker" class="type"><cluster.Worker></a></li> 9521cb0ef41Sopenharmony_ci<li><code>address</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 9531cb0ef41Sopenharmony_ci</ul> 9541cb0ef41Sopenharmony_ci<p>After calling <code>listen()</code> from a worker, when the <code>'listening'</code> event is emitted 9551cb0ef41Sopenharmony_cion the server, a <code>'listening'</code> event will also be emitted on <code>cluster</code> in the 9561cb0ef41Sopenharmony_ciprimary.</p> 9571cb0ef41Sopenharmony_ci<p>The event handler is executed with two arguments, the <code>worker</code> contains the 9581cb0ef41Sopenharmony_ciworker object and the <code>address</code> object contains the following connection 9591cb0ef41Sopenharmony_ciproperties: <code>address</code>, <code>port</code>, and <code>addressType</code>. This is very useful if the 9601cb0ef41Sopenharmony_ciworker is listening on more than one address.</p> 9611cb0ef41Sopenharmony_ci<pre><code class="language-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'listening'</span>, <span class="hljs-function">(<span class="hljs-params">worker, address</span>) =></span> { 9621cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 9631cb0ef41Sopenharmony_ci <span class="hljs-string">`A worker is now connected to <span class="hljs-subst">${address.address}</span>:<span class="hljs-subst">${address.port}</span>`</span>); 9641cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 9651cb0ef41Sopenharmony_ci<p>The <code>addressType</code> is one of:</p> 9661cb0ef41Sopenharmony_ci<ul> 9671cb0ef41Sopenharmony_ci<li><code>4</code> (TCPv4)</li> 9681cb0ef41Sopenharmony_ci<li><code>6</code> (TCPv6)</li> 9691cb0ef41Sopenharmony_ci<li><code>-1</code> (Unix domain socket)</li> 9701cb0ef41Sopenharmony_ci<li><code>'udp4'</code> or <code>'udp6'</code> (UDPv4 or UDPv6)</li> 9711cb0ef41Sopenharmony_ci</ul> 9721cb0ef41Sopenharmony_ci</section><section><h3>Event: <code>'message'</code><span><a class="mark" href="#event-message_1" id="event-message_1">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_message_1"></a></h3> 9731cb0ef41Sopenharmony_ci<div class="api_metadata"> 9741cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 9751cb0ef41Sopenharmony_ci<table> 9761cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 9771cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 9781cb0ef41Sopenharmony_ci<td><p>The <code>worker</code> parameter is passed now; see below for details.</p></td></tr> 9791cb0ef41Sopenharmony_ci<tr><td>v2.5.0</td> 9801cb0ef41Sopenharmony_ci<td><p><span>Added in: v2.5.0</span></p></td></tr> 9811cb0ef41Sopenharmony_ci</tbody></table> 9821cb0ef41Sopenharmony_ci</details> 9831cb0ef41Sopenharmony_ci</div> 9841cb0ef41Sopenharmony_ci<ul> 9851cb0ef41Sopenharmony_ci<li><code>worker</code> <a href="cluster.html#class-worker" class="type"><cluster.Worker></a></li> 9861cb0ef41Sopenharmony_ci<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 9871cb0ef41Sopenharmony_ci<li><code>handle</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 9881cb0ef41Sopenharmony_ci</ul> 9891cb0ef41Sopenharmony_ci<p>Emitted when the cluster primary receives a message from any worker.</p> 9901cb0ef41Sopenharmony_ci<p>See <a href="child_process.html#event-message"><code>child_process</code> event: <code>'message'</code></a>.</p> 9911cb0ef41Sopenharmony_ci</section><section><h3>Event: <code>'online'</code><span><a class="mark" href="#event-online_1" id="event-online_1">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_online_1"></a></h3> 9921cb0ef41Sopenharmony_ci<div class="api_metadata"> 9931cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 9941cb0ef41Sopenharmony_ci</div> 9951cb0ef41Sopenharmony_ci<ul> 9961cb0ef41Sopenharmony_ci<li><code>worker</code> <a href="cluster.html#class-worker" class="type"><cluster.Worker></a></li> 9971cb0ef41Sopenharmony_ci</ul> 9981cb0ef41Sopenharmony_ci<p>After forking a new worker, the worker should respond with an online message. 9991cb0ef41Sopenharmony_ciWhen the primary receives an online message it will emit this event. 10001cb0ef41Sopenharmony_ciThe difference between <code>'fork'</code> and <code>'online'</code> is that fork is emitted when the 10011cb0ef41Sopenharmony_ciprimary forks a worker, and <code>'online'</code> is emitted when the worker is running.</p> 10021cb0ef41Sopenharmony_ci<pre><code class="language-js">cluster.<span class="hljs-title function_">on</span>(<span class="hljs-string">'online'</span>, <span class="hljs-function">(<span class="hljs-params">worker</span>) =></span> { 10031cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Yay, the worker responded after it was forked'</span>); 10041cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 10051cb0ef41Sopenharmony_ci</section><section><h3>Event: <code>'setup'</code><span><a class="mark" href="#event-setup" id="event-setup">#</a></span><a aria-hidden="true" class="legacy" id="cluster_event_setup"></a></h3> 10061cb0ef41Sopenharmony_ci<div class="api_metadata"> 10071cb0ef41Sopenharmony_ci<span>Added in: v0.7.1</span> 10081cb0ef41Sopenharmony_ci</div> 10091cb0ef41Sopenharmony_ci<ul> 10101cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 10111cb0ef41Sopenharmony_ci</ul> 10121cb0ef41Sopenharmony_ci<p>Emitted every time <a href="#clustersetupprimarysettings"><code>.setupPrimary()</code></a> is called.</p> 10131cb0ef41Sopenharmony_ci<p>The <code>settings</code> object is the <code>cluster.settings</code> object at the time 10141cb0ef41Sopenharmony_ci<a href="#clustersetupprimarysettings"><code>.setupPrimary()</code></a> was called and is advisory only, since multiple calls to 10151cb0ef41Sopenharmony_ci<a href="#clustersetupprimarysettings"><code>.setupPrimary()</code></a> can be made in a single tick.</p> 10161cb0ef41Sopenharmony_ci<p>If accuracy is important, use <code>cluster.settings</code>.</p> 10171cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.disconnect([callback])</code><span><a class="mark" href="#clusterdisconnectcallback" id="clusterdisconnectcallback">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_disconnect_callback"></a></h3> 10181cb0ef41Sopenharmony_ci<div class="api_metadata"> 10191cb0ef41Sopenharmony_ci<span>Added in: v0.7.7</span> 10201cb0ef41Sopenharmony_ci</div> 10211cb0ef41Sopenharmony_ci<ul> 10221cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Called when all workers are disconnected and handles are 10231cb0ef41Sopenharmony_ciclosed.</li> 10241cb0ef41Sopenharmony_ci</ul> 10251cb0ef41Sopenharmony_ci<p>Calls <code>.disconnect()</code> on each worker in <code>cluster.workers</code>.</p> 10261cb0ef41Sopenharmony_ci<p>When they are disconnected all internal handles will be closed, allowing the 10271cb0ef41Sopenharmony_ciprimary process to die gracefully if no other event is waiting.</p> 10281cb0ef41Sopenharmony_ci<p>The method takes an optional callback argument which will be called when 10291cb0ef41Sopenharmony_cifinished.</p> 10301cb0ef41Sopenharmony_ci<p>This can only be called from the primary process.</p> 10311cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.fork([env])</code><span><a class="mark" href="#clusterforkenv" id="clusterforkenv">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_fork_env"></a></h3> 10321cb0ef41Sopenharmony_ci<div class="api_metadata"> 10331cb0ef41Sopenharmony_ci<span>Added in: v0.6.0</span> 10341cb0ef41Sopenharmony_ci</div> 10351cb0ef41Sopenharmony_ci<ul> 10361cb0ef41Sopenharmony_ci<li><code>env</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Key/value pairs to add to worker process environment.</li> 10371cb0ef41Sopenharmony_ci<li>Returns: <a href="cluster.html#class-worker" class="type"><cluster.Worker></a></li> 10381cb0ef41Sopenharmony_ci</ul> 10391cb0ef41Sopenharmony_ci<p>Spawn a new worker process.</p> 10401cb0ef41Sopenharmony_ci<p>This can only be called from the primary process.</p> 10411cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.isMaster</code><span><a class="mark" href="#clusterismaster" id="clusterismaster">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_ismaster"></a></h3> 10421cb0ef41Sopenharmony_ci<div class="api_metadata"> 10431cb0ef41Sopenharmony_ci<span>Added in: v0.8.1</span><span>Deprecated since: v16.0.0</span> 10441cb0ef41Sopenharmony_ci</div> 10451cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 10461cb0ef41Sopenharmony_ci<p>Deprecated alias for <a href="#clusterisprimary"><code>cluster.isPrimary</code></a>.</p> 10471cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.isPrimary</code><span><a class="mark" href="#clusterisprimary" id="clusterisprimary">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_isprimary"></a></h3> 10481cb0ef41Sopenharmony_ci<div class="api_metadata"> 10491cb0ef41Sopenharmony_ci<span>Added in: v16.0.0</span> 10501cb0ef41Sopenharmony_ci</div> 10511cb0ef41Sopenharmony_ci<ul> 10521cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 10531cb0ef41Sopenharmony_ci</ul> 10541cb0ef41Sopenharmony_ci<p>True if the process is a primary. This is determined 10551cb0ef41Sopenharmony_ciby the <code>process.env.NODE_UNIQUE_ID</code>. If <code>process.env.NODE_UNIQUE_ID</code> is 10561cb0ef41Sopenharmony_ciundefined, then <code>isPrimary</code> is <code>true</code>.</p> 10571cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.isWorker</code><span><a class="mark" href="#clusterisworker" id="clusterisworker">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_isworker"></a></h3> 10581cb0ef41Sopenharmony_ci<div class="api_metadata"> 10591cb0ef41Sopenharmony_ci<span>Added in: v0.6.0</span> 10601cb0ef41Sopenharmony_ci</div> 10611cb0ef41Sopenharmony_ci<ul> 10621cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 10631cb0ef41Sopenharmony_ci</ul> 10641cb0ef41Sopenharmony_ci<p>True if the process is not a primary (it is the negation of <code>cluster.isPrimary</code>).</p> 10651cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.schedulingPolicy</code><span><a class="mark" href="#clusterschedulingpolicy" id="clusterschedulingpolicy">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_schedulingpolicy"></a></h3> 10661cb0ef41Sopenharmony_ci<div class="api_metadata"> 10671cb0ef41Sopenharmony_ci<span>Added in: v0.11.2</span> 10681cb0ef41Sopenharmony_ci</div> 10691cb0ef41Sopenharmony_ci<p>The scheduling policy, either <code>cluster.SCHED_RR</code> for round-robin or 10701cb0ef41Sopenharmony_ci<code>cluster.SCHED_NONE</code> to leave it to the operating system. This is a 10711cb0ef41Sopenharmony_ciglobal setting and effectively frozen once either the first worker is spawned, 10721cb0ef41Sopenharmony_cior <a href="#clustersetupprimarysettings"><code>.setupPrimary()</code></a> is called, whichever comes first.</p> 10731cb0ef41Sopenharmony_ci<p><code>SCHED_RR</code> is the default on all operating systems except Windows. 10741cb0ef41Sopenharmony_ciWindows will change to <code>SCHED_RR</code> once libuv is able to effectively 10751cb0ef41Sopenharmony_cidistribute IOCP handles without incurring a large performance hit.</p> 10761cb0ef41Sopenharmony_ci<p><code>cluster.schedulingPolicy</code> can also be set through the 10771cb0ef41Sopenharmony_ci<code>NODE_CLUSTER_SCHED_POLICY</code> environment variable. Valid 10781cb0ef41Sopenharmony_civalues are <code>'rr'</code> and <code>'none'</code>.</p> 10791cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.settings</code><span><a class="mark" href="#clustersettings" id="clustersettings">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_settings"></a></h3> 10801cb0ef41Sopenharmony_ci<div class="api_metadata"> 10811cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 10821cb0ef41Sopenharmony_ci<table> 10831cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 10841cb0ef41Sopenharmony_ci<tr><td>v13.2.0, v12.16.0</td> 10851cb0ef41Sopenharmony_ci<td><p>The <code>serialization</code> option is supported now.</p></td></tr> 10861cb0ef41Sopenharmony_ci<tr><td>v9.5.0</td> 10871cb0ef41Sopenharmony_ci<td><p>The <code>cwd</code> option is supported now.</p></td></tr> 10881cb0ef41Sopenharmony_ci<tr><td>v9.4.0</td> 10891cb0ef41Sopenharmony_ci<td><p>The <code>windowsHide</code> option is supported now.</p></td></tr> 10901cb0ef41Sopenharmony_ci<tr><td>v8.2.0</td> 10911cb0ef41Sopenharmony_ci<td><p>The <code>inspectPort</code> option is supported now.</p></td></tr> 10921cb0ef41Sopenharmony_ci<tr><td>v6.4.0</td> 10931cb0ef41Sopenharmony_ci<td><p>The <code>stdio</code> option is supported now.</p></td></tr> 10941cb0ef41Sopenharmony_ci<tr><td>v0.7.1</td> 10951cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.1</span></p></td></tr> 10961cb0ef41Sopenharmony_ci</tbody></table> 10971cb0ef41Sopenharmony_ci</details> 10981cb0ef41Sopenharmony_ci</div> 10991cb0ef41Sopenharmony_ci<ul> 11001cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 11011cb0ef41Sopenharmony_ci<ul> 11021cb0ef41Sopenharmony_ci<li><code>execArgv</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> List of string arguments passed to the Node.js 11031cb0ef41Sopenharmony_ciexecutable. <strong>Default:</strong> <code>process.execArgv</code>.</li> 11041cb0ef41Sopenharmony_ci<li><code>exec</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> File path to worker file. <strong>Default:</strong> <code>process.argv[1]</code>.</li> 11051cb0ef41Sopenharmony_ci<li><code>args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> String arguments passed to worker. 11061cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>process.argv.slice(2)</code>.</li> 11071cb0ef41Sopenharmony_ci<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Current working directory of the worker process. <strong>Default:</strong> 11081cb0ef41Sopenharmony_ci<code>undefined</code> (inherits from parent process).</li> 11091cb0ef41Sopenharmony_ci<li><code>serialization</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specify the kind of serialization used for sending 11101cb0ef41Sopenharmony_cimessages between processes. Possible values are <code>'json'</code> and <code>'advanced'</code>. 11111cb0ef41Sopenharmony_ciSee <a href="child_process.html#advanced-serialization">Advanced serialization for <code>child_process</code></a> for more details. 11121cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 11131cb0ef41Sopenharmony_ci<li><code>silent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether or not to send output to parent's stdio. 11141cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>false</code>.</li> 11151cb0ef41Sopenharmony_ci<li><code>stdio</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type"><Array></a> Configures the stdio of forked processes. Because the 11161cb0ef41Sopenharmony_cicluster module relies on IPC to function, this configuration must contain an 11171cb0ef41Sopenharmony_ci<code>'ipc'</code> entry. When this option is provided, it overrides <code>silent</code>. See 11181cb0ef41Sopenharmony_ci<a href="child_process.html#child_processspawncommand-args-options"><code>child_process.spawn()</code></a>'s <a href="child_process.html#optionsstdio"><code>stdio</code></a>.</li> 11191cb0ef41Sopenharmony_ci<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the user identity of the process. (See <a href="http://man7.org/linux/man-pages/man2/setuid.2.html"><code>setuid(2)</code></a>.)</li> 11201cb0ef41Sopenharmony_ci<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Sets the group identity of the process. (See <a href="http://man7.org/linux/man-pages/man2/setgid.2.html"><code>setgid(2)</code></a>.)</li> 11211cb0ef41Sopenharmony_ci<li><code>inspectPort</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Sets inspector port of worker. 11221cb0ef41Sopenharmony_ciThis can be a number, or a function that takes no arguments and returns a 11231cb0ef41Sopenharmony_cinumber. By default each worker gets its own port, incremented from the 11241cb0ef41Sopenharmony_ciprimary's <code>process.debugPort</code>.</li> 11251cb0ef41Sopenharmony_ci<li><code>windowsHide</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Hide the forked processes console window that would 11261cb0ef41Sopenharmony_cinormally be created on Windows systems. <strong>Default:</strong> <code>false</code>.</li> 11271cb0ef41Sopenharmony_ci</ul> 11281cb0ef41Sopenharmony_ci</li> 11291cb0ef41Sopenharmony_ci</ul> 11301cb0ef41Sopenharmony_ci<p>After calling <a href="#clustersetupprimarysettings"><code>.setupPrimary()</code></a> (or <a href="#clusterforkenv"><code>.fork()</code></a>) this settings object will 11311cb0ef41Sopenharmony_cicontain the settings, including the default values.</p> 11321cb0ef41Sopenharmony_ci<p>This object is not intended to be changed or set manually.</p> 11331cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.setupMaster([settings])</code><span><a class="mark" href="#clustersetupmastersettings" id="clustersetupmastersettings">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_setupmaster_settings"></a></h3> 11341cb0ef41Sopenharmony_ci<div class="api_metadata"> 11351cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11361cb0ef41Sopenharmony_ci<table> 11371cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11381cb0ef41Sopenharmony_ci<tr><td>v16.0.0</td> 11391cb0ef41Sopenharmony_ci<td><p><span>Deprecated since: v16.0.0</span></p></td></tr> 11401cb0ef41Sopenharmony_ci<tr><td>v6.4.0</td> 11411cb0ef41Sopenharmony_ci<td><p>The <code>stdio</code> option is supported now.</p></td></tr> 11421cb0ef41Sopenharmony_ci<tr><td>v0.7.1</td> 11431cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.1</span></p></td></tr> 11441cb0ef41Sopenharmony_ci</tbody></table> 11451cb0ef41Sopenharmony_ci</details> 11461cb0ef41Sopenharmony_ci</div> 11471cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated</div><p></p> 11481cb0ef41Sopenharmony_ci<p>Deprecated alias for <a href="#clustersetupprimarysettings"><code>.setupPrimary()</code></a>.</p> 11491cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.setupPrimary([settings])</code><span><a class="mark" href="#clustersetupprimarysettings" id="clustersetupprimarysettings">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_setupprimary_settings"></a></h3> 11501cb0ef41Sopenharmony_ci<div class="api_metadata"> 11511cb0ef41Sopenharmony_ci<span>Added in: v16.0.0</span> 11521cb0ef41Sopenharmony_ci</div> 11531cb0ef41Sopenharmony_ci<ul> 11541cb0ef41Sopenharmony_ci<li><code>settings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> See <a href="#clustersettings"><code>cluster.settings</code></a>.</li> 11551cb0ef41Sopenharmony_ci</ul> 11561cb0ef41Sopenharmony_ci<p><code>setupPrimary</code> is used to change the default 'fork' behavior. Once called, 11571cb0ef41Sopenharmony_cithe settings will be present in <code>cluster.settings</code>.</p> 11581cb0ef41Sopenharmony_ci<p>Any settings changes only affect future calls to <a href="#clusterforkenv"><code>.fork()</code></a> and have no 11591cb0ef41Sopenharmony_cieffect on workers that are already running.</p> 11601cb0ef41Sopenharmony_ci<p>The only attribute of a worker that cannot be set via <code>.setupPrimary()</code> is 11611cb0ef41Sopenharmony_cithe <code>env</code> passed to <a href="#clusterforkenv"><code>.fork()</code></a>.</p> 11621cb0ef41Sopenharmony_ci<p>The defaults above apply to the first call only; the defaults for later 11631cb0ef41Sopenharmony_cicalls are the current values at the time of <code>cluster.setupPrimary()</code> is called.</p> 11641cb0ef41Sopenharmony_ci 11651cb0ef41Sopenharmony_ci<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> cluster <span class="hljs-keyword">from</span> <span class="hljs-string">'node:cluster'</span>; 11661cb0ef41Sopenharmony_ci 11671cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">setupPrimary</span>({ 11681cb0ef41Sopenharmony_ci <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>, 11691cb0ef41Sopenharmony_ci <span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-string">'https'</span>], 11701cb0ef41Sopenharmony_ci <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>, 11711cb0ef41Sopenharmony_ci}); 11721cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// https worker</span> 11731cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">setupPrimary</span>({ 11741cb0ef41Sopenharmony_ci <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>, 11751cb0ef41Sopenharmony_ci <span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-string">'http'</span>], 11761cb0ef41Sopenharmony_ci}); 11771cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// http worker</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:cluster'</span>); 11781cb0ef41Sopenharmony_ci 11791cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">setupPrimary</span>({ 11801cb0ef41Sopenharmony_ci <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>, 11811cb0ef41Sopenharmony_ci <span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-string">'https'</span>], 11821cb0ef41Sopenharmony_ci <span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>, 11831cb0ef41Sopenharmony_ci}); 11841cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// https worker</span> 11851cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">setupPrimary</span>({ 11861cb0ef41Sopenharmony_ci <span class="hljs-attr">exec</span>: <span class="hljs-string">'worker.js'</span>, 11871cb0ef41Sopenharmony_ci <span class="hljs-attr">args</span>: [<span class="hljs-string">'--use'</span>, <span class="hljs-string">'http'</span>], 11881cb0ef41Sopenharmony_ci}); 11891cb0ef41Sopenharmony_cicluster.<span class="hljs-title function_">fork</span>(); <span class="hljs-comment">// http worker</span></code><button class="copy-button">copy</button></pre> 11901cb0ef41Sopenharmony_ci<p>This can only be called from the primary process.</p> 11911cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.worker</code><span><a class="mark" href="#clusterworker" id="clusterworker">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_worker"></a></h3> 11921cb0ef41Sopenharmony_ci<div class="api_metadata"> 11931cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 11941cb0ef41Sopenharmony_ci</div> 11951cb0ef41Sopenharmony_ci<ul> 11961cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 11971cb0ef41Sopenharmony_ci</ul> 11981cb0ef41Sopenharmony_ci<p>A reference to the current worker object. Not available in the primary process.</p> 11991cb0ef41Sopenharmony_ci 12001cb0ef41Sopenharmony_ci<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> cluster <span class="hljs-keyword">from</span> <span class="hljs-string">'node:cluster'</span>; 12011cb0ef41Sopenharmony_ci 12021cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 12031cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'I am primary'</span>); 12041cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 12051cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 12061cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isWorker</span>) { 12071cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`I am worker #<span class="hljs-subst">${cluster.worker.id}</span>`</span>); 12081cb0ef41Sopenharmony_ci}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:cluster'</span>); 12091cb0ef41Sopenharmony_ci 12101cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isPrimary</span>) { 12111cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'I am primary'</span>); 12121cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 12131cb0ef41Sopenharmony_ci cluster.<span class="hljs-title function_">fork</span>(); 12141cb0ef41Sopenharmony_ci} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (cluster.<span class="hljs-property">isWorker</span>) { 12151cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`I am worker #<span class="hljs-subst">${cluster.worker.id}</span>`</span>); 12161cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre> 12171cb0ef41Sopenharmony_ci</section><section><h3><code>cluster.workers</code><span><a class="mark" href="#clusterworkers" id="clusterworkers">#</a></span><a aria-hidden="true" class="legacy" id="cluster_cluster_workers"></a></h3> 12181cb0ef41Sopenharmony_ci<div class="api_metadata"> 12191cb0ef41Sopenharmony_ci<span>Added in: v0.7.0</span> 12201cb0ef41Sopenharmony_ci</div> 12211cb0ef41Sopenharmony_ci<ul> 12221cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 12231cb0ef41Sopenharmony_ci</ul> 12241cb0ef41Sopenharmony_ci<p>A hash that stores the active worker objects, keyed by <code>id</code> field. This makes it 12251cb0ef41Sopenharmony_cieasy to loop through all the workers. It is only available in the primary 12261cb0ef41Sopenharmony_ciprocess.</p> 12271cb0ef41Sopenharmony_ci<p>A worker is removed from <code>cluster.workers</code> after the worker has disconnected 12281cb0ef41Sopenharmony_ci<em>and</em> exited. The order between these two events cannot be determined in 12291cb0ef41Sopenharmony_ciadvance. However, it is guaranteed that the removal from the <code>cluster.workers</code> 12301cb0ef41Sopenharmony_cilist happens before the last <code>'disconnect'</code> or <code>'exit'</code> event is emitted.</p> 12311cb0ef41Sopenharmony_ci 12321cb0ef41Sopenharmony_ci<pre class="with-40-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> cluster <span class="hljs-keyword">from</span> <span class="hljs-string">'node:cluster'</span>; 12331cb0ef41Sopenharmony_ci 12341cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> worker <span class="hljs-keyword">of</span> <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">values</span>(cluster.<span class="hljs-property">workers</span>)) { 12351cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'big announcement to all workers'</span>); 12361cb0ef41Sopenharmony_ci}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> cluster = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:cluster'</span>); 12371cb0ef41Sopenharmony_ci 12381cb0ef41Sopenharmony_ci<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> worker <span class="hljs-keyword">of</span> <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">values</span>(cluster.<span class="hljs-property">workers</span>)) { 12391cb0ef41Sopenharmony_ci worker.<span class="hljs-title function_">send</span>(<span class="hljs-string">'big announcement to all workers'</span>); 12401cb0ef41Sopenharmony_ci}</code><button class="copy-button">copy</button></pre></section> 12411cb0ef41Sopenharmony_ci <!-- API END --> 12421cb0ef41Sopenharmony_ci </div> 12431cb0ef41Sopenharmony_ci </div> 12441cb0ef41Sopenharmony_ci </div> 12451cb0ef41Sopenharmony_ci</body> 12461cb0ef41Sopenharmony_ci</html> 1247