feat: add waitForRun(runId) API (#144)#151
Conversation
) Add waitForRun() method to Durably interface that waits for an existing run to reach a terminal state without creating a new run. Extract shared waitForRunCompletion helper from triggerAndWait, adding run:cancel handling and NotFoundError support to both APIs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughDurably インターフェースに Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client
participant Durably as Durably
participant Storage as Storage
participant EventEmitter as EventEmitter
Client->>Durably: waitForRun(runId, options?)
Durably->>Storage: get run status
alt run not found
Storage-->>Durably: null
Durably-->>Client: Reject NotFoundError
else run completed
Storage-->>Durably: {status: 'completed', output}
Durably-->>Client: Resolve completed run
else in-flight
Durably->>EventEmitter: subscribe (run:complete/run:fail/run:cancel)
opt callbacks provided
Durably->>EventEmitter: subscribe (run:progress, log:write)
end
EventEmitter-->>Durably: event (progress/log/complete/fail/cancel)
alt complete
Durably-->>Client: Resolve completed run
else fail
Durably-->>Client: Reject with failure
else cancel
Durably-->>Client: Reject CancelledError
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/durably/src/job.ts`:
- Around line 265-383: The function waitForRunCompletion currently only listens
to the local eventEmitter and will miss terminal updates made by other
instances; add a fallback polling path that repeatedly calls
storage.getRun(runId) until the run reaches a terminal state
(completed/failed/cancelled) so cross-process completions are observed.
Concretely, inside waitForRunCompletion (the function shown), start a
setInterval (use a small default like 500ms or options.pollInterval if provided)
after the initial storage.getRun() check, have the interval call
storage.getRun(runId) and on each result apply the same terminal-state logic
(resolve with run, reject with Error/CancelledError/NotFoundError), ensure the
interval id is cleared in cleanup and that polling stops once resolved is true,
and keep existing eventEmitter handlers (or remove polling if a DB-backed
subscription is later added). Ensure timeout handling (options.timeout) still
cancels polling via cleanup.
In `@packages/durably/tests/shared/run-api.shared.ts`:
- Around line 693-706: The live-callback test is timing-dependent because
waitForRun() does not replay past events: move the call to d.waitForRun(run.id,
{...}) (with onProgress/onLog handlers collecting into progressUpdates/logs) to
run before d.start(), so the subscription is established before the worker is
started (the code invoking d.start() and d.jobs.job.trigger({}) should be
ordered so waitForRun is initiated prior to d.start()).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 27e651ff-1e43-45fe-be1c-8dd9de1ca0c7
📒 Files selected for processing (5)
packages/durably/src/durably.tspackages/durably/src/index.tspackages/durably/src/job.tspackages/durably/tests/node/types.test.tspackages/durably/tests/shared/run-api.shared.ts
Start waitForRun subscription before worker to ensure all live progress/log events are captured reliably. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add usage examples, WaitForRunOptions type definition, and update TriggerAndWaitOptions to show the new extends relationship. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- create-durably.md: new waitForRun() section with signature and options - define-job.md: tip comparing triggerAndWait vs waitForRun - index.md: cheat sheet example and instance methods table - server-mode.md: "Waiting for Existing Runs" guide section - config.ts: sidebar entry for waitForRun Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
waitForRun(runId, options?)method toDurablyinterface — waits for an existing run to complete without creating a new runwaitForRunCompletionhelper fromtriggerAndWait, eliminating code duplicationrun:cancelhandling to bothwaitForRunandtriggerAndWait(throwsCancelledError)NotFoundErrorfor non-existent run IDsWaitForRunOptionstype exportAPI
Test plan
CancelledErroron cancelled runNotFoundErrorfor non-existent runIdtriggerAndWaitcancel handling (new behavior via shared helper)waitForRunandWaitForRunOptionspnpm validatepasses (format, lint, typecheck, 204 tests)Closes #144
🤖 Generated with Claude Code
Summary by CodeRabbit