marbemac
marbemac
CDCloudflare Developers
Created by Diogo Ferreira on 10/29/2024 in #workflows
waitForEvent
ok great thank ya
6 replies
CDCloudflare Developers
Created by Diogo Ferreira on 10/29/2024 in #workflows
waitForEvent
One more thought - would using pause/resume work as expected? Not as elegant as purely event based like Inngest, but similar outcome and probably less of a lift to fit into Cloudflare's world (since events as a concept is not baked into cloudflare atm as far as I know).
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
// do work...

// is pause method available here?
this.pause();

const approved = xyz; // get state from wherever
if (!approved) {
return;
}

// continue work...
}
};
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
// do work...

// is pause method available here?
this.pause();

const approved = xyz; // get state from wherever
if (!approved) {
return;
}

// continue work...
}
};
Separately, whenever the approval or "event" occurs...
const instance = await env.MY_WORKFLOW.get(instanceId);
await instance.resume();
const instance = await env.MY_WORKFLOW.get(instanceId);
await instance.resume();
6 replies
CDCloudflare Developers
Created by Diogo Ferreira on 10/29/2024 in #workflows
waitForEvent
ah yes a poll could work - something like this?
let waiting = true;
let waitCount = 0;

while (waiting) {
await step.sleep(`sleep for a bit ${waitCount}`, "10 seconds");

waitCount++;
const approved = await env.KV.get('approval-123');
if (approved) {
waiting = false;
}
}
let waiting = true;
let waitCount = 0;

while (waiting) {
await step.sleep(`sleep for a bit ${waitCount}`, "10 seconds");

waitCount++;
const approved = await env.KV.get('approval-123');
if (approved) {
waiting = false;
}
}
@Diogo Ferreira is it OK to include that dynamic waitCount in the sleep step name? Is it even necessary for sleep steps for the name to be unique - perhaps can drop waitCount altogether? Assuming rules of workflow steps don't all apply to sleep steps like they do to regular steps (assuming sleep steps are not cached.. or are they and then skipped if workflow fails and re-runs?)
6 replies