child_process#fork JavaScript Examples

The following examples show how to use child_process#fork. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: app.js    From Adachi-BOT with MIT License 6 votes vote down vote up
function runBot() {
  log(m_LOG_TYPE.INFO, `正在从 ${mBot.path} 拉起${mBot.name}。`);

  mBot.process = fork(mBot.path);
  mBot.process.on("exit", () => {
    if (false === mPostRunning) {
      log(m_LOG_TYPE.ERROR, `${mBot.name}异常退出!`);
      quit();
    }
  });
}
Example #2
Source File: app.js    From Adachi-BOT with MIT License 6 votes vote down vote up
function runServer(port = 9934) {
  log(m_LOG_TYPE.INFO, `正在从 ${mServer.path} 拉起${mServer.name}。`);

  mServer.process = fork(mServer.path, ["-p", port.toString()]);
  mServer.process.on("exit", () => {
    if (false === mPostRunning) {
      log(m_LOG_TYPE.ERROR, `${mServer.name}异常退出!`);
      runServer(port);
    }
  });
}
Example #3
Source File: create-flush.js    From medusa with MIT License 6 votes vote down vote up
function createFlush(enabled) {
  if (!enabled) {
    return
  }

  return async function flush() {
    if (isTruthy(MEDUSA_TELEMETRY_VERBOSE)) {
      console.log("Flushing queue...")
    }

    const forked = fork(join(__dirname, `send.js`), {
      detached: true,
      stdio: MEDUSA_TELEMETRY_VERBOSE ? `inherit` : `ignore`,
      execArgv: [],
    })
    forked.unref()
  }
}
Example #4
Source File: fork.js    From fes.js with MIT License 5 votes vote down vote up
export default function start({
    scriptPath
}) {
    const execArgv = process.execArgv.slice(0);
    const inspectArgvIndex = execArgv.findIndex(argv => argv.includes('--inspect-brk'),);

    if (inspectArgvIndex > -1) {
        const inspectArgv = execArgv[inspectArgvIndex];
        execArgv.splice(
            inspectArgvIndex,
            1,
            inspectArgv.replace(/--inspect-brk=(.*)/, (match, s1) => {
                let port;
                try {
                    port = parseInt(s1, 10) + 1;
                } catch (e) {
                    port = 9230; // node default inspect port plus 1.
                }
                if (usedPorts.includes(port)) {
                    port += 1;
                }
                usedPorts.push(port);
                return `--inspect-brk=${port}`;
            }),
        );
    }

    // set port to env when current port has value
    if (CURRENT_PORT) {
        // @ts-ignore
        process.env.PORT = CURRENT_PORT;
    }

    const child = fork(scriptPath, process.argv.slice(2), {
        execArgv
    });

    child.on('message', (data) => {
        const type = (data && data.type) || null;
        if (type === 'RESTART') {
            child.kill();
            start({
                scriptPath
            });
        } else if (type === 'UPDATE_PORT') {
            // set current used port
            CURRENT_PORT = data.port;
        }
        process.send && process.send(data);
    });

    return child;
}