From 7983a45281e4ba99727d0f40eef1637fc35e8ad9 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 24 Mar 2024 02:46:16 -0400 Subject: [PATCH] Improve service handler --- client/modules/serviceHandler.mjs | 62 +++++++++++++++++-------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/client/modules/serviceHandler.mjs b/client/modules/serviceHandler.mjs index 5a714e7..505aa1d 100644 --- a/client/modules/serviceHandler.mjs +++ b/client/modules/serviceHandler.mjs @@ -1,52 +1,58 @@ import { exec } from 'child_process'; /** - * Starts the given service from the command line. - * @param {string} serviceName The service name to be started. Ex: ... start "{serviceName}.service" - * @returns {Promise} + * Executes a system command with error handling. + * @param {string} command The command to execute. + * @returns {Promise<{ stdout: string, stderr: string }>} A promise resolving to an object containing stdout and stderr. */ -export const startService = async (serviceName) => { +const executeCommand = (command) => { return new Promise((resolve, reject) => { - exec(`sudo systemctl start ${serviceName}.service`, (error, stdout, stderr) => { + exec(command, (error, stdout, stderr) => { if (error) { - reject(error); + console.error(`Command failed with error: ${error.message}`); + resolve({ stdout, stderr }); } else { - resolve(); + resolve({ stdout, stderr }); } }); }); }; +/** + * Starts the given service from the command line. + * @param {string} serviceName The service name to be started. + * @returns {Promise} + */ +export const startService = async (serviceName) => { + try { + await executeCommand(`sudo systemctl start ${serviceName}.service`); + } catch (error) { + console.error(`Failed to start service: ${error.message}`); + } +}; + /** * Restarts the given service from the command line. - * @param {string} serviceName The service name to be restarted. Ex: ... restart "{serviceName}.service" + * @param {string} serviceName The service name to be restarted. * @returns {Promise} */ export const restartService = async (serviceName) => { - return new Promise((resolve, reject) => { - exec(`sudo systemctl restart ${serviceName}.service`, (error, stdout, stderr) => { - if (error) { - reject(error); - } else { - resolve(); - } - }); - }); + try { + await executeCommand(`sudo systemctl restart ${serviceName}.service`); + } catch (error) { + console.error(`Failed to restart service: ${error.message}`); + } }; /** * Stops the given service from the command line. - * @param {string} serviceName The service name to be stopped. Ex: ... stop "{serviceName}.service" + * @param {string} serviceName The service name to be stopped. * @returns {Promise} */ export const stopService = async (serviceName) => { - return new Promise((resolve, reject) => { - exec(`sudo systemctl stop ${serviceName}.service`, (error, stdout, stderr) => { - if (error) { - reject(error); - } else { - resolve(); - } - }); - }); -}; \ No newline at end of file + try { + await executeCommand(`sudo systemctl stop ${serviceName}.service`); + } catch (error) { + console.error(`Failed to stop service: ${error.message}`); + } +};