Skip to content

Commit

Permalink
Ability to do foreground runner
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Nov 14, 2024
1 parent 97d4a38 commit fc2f548
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
60 changes: 53 additions & 7 deletions src/controllers/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,62 @@ export async function runConfigInBackgroundSingleton(payload) {
}).unref();
}

export async function runConfigInForeground(payload) {
return new Promise((resolve, reject) => {
let child = spawn('node', [path.join(process.cwd(), '/runner.js')], {
stdio: 'pipe',
env: {
RUNNER_PAYLOAD: JSON.stringify(payload),
}
})

let stdoutData = '';

// Collect stdout data
child.stdout.on('data', (data) => {
stdoutData += data.toString();
});

// Collect stderr data
child.stderr.on('data', (data) => {
stdoutData += data.toString();
});

// Handle process exit
child.on('exit', (exitCode) => {
if (exitCode !== 0) {
reject(new Error(`Process exited with code ${exitCode}\n${stdoutData}`));
} else {
resolve(stdoutData.trim()); // Resolve with collected stdout
}
});

// Handle spawn errors
child.on('error', (error) => {
reject(error);
});
})
}

export default function () {
var router = express.Router();
router.post('/', checkGet(['domain']), async function (req, res, next) {
runConfigInBackgroundSingleton({
body: req.body,
domain: req.query.domain + "",
sandbox: !!parseInt(req.query.sandbox + '' || '0'),
callback: req.header('x-callback'),
});
res.send('OK');
const callback = req.header('x-callback');
if (callback) {
runConfigInBackgroundSingleton({
body: req.body,
domain: req.query.domain + "",
sandbox: !!parseInt(req.query.sandbox + '' || '0'),
callback: req.header('x-callback'),
});
res.send('OK');
} else {
res.send(await runConfigInForeground({
body: req.body,
domain: req.query.domain + "",
sandbox: !!parseInt(req.query.sandbox + '' || '0'),
}));
}
});
return router;
}
Expand Down
2 changes: 1 addition & 1 deletion src/executor/virtualmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class VirtualminExecutor {
return new Promise((resolve, reject) => {
spawnSudoUtil('VIRTUAL_SERVER_GET', [id]).then(() => {
const fileConf = cat(tmpFile);
const config = cat(tmpFile).trimEnd().split("\n");
const config = fileConf.trimEnd().split("\n");
for (const [key, value] of Object.entries(props)) {
let i = config.findIndex(x => x.startsWith(key + '='));
if (i >= 0) {
Expand Down

0 comments on commit fc2f548

Please sign in to comment.