Improve service handler

This commit is contained in:
Logan Cusano
2024-03-24 02:46:16 -04:00
parent fea7ed2c7f
commit 7983a45281

View File

@@ -1,52 +1,58 @@
import { exec } from 'child_process'; import { exec } from 'child_process';
/** /**
* Starts the given service from the command line. * Executes a system command with error handling.
* @param {string} serviceName The service name to be started. Ex: ... start "{serviceName}.service" * @param {string} command The command to execute.
* @returns {Promise<void>} * @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) => { return new Promise((resolve, reject) => {
exec(`sudo systemctl start ${serviceName}.service`, (error, stdout, stderr) => { exec(command, (error, stdout, stderr) => {
if (error) { if (error) {
reject(error); console.error(`Command failed with error: ${error.message}`);
resolve({ stdout, stderr });
} else { } else {
resolve(); resolve({ stdout, stderr });
} }
}); });
}); });
}; };
/**
* Starts the given service from the command line.
* @param {string} serviceName The service name to be started.
* @returns {Promise<void>}
*/
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. * 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<void>} * @returns {Promise<void>}
*/ */
export const restartService = async (serviceName) => { export const restartService = async (serviceName) => {
return new Promise((resolve, reject) => { try {
exec(`sudo systemctl restart ${serviceName}.service`, (error, stdout, stderr) => { await executeCommand(`sudo systemctl restart ${serviceName}.service`);
if (error) { } catch (error) {
reject(error); console.error(`Failed to restart service: ${error.message}`);
} else { }
resolve();
}
});
});
}; };
/** /**
* Stops the given service from the command line. * 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<void>} * @returns {Promise<void>}
*/ */
export const stopService = async (serviceName) => { export const stopService = async (serviceName) => {
return new Promise((resolve, reject) => { try {
exec(`sudo systemctl stop ${serviceName}.service`, (error, stdout, stderr) => { await executeCommand(`sudo systemctl stop ${serviceName}.service`);
if (error) { } catch (error) {
reject(error); console.error(`Failed to stop service: ${error.message}`);
} else { }
resolve(); };
}
});
});
};