This commit is contained in:
@@ -6,8 +6,10 @@ import {
|
||||
|
||||
type Env = {
|
||||
GUIDE_JOB_URL: string;
|
||||
NOTIFICATION_JOB_URL: string;
|
||||
GUIDE_JOB_SECRET: string;
|
||||
RECOMMENDATION_WORKFLOW: Workflow;
|
||||
NOTIFICATION_WORKFLOW: Workflow;
|
||||
};
|
||||
|
||||
export class RecommendationWorkflow extends WorkflowEntrypoint<Env> {
|
||||
@@ -32,11 +34,38 @@ export class RecommendationWorkflow extends WorkflowEntrypoint<Env> {
|
||||
}
|
||||
}
|
||||
|
||||
export class NotificationWorkflow extends WorkflowEntrypoint<Env> {
|
||||
async run(_event: WorkflowEvent<unknown>, step: WorkflowStep) {
|
||||
return step.do(
|
||||
"send due expiry notifications",
|
||||
{
|
||||
retries: { limit: 3, delay: "2 minutes", backoff: "exponential" },
|
||||
timeout: "5 minutes",
|
||||
},
|
||||
async () => {
|
||||
const response = await fetch(this.env.NOTIFICATION_JOB_URL, {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${this.env.GUIDE_JOB_SECRET}` },
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Notification endpoint failed (${response.status})`);
|
||||
}
|
||||
return response.json<{ checked: number; sent: number }>();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
fetch() {
|
||||
return Response.json({ service: "hotfridge-recommendation-workflow" });
|
||||
},
|
||||
async scheduled(_controller: ScheduledController, env: Env, ctx: ExecutionContext) {
|
||||
ctx.waitUntil(env.RECOMMENDATION_WORKFLOW.create({ params: {} }));
|
||||
ctx.waitUntil(
|
||||
Promise.all([
|
||||
env.RECOMMENDATION_WORKFLOW.create({ params: {} }),
|
||||
env.NOTIFICATION_WORKFLOW.create({ params: {} }),
|
||||
]),
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user