Can I combine sleep() with hooks for non-blocking scheduling?

HEAD
74aea7bAdd DurableAgent compat tests, e2e tests, and migrate to AI SDK v6 (#1362)
This post might have stale content, as HEAD is 861 commits ahead.

can i combine/race a sleep() with a hook? or like, imagine an "infinite workflow" pattern in which, with some hooks, i can schedule work for later (but not blocking the "hook loop")

Avatar of julianbenegas
Julian Benegas
commented

so cool. does calling a step in the sleep().then() work as expected and will get awaited?

like

void sleep(10_000).then(async () => { await someLongRunningStep() })

Avatar of julianbenegas
Julian Benegas
commented

interesting.

what would happen on something like this

export async function myWorkflow() { "use workflow";

using hook = createHook({ token: "my-worker" }); const queue = []

for await (const command of hook) { queue.push(command)

while (queue.length > 0) {
  const event = queue.shift()
  switch () {
    ...
    // imagine in some case, we update the queue
    // but after the hook is "settled" and the queue is at 0
    // does the new queue item get processed?
      void sleep('1d').then(() => {
         queue.push({something})
      });
  }
}

}

return "done"; }

Avatar of julianbenegas
Julian Benegas
commented

yeah like your self resuming pattern would work if that step got awaited. alternatively, we could start() a sibling workflow that sleeps and resumes the loop


END OF POST

Can I combine sleep() with hooks for non-blocking scheduling? — vercel/workflow