42 行
923 B
JavaScript
42 行
923 B
JavaScript
const cron = require("node-cron");
|
|
|
|
const { runIntegrationOutboxOnce } = require("./integrationOutboxJob");
|
|
|
|
function startIntegrationOutboxWorker(fastify) {
|
|
let running = false;
|
|
|
|
const task = cron.schedule(fastify.config.integrationOutboxCron, async () => {
|
|
if (running) {
|
|
return;
|
|
}
|
|
|
|
running = true;
|
|
try {
|
|
await runIntegrationOutboxOnce(fastify.prisma, fastify.config, fastify.log);
|
|
} catch (error) {
|
|
fastify.log.error({ err: error }, "Integration outbox worker tick failed");
|
|
} finally {
|
|
running = false;
|
|
}
|
|
});
|
|
|
|
fastify.addHook("onClose", async () => {
|
|
task.stop();
|
|
task.destroy();
|
|
});
|
|
|
|
fastify.log.info(
|
|
{
|
|
cron: fastify.config.integrationOutboxCron,
|
|
batchSize: fastify.config.integrationOutboxBatchSize,
|
|
},
|
|
"Integration outbox worker scheduled",
|
|
);
|
|
|
|
return task;
|
|
}
|
|
|
|
module.exports = {
|
|
startIntegrationOutboxWorker,
|
|
};
|