Feedback Send now and Remind run in the background (2.1.1)
- Send now and Remind answer straight away with who they're going to and queue the feedback-send task; the Feedback page refreshes to show progress. - One-off background tasks (enqueueTask in src/jobs/index.js): pg-boss with retries, or in-process after the response when the queue is off. - Reminders are claimed with the 24-hour rule, so two tasks at once never remind the same person twice. - Tests: tests/tasks.test.js, tests/feedbackPage.test.ts, e2e/feedback.spec.ts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -139,6 +139,38 @@ function isNoop(value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* One-off background tasks (2.1.1): work a request starts but shouldn't wait for, like sending
|
||||
* a feedback form to everyone. Queued in pg-boss (retried if it fails, kept across a restart);
|
||||
* without the queue they run in this process straight after the response. A task must be safe
|
||||
* to run twice.
|
||||
*/
|
||||
const TASKS = {
|
||||
'feedback-send': (data) => require('../utils/feedback').runFeedbackTask(data),
|
||||
};
|
||||
|
||||
async function runTask(name, data) {
|
||||
const started = Date.now();
|
||||
const result = await TASKS[name](data || {});
|
||||
if (!isNoop(result)) console.log(`[jobs] task ${name} done in ${Date.now() - started} ms: ${summarize(result)}`);
|
||||
return result ?? null;
|
||||
}
|
||||
|
||||
/** Starts a task in the background. Resolves once it's queued (or started), not when it's done. */
|
||||
async function enqueueTask(name, data) {
|
||||
if (!TASKS[name]) throw new Error(`Unknown task ${name}`);
|
||||
if (mode === 'queue' && boss) {
|
||||
try {
|
||||
await boss.send(name, data || {}, { retryLimit: 2, retryDelay: 30, expireInSeconds: 60 * 60 });
|
||||
return { queued: true };
|
||||
} catch (e) {
|
||||
console.warn(`[jobs] could not queue ${name}, running it here:`, e?.message || e);
|
||||
}
|
||||
}
|
||||
setImmediate(() => runTask(name, data).catch(e => console.warn(`[jobs] task ${name} failed:`, e?.message || e)));
|
||||
return { queued: false };
|
||||
}
|
||||
|
||||
async function runJob(job) {
|
||||
const started = Date.now();
|
||||
const result = await job.run();
|
||||
@@ -218,6 +250,23 @@ async function startJobs() {
|
||||
await boss.unschedule(job.name).catch(() => {});
|
||||
}
|
||||
}
|
||||
// One-off tasks: any number can wait and run (unlike the stately scheduled jobs).
|
||||
for (const name of Object.keys(TASKS)) {
|
||||
if (!existing.has(name)) await boss.createQueue(name, { policy: 'standard', retryLimit: 2, retryDelay: 30, expireInSeconds: 60 * 60 });
|
||||
await boss.work(name, async (batch) => {
|
||||
const list = Array.isArray(batch) ? batch : [batch];
|
||||
const out = [];
|
||||
for (const job of list) {
|
||||
try {
|
||||
out.push(await runTask(name, job?.data));
|
||||
} catch (e) {
|
||||
console.warn(`[jobs] task ${name} failed:`, e?.message || e);
|
||||
throw e; // recorded as failed, and retried
|
||||
}
|
||||
}
|
||||
return out.length === 1 ? out[0] : out;
|
||||
});
|
||||
}
|
||||
mode = 'queue';
|
||||
const off = JOBS.filter(j => !isEnabled(j)).map(j => j.name);
|
||||
console.log(`[jobs] Job queue started: ${enabled.map(j => j.name).join(', ')}${off.length ? ` (off: ${off.join(', ')})` : ''}.`);
|
||||
@@ -298,4 +347,4 @@ async function jobStatus(prisma) {
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { JOBS, TIMEZONE, isEnabled, cronFor, nextRun, isNoop, startJobs, stopJobs, runNow, jobStatus };
|
||||
module.exports = { JOBS, TASKS, TIMEZONE, isEnabled, cronFor, nextRun, isNoop, startJobs, stopJobs, runNow, jobStatus, enqueueTask };
|
||||
|
||||
Reference in New Issue
Block a user