UNPKG

vrem

Version:

An open-source automatic time-tracker

91 lines (90 loc) 3.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.serverScriptPath = exports.serverProcessName = exports.trackerScriptPath = exports.trackerProcessName = exports.stopProcess = exports.startProcess = exports.isProcessAlive = void 0; const ipc_1 = require("./ipc"); const child_process_1 = require("child_process"); const path_1 = __importDefault(require("path")); const colors_1 = __importDefault(require("./colors")); const sleep = time => new Promise(resolve => setTimeout(resolve, time)); async function isProcessAlive(socketPath, attempts = 3, waitTime = 150) { const check = async () => { try { //return process.kill(pid, 0); const data = await (0, ipc_1.ipcRequest)(socketPath, 'ping'); return data.name === 'vrem' /*&& data.version === version*/; } catch (e) { return false; } }; let isAlive = await check(); for (let i = 0; i < attempts && !isAlive; i++) { await sleep(waitTime); isAlive = await check(); } return isAlive; } exports.isProcessAlive = isProcessAlive; function runProcess(filePath) { const proc = (0, child_process_1.fork)(path_1.default.resolve(__dirname, filePath), { detached: true, stdio: 'ignore', env: { NODE_ENV: 'production', }, }); proc.unref(); proc.disconnect(); } async function _stopProcess(socketPath) { let totalTime = 0; const timeLimit = 2000; const step = 500; await (0, ipc_1.ipcRequest)(socketPath, 'exit'); while (await isProcessAlive(socketPath)) { if (totalTime > timeLimit) return false; totalTime += step; await sleep(step); } return true; } async function startProcess(processName, filePath, socketPath) { if (await isProcessAlive(socketPath)) { console.info(colors_1.default.cyan(`The ${processName} process is already running.`)); return true; } console.info(`Starting the ${processName} process...`); runProcess(filePath); // it takes about 1 second to start the server process first time if (await isProcessAlive(socketPath, 8, 250)) { console.info(colors_1.default.green(`The ${processName} process has been started.`)); return true; } else { console.info(colors_1.default.red(`Failed to start the ${processName} process.`)); return false; } } exports.startProcess = startProcess; async function stopProcess(processName, socketPath) { if (await isProcessAlive(socketPath)) { console.info(`Stopping the ${processName} process...`); if (await _stopProcess(socketPath)) { console.info(colors_1.default.yellow(`The ${processName} process has been stopped.`)); } else { console.info(colors_1.default.red(`Cannot stop the ${processName} process. Remove it manually.`)); } return; } console.info(colors_1.default.yellow(`The ${processName} process has been already stopped.`)); } exports.stopProcess = stopProcess; exports.trackerProcessName = 'auto-tracker'; exports.trackerScriptPath = path_1.default.resolve(__dirname, './tracker/tracker'); exports.serverProcessName = 'server'; exports.serverScriptPath = path_1.default.resolve(__dirname, './server/server');