Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const taskConfigs = await getAllStelaceTasks()
const filteredTaskConfigs = filterTasks(taskConfigs, refDate, nbMinutes)
fetchEventsTransaction.end()
fetchEventsTransaction = null // set null to prevent stopping a second time in the finally block
for (let i = 0; i < filteredTaskConfigs.length; i++) {
const taskConfig = filteredTaskConfigs[i]
const { platformId, env, task } = taskConfig
const emitEventTransaction = apm.startTransaction('Emit task event via cron')
apm.setUserContext({ id: platformId })
apm.addLabels({ env, platformId, eventType: task.eventType })
apm.setCustomContext({ taskId: task.id })
try {
// use redlock to ensure the cron process is handled only by one server at a time
// even within a distributed system
const lockResource = `locks:stelace_tasks:${task.id}_${refDate}`
const lock = await redlock.lock(lockResource, lockTtl)
const alreadyExecuted = await didStelaceTaskExecute({ taskId: task.id, executionDate: refDate })
if (!alreadyExecuted) {
await addStelaceTaskExecutionDate({ taskId: task.id, executionDate: refDate })
await emitTaskEvent({ platformId, env, task })
}
await lock.unlock()
} catch (err) {
} catch (err) {
// Sending error when response is sent
ctx.res.on('finish', () => {
apm.captureError(err);
debug('Sent error to APM server');
});
throw err;
} finally {
// Set custom context data
const reqId = ctx.state.reqId
|| ctx.reqId
|| ctx.req.id
|| ctx.get('X-Request-Id');
const custom = { reqId };
apm.setCustomContext(custom);
// Set user context data
apm.setUserContext(
serializeUser(ctx.state.user)
);
}
};
};