Render adoption reports in worker
This commit is contained in:
@@ -3,6 +3,12 @@ import { Worker } from 'bullmq';
|
||||
import { ensureSchema } from './db/schema.js';
|
||||
import { db } from './db/client.js';
|
||||
import { runBirdMilestoneReminders, startBirdMilestoneReminderScheduler } from './app.js';
|
||||
import {
|
||||
adoptionReportQueueName,
|
||||
closeAdoptionReportQueue,
|
||||
type AdoptionReportJobData,
|
||||
type AdoptionReportJobResult,
|
||||
} from './queues/adoptionReportQueue.js';
|
||||
import {
|
||||
birdMilestoneReminderQueueName,
|
||||
closeBirdMilestoneReminderQueue,
|
||||
@@ -10,8 +16,10 @@ import {
|
||||
type BirdMilestoneReminderJobResult,
|
||||
} from './queues/birdMilestoneReminderQueue.js';
|
||||
import { redisConnection } from './queues/redisConnection.js';
|
||||
import { renderAdoptionReportForBird } from './reports/adoptionReportJob.js';
|
||||
|
||||
let birdMilestoneWorker: Worker<BirdMilestoneReminderJobData, BirdMilestoneReminderJobResult> | null = null;
|
||||
let adoptionReportWorker: Worker<AdoptionReportJobData, AdoptionReportJobResult> | null = null;
|
||||
|
||||
const startWorker = async () => {
|
||||
await ensureSchema();
|
||||
@@ -35,6 +43,25 @@ const startWorker = async () => {
|
||||
console.error(`Bird milestone reminder job failed: id=${job?.id ?? 'unknown'}`, error);
|
||||
});
|
||||
|
||||
adoptionReportWorker = new Worker<AdoptionReportJobData, AdoptionReportJobResult>(
|
||||
adoptionReportQueueName,
|
||||
async (job) => {
|
||||
const pdf = await renderAdoptionReportForBird(job.data);
|
||||
console.log(`Adoption report job completed: id=${job.id ?? 'unknown'}, birdId=${job.data.birdId}, bytes=${pdf.length}`);
|
||||
return {
|
||||
pdfBase64: pdf.toString('base64'),
|
||||
};
|
||||
},
|
||||
{
|
||||
connection: redisConnection,
|
||||
concurrency: 1,
|
||||
},
|
||||
);
|
||||
|
||||
adoptionReportWorker.on('failed', (job, error) => {
|
||||
console.error(`Adoption report job failed: id=${job?.id ?? 'unknown'}, birdId=${job?.data.birdId ?? 'unknown'}`, error);
|
||||
});
|
||||
|
||||
startBirdMilestoneReminderScheduler();
|
||||
console.log('FlockPal worker started.');
|
||||
};
|
||||
@@ -42,7 +69,9 @@ const startWorker = async () => {
|
||||
const shutdown = async (signal: string) => {
|
||||
console.log(`FlockPal worker received ${signal}; shutting down.`);
|
||||
await birdMilestoneWorker?.close();
|
||||
await adoptionReportWorker?.close();
|
||||
await closeBirdMilestoneReminderQueue();
|
||||
await closeAdoptionReportQueue();
|
||||
await db.close();
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user