0.1.2 • Published 6 years ago

first-officer v0.1.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

First Officer

In multi-processing scenario, it's better to handle some operations in just one process, rather than repeatedly running them in all processes. If you you have all control of the master process, you can just put the code in the master process. However, you don't have control, e.g. using PM2, you need a way to find out which process should run the specific program. Such a process, I call it the First Officer process.

The module provides two functions, one gets the pid of the first officer process, one checking whether the current process is the first officer process.

Example

const FirstOfficer = require("first-officer");

(async () => {
    let pid = await FirstOfficer.getPid();
    let isFirstOfficer = await FirstOfficer.isFirstOfficer();
    let members = await FirstOfficer.getCrew();

    if (isFirstOfficer) {
        // TODO...
    }

    // This is the same
    if (pid === process.pid) {
        // TODO...
    }

    console.log(members);
    // Array<{
    //  pid: number,
    //  ppid?: number,
    //  uid?: number;
    //  gid?: number;
    //  name: string;
    //  cmd: string;
    // }>
})();