UNPKG

pot-js

Version:

Process management module

166 lines (122 loc) 4.68 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.writePid = exports.killPid = exports.getPids = exports.getPidFile = exports.removePidFile = undefined; let getPidFile = exports.getPidFile = (() => { var _ref4 = _asyncToGenerator(function* (keyOrMonitor) { const key = (0, _getKey2.default)(keyOrMonitor); const runDir = yield _workspace2.default.getRunDir(); return (0, _path.join)(runDir, `${key}.pid`); }); return function getPidFile(_x4) { return _ref4.apply(this, arguments); }; })(); let getPids = exports.getPids = (() => { var _ref5 = _asyncToGenerator(function* () { const runDir = yield _workspace2.default.getRunDir(); // DEPRECATED: workspace.DEPRECATED_getPidsDir() const deprecatedPidsDir = yield _workspace2.default.DEPRECATED_getPidsDir(); const patterns = [`${runDir}/*.pid`, `${deprecatedPidsDir}/*.pid`]; const pidFiles = yield (0, _globby2.default)(patterns, { absolute: true }); const pids = yield Promise.all(pidFiles.map(parsePidFile)); return pids.filter(Boolean); }); return function getPids() { return _ref5.apply(this, arguments); }; })(); let killPid = exports.killPid = (() => { var _ref6 = _asyncToGenerator(function* (key, ppid) { let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; try { const shouldLog = options.shouldLog; yield (0, _fkill2.default)(ppid, { force: _isWin2.default }); _potLogger.logger.trace(`killed pid ${ppid}`); shouldLog && _potLogger.logger.info(`"${key}" stopped`); return true; } catch (err) { _potLogger.logger.error(`Stop "${key}" failed.`); _potLogger.logger.debug(err); return false; } }); return function killPid(_x6, _x7) { return _ref6.apply(this, arguments); }; })(); let writePid = exports.writePid = (() => { var _ref8 = _asyncToGenerator(function* (_ref7) { let pidFile = _ref7.pidFile, pid = _ref7.pid; _potLogger.logger.trace('pid file saved in', _chalk2.default.gray(pidFile)); yield (0, _fsExtra.writeFile)(pidFile, pid).catch(function (err) { return _potLogger.logger.debug(err); }); }); return function writePid(_x8) { return _ref8.apply(this, arguments); }; })(); var _processExists = require('process-exists'); var _processExists2 = _interopRequireDefault(_processExists); var _fsExtra = require('fs-extra'); var _workspace = require('./workspace'); var _workspace2 = _interopRequireDefault(_workspace); var _path = require('path'); var _lodash = require('lodash'); var _globby = require('globby'); var _globby2 = _interopRequireDefault(_globby); var _potLogger = require('pot-logger'); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _fkill = require('fkill'); var _fkill2 = _interopRequireDefault(_fkill); var _isWin = require('./isWin'); var _isWin2 = _interopRequireDefault(_isWin); var _getKey = require('./getKey'); var _getKey2 = _interopRequireDefault(_getKey); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } const removePidFile = (() => { var _ref = _asyncToGenerator(function* (pidFile) { _potLogger.logger.trace('remove pid file', pidFile); return (0, _fsExtra.remove)(pidFile).catch(_lodash.noop); }); function removePidFile(_x) { return _ref.apply(this, arguments); } return removePidFile; })(); const getPid = (() => { var _ref2 = _asyncToGenerator(function* (pidFile) { try { const pid = +(0, _lodash.trim)((yield (0, _fsExtra.readFile)(pidFile, 'utf-8'))); const isProcessExists = yield (0, _processExists2.default)(pid); if (!isProcessExists) { removePidFile(pidFile); } return isProcessExists && pid; } catch (err) { return false; } }); function getPid(_x2) { return _ref2.apply(this, arguments); } return getPid; })(); const parsePidFile = (() => { var _ref3 = _asyncToGenerator(function* (pidFile) { const pid = yield getPid(pidFile); const key = (0, _path.basename)(pidFile, '.pid'); if (!pid) return false; return { key, pidFile }; }); function parsePidFile(_x3) { return _ref3.apply(this, arguments); } return parsePidFile; })(); exports.removePidFile = removePidFile;