From 653956e939734c508cf689129344bb059c026487 Mon Sep 17 00:00:00 2001 From: Miles Zimmerman Date: Sun, 1 Oct 2023 14:50:16 -0700 Subject: [PATCH] fix(job): make ctx and env binding to Job instances to use in handle() --- packages/superflare/src/job.ts | 3 +++ packages/superflare/src/queue.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/superflare/src/job.ts b/packages/superflare/src/job.ts index a8312ba..7792590 100644 --- a/packages/superflare/src/job.ts +++ b/packages/superflare/src/job.ts @@ -8,6 +8,9 @@ export abstract class Job { */ queue = "default"; + env: any; + ctx: any; + constructor() {} abstract handle(): Promise; diff --git a/packages/superflare/src/queue.ts b/packages/superflare/src/queue.ts index de2e15c..91008b9 100644 --- a/packages/superflare/src/queue.ts +++ b/packages/superflare/src/queue.ts @@ -28,16 +28,20 @@ export async function handleQueue( config({ env, ctx }); return await Promise.all( - batch.messages.map((message) => handleQueueMessage(message, ctx)) + batch.messages.map((message) => handleQueueMessage(message, ctx, env)) ); } -async function handleQueueMessage(message: Message, ctx: ExecutionContext) { +async function handleQueueMessage(message: Message, ctx: ExecutionContext, env: Env) { const instance = await hydrateInstanceFromQueuePayload( message.body as MessagePayload ); if (instance instanceof Job) { + // Set context and Env so its available to the job. + instance.ctx = ctx; + instance.env = env; + // invoke handler await instance.handle(); return; }