shell: make shell.sudo promise based and waitable

This commit is contained in:
Girish Ramakrishnan
2025-07-16 21:53:22 +02:00
parent 32d07e7959
commit 989d843fcb
15 changed files with 86 additions and 64 deletions
+3 -3
View File
@@ -172,7 +172,7 @@ async function startTask(id, options) {
if (constants.TEST) sudoOptions.logStream = fs.createWriteStream('/dev/null'); // without this output is messed up, not sure why
const p = new Promise((resolve, reject) => {
gTasks[id] = shell.sudo([ START_TASK_CMD, id, logFile, options.nice || 0, options.memoryLimit || 400, options.oomScoreAdjust || 0 ], sudoOptions, async function (sudoError) {
gTasks[id] = shell.sudoCallback([ START_TASK_CMD, id, logFile, options.nice || 0, options.memoryLimit || 400, options.oomScoreAdjust || 0 ], sudoOptions, async function (sudoError) {
const code = sudoError ? sudoError.code : 0;
debug(`startTask: ${id} completed with code ${code}. ignored: ${!gTasks[id]}`);
@@ -233,7 +233,7 @@ async function stopTask(id) {
debug(`stopTask: stopping task ${id}`);
await shell.promises.sudo([ STOP_TASK_CMD, id, ], {});
await shell.sudo([ STOP_TASK_CMD, id, ], {});
}
async function stopAllTasks() {
@@ -241,7 +241,7 @@ async function stopAllTasks() {
const cps = Object.values(gTasks);
gTasks = {}; // this signals startTask() to not set completion status as "crashed"
cps.forEach(cp => { debug(`stopAllTasks: terminating process group ${cp.pid}`); cp.terminate(); }); // cleanup all the sudos and systemd-run
const [error] = await safe(shell.promises.sudo([ STOP_TASK_CMD, 'all' ], { cwd: paths.baseDir() }));
const [error] = await safe(shell.sudo([ STOP_TASK_CMD, 'all' ], { cwd: paths.baseDir() }));
if (error) debug(`stopAllTasks: error stopping stasks: ${error.message}`);
}