Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(job): make ctx and env binding to Job instances to use in handle() #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/superflare/src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export abstract class Job {
*/
queue = "default";

env: any;
ctx: any;

constructor() {}

abstract handle(): Promise<void>;
Expand Down
8 changes: 6 additions & 2 deletions packages/superflare/src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ export async function handleQueue<Env>(
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<Env>(message: Message, ctx: ExecutionContext, env: Env) {
const instance = await hydrateInstanceFromQueuePayload(
message.body as MessagePayload
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to pass ctx and env here as second or third args, since that function is responsible for hydrating an instance. (even if we only end up assigning it for Job instances). Thoughts?

);

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;
}
Expand Down