feat: add abort run job to pgboss queue
This commit is contained in:
@@ -904,42 +904,32 @@ router.delete('/schedule/:id', requireSignIn, async (req: AuthenticatedRequest,
|
|||||||
router.post('/runs/abort/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
router.post('/runs/abort/:id', requireSignIn, async (req: AuthenticatedRequest, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.user) { return res.status(401).send({ error: 'Unauthorized' }); }
|
if (!req.user) { return res.status(401).send({ error: 'Unauthorized' }); }
|
||||||
const run = await Run.findOne({ where: {
|
|
||||||
|
const run = await Run.findOne({ where: {
|
||||||
runId: req.params.id,
|
runId: req.params.id,
|
||||||
runByUserId: req.user.id,
|
runByUserId: req.user.id,
|
||||||
} });
|
} });
|
||||||
|
|
||||||
if (!run) {
|
if (!run) {
|
||||||
return res.status(404).send(false);
|
return res.status(404).send(false);
|
||||||
}
|
}
|
||||||
const plainRun = run.toJSON();
|
|
||||||
|
|
||||||
const browser = browserPool.getRemoteBrowser(plainRun.browserId);
|
const userQueueName = `abort-run-user-${req.user.id}`;
|
||||||
const currentLog = browser?.interpreter.debugMessages.join('/n');
|
await pgBoss.createQueue(userQueueName);
|
||||||
const serializableOutput = browser?.interpreter.serializableData.reduce((reducedObject, item, index) => {
|
|
||||||
return {
|
await pgBoss.send(userQueueName, {
|
||||||
[`item-${index}`]: item,
|
userId: req.user.id,
|
||||||
...reducedObject,
|
runId: req.params.id
|
||||||
}
|
|
||||||
}, {});
|
|
||||||
const binaryOutput = browser?.interpreter.binaryData.reduce((reducedObject, item, index) => {
|
|
||||||
return {
|
|
||||||
[`item-${index}`]: item,
|
|
||||||
...reducedObject,
|
|
||||||
}
|
|
||||||
}, {});
|
|
||||||
await run.update({
|
|
||||||
...run,
|
|
||||||
status: 'aborted',
|
|
||||||
finishedAt: new Date().toLocaleString(),
|
|
||||||
browserId: plainRun.browserId,
|
|
||||||
log: currentLog,
|
|
||||||
serializableOutput,
|
|
||||||
binaryOutput,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await run.update({
|
||||||
|
status: 'aborting'
|
||||||
|
});
|
||||||
|
|
||||||
return res.send(true);
|
return res.send(true);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const { message } = e as Error;
|
const { message } = e as Error;
|
||||||
logger.log('info', `Error while running a robot with name: ${req.params.fileName}_${req.params.runId}.json`);
|
logger.log('info', `Error while aborting run with id: ${req.params.id} - ${message}`);
|
||||||
return res.send(false);
|
return res.send(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user