From c50998eb8204ae9cac4c49541ae9e60e5877841f Mon Sep 17 00:00:00 2001 From: Lightemerald Date: Mon, 26 Jun 2023 09:41:31 +0200 Subject: [PATCH] Promisified bot process --- index.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 56f0fa7..4ad8ade 100644 --- a/index.js +++ b/index.js @@ -149,9 +149,8 @@ async function startBot(accountName) { } } - try { - // execSync(`timeout 150m bash -c "${commandPrefix} ${commandSuffix}; exit"`, { stdio: 'inherit', shell: '/bin/bash' }); - const childProcess = spawn('bash', ['-c', `${commandPrefix} ${commandSuffix}`], { stdio: 'inherit' }); + return new Promise((resolve, reject) => { + const childProcess = spawn('timeout 150m bash', ['-c', `${commandPrefix} ${commandSuffix}`], { stdio: 'inherit' }); childProcess.stdout.on('data', (data) => { console.log(`[${accountName}] ${data.toString()}`); @@ -162,19 +161,25 @@ async function startBot(accountName) { } }); - childProcess.on('close', (code) => { + childProcess.on('exit', (code) => { if (code !== 0) { consoleLog(`[${host}] Bot process for ${accountName} exited with code ${code}. Restarting...`, 'error'); - startBot(accountName); + startBot(accountName).then(resolve).catch(reject); + } + else { + resolve(); } }); - } - catch (error) { - consoleLog(`[${host}] Failed to start bot for ${accountName}.`, 'error'); - } + + childProcess.on('error', (err) => { + consoleLog(`[${host}] Failed to start bot for ${accountName}.`, 'error'); + reject(err); + }); + }); } else { consoleLog(`[${host}] File ${accountPath} does not exist, skipping starting bot for this VPN!`, 'error'); + return Promise.reject(`File ${accountPath} does not exist.`); } }