UNPKG

@imqueue/rpc

Version:

RPC-like client-service implementation over messaging queue

85 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IMQ_PID_DIR = exports.IMQ_TMP_DIR = exports.SIGNALS = void 0; exports.pid = pid; exports.forgetPid = forgetPid; /*! * IMQ-RPC helpers: pid, forgetPid * * Copyright (c) 2018, imqueue.com <support@imqueue.com> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ const path = require("path"); const fs = require("fs"); exports.SIGNALS = [ 'SIGTERM', 'SIGINT', 'SIGHUP', 'SIGQUIT', ]; exports.IMQ_TMP_DIR = process.env.TMPDIR || // istanbul ignore next '/tmp'; exports.IMQ_PID_DIR = path.resolve(exports.IMQ_TMP_DIR, '.imq-rpc'); /** * Returns increment-based process identifier for a given name * * @param {string} name - name of a service to create pid file for * @param {string} path - directory to * @returns {number} */ function pid(name, // istanbul ignore next path = exports.IMQ_PID_DIR) { const pidFile = `${path}/${name}`; const pidOpts = { encoding: 'utf8', flag: 'wx' }; if (!fs.existsSync(path)) { fs.mkdirSync(path); } let id = 0; let done = false; while (!done) { try { fs.writeFileSync(`${pidFile}-${id}.pid`, process.pid + '', pidOpts); done = true; } catch (err) { // istanbul ignore next if (err.code === 'EEXIST') { id++; } else { throw err; } } } return id; } /** * Removes pid file for a given name and id * * @param {string} name * @param {number} id * @param {ILogger} logger * @param {string} [path] */ function forgetPid(name, id, logger, // istanbul ignore next path = exports.IMQ_PID_DIR) { try { fs.unlinkSync(`${path}/${name}-${id}.pid`); } catch (err) { /* ignore */ } } //# sourceMappingURL=pid.js.map