feat: revamp abort run route
This commit is contained in:
@@ -891,31 +891,57 @@ router.post('/runs/abort/:id', requireSignIn, async (req: AuthenticatedRequest,
|
|||||||
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,
|
|
||||||
} });
|
|
||||||
|
|
||||||
if (!run) {
|
if (!run) {
|
||||||
return res.status(404).send(false);
|
return res.status(404).send({ error: 'Run not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!['running', 'queued'].includes(run.status)) {
|
||||||
|
return res.status(400).send({
|
||||||
|
error: `Cannot abort run with status: ${run.status}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const isQueued = run.status === 'queued';
|
||||||
|
|
||||||
|
await run.update({
|
||||||
|
status: 'aborting'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (isQueued) {
|
||||||
|
await run.update({
|
||||||
|
status: 'aborted',
|
||||||
|
finishedAt: new Date().toLocaleString(),
|
||||||
|
log: 'Run aborted while queued'
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.send({
|
||||||
|
success: true,
|
||||||
|
message: 'Queued run aborted',
|
||||||
|
isQueued: true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const userQueueName = `abort-run-user-${req.user.id}`;
|
const userQueueName = `abort-run-user-${req.user.id}`;
|
||||||
await pgBoss.createQueue(userQueueName);
|
await pgBoss.createQueue(userQueueName);
|
||||||
|
|
||||||
await pgBoss.send(userQueueName, {
|
const jobId = await pgBoss.send(userQueueName, {
|
||||||
userId: req.user.id,
|
userId: req.user.id,
|
||||||
runId: req.params.id
|
runId: req.params.id
|
||||||
});
|
});
|
||||||
|
|
||||||
await run.update({
|
logger.log('info', `Abort signal sent for run ${req.params.id}, job ID: ${jobId}`);
|
||||||
status: 'aborting'
|
|
||||||
});
|
return res.send({
|
||||||
|
success: true,
|
||||||
return res.send(true);
|
message: 'Abort signal sent',
|
||||||
|
jobId,
|
||||||
|
isQueued: false
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const { message } = e as Error;
|
const { message } = e as Error;
|
||||||
logger.log('info', `Error while aborting run with id: ${req.params.id} - ${message}`);
|
logger.log('error', `Error aborting run ${req.params.id}: ${message}`);
|
||||||
return res.send(false);
|
return res.status(500).send({ error: 'Failed to abort run' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user