Skip to content

feat(workflows): make run dispatch durable and fail loudly - #256

Merged
artempartos merged 1 commit into
developfrom
feat/durable-workflow-run-dispatch
Sep 12, 2026
Merged

artempartos merged 1 commit into
developfrom
feat/durable-workflow-run-dispatch

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Follow-up to #253 / #255, from the same incident: the queue is sold as always available, so a
stall has to be loud and the work has to survive it.

The hole

WorkflowService.start committed the run, then dispatched it to Temporal with an inline RPC
whose result nobody read:

TemporalWorkflowRegistry.start_workflow_execution(run)   # return value discarded
record_activity(run, :workflow_started)
...
rescue StandardError => e
  Rails.logger.error("[WorkflowService] Failed to start workflow for run ##{run&.id}: #{e.message}")
  run
end

TemporalService.start_workflow does not raise on a failed RPC — it answers { ok: false, ... }.
So a Temporal outage never even reached that rescue: it produced a committed run, a
workflow_started activity, and a success response. The rescue caught the other half
(ActiveRecord, NoMethodError) and also returned the run as if started.

Nothing re-drove it:

mechanism covers
CleanupStaleRunsActivity only running / paused past 4h — pending is out of scope
SessionAdmissionReconciler does not look at runs
OutboxRelay trigger events — the hop before this one

A run created while Temporal was unreachable stayed pending forever, and looked exactly like one
the worker had simply not picked up yet.

The change

Loud. dispatch! reads the result and raises WorkflowService::DispatchFailed. The caller
gets an exception and Sentry an event.

A deployment that has switched Temporal off is not a failure and does not raise — it has no
executor to strand a run from, and no relay either, since the relay is itself a Temporal schedule.
The check is on TemporalService.enabled?, the deployment's own switch, not on the shape of the
error string. Development and test keep working exactly as before.

Durable. The run is enrolled in the outbox in the same write that creates it
(relay_state: "pending"), and WorkflowRunRelay re-drives anything still undispatched past
WorkflowRun::RELAY_GRACE, in the same minutely outbox_relay_workflow tick as the event relay —
events first, since draining them is what creates runs.

The two halves need each other. Loud without durable turns an outage into lost work; durable
without loud is the silence this incident was made of.

Idempotent. start_workflow_execution now passes reject_duplicate: true. The execution id is
per-run, so a duplicate start can only mean this run's execution already exists — reported as
success, which is how a re-drive of a run Temporal did receive settles. It also forbids reusing
the id after the execution closed, so a finished run can never be silently re-run.

The relay ignores runs the worker has already advanced (nothing to do) and runs cancelled while
undispatched (must never be started), and gives up after RELAY_MAX_ATTEMPTS so one poison run
cannot own every sweep.

Consequence worth naming

A user whose run fails to dispatch now sees an error instead of a run page — and the run still
starts within the grace window. That is the intended trade: the alternative is what we had, where
nobody saw anything at all.

Schema

db/schema.rb was edited by hand (columns + partial index, alphabetical, version bumped) rather
than dumped from a local database, per the local-dump hazards. SchemaParityTest replays the
migrations into a throwaway database and diffs, so CI is the arbiter of whether the hand edit
matches what Rails renders.

🤖 Generated with Claude Code

A WorkflowRun was committed and then handed to Temporal by an inline RPC
whose result nobody read. TemporalService.start_workflow does not raise on
a failed RPC — it returns { ok: false, ... } — and WorkflowService.start
discarded that, recorded a `workflow_started` activity and returned the
run as if it had started. A `rescue StandardError` underneath logged one
line and returned the same run, so the only two ways a dispatch could fail
both ended in a success response.

Nothing re-drove such a run. The stale-run reaper only touches
`running`/`paused` runs past four hours, the admission reconciler does not
look at runs at all, and the existing outbox covers trigger events — the
hop before this one. A run created while Temporal was unreachable sat in
`pending` forever, indistinguishable from one the worker had not picked up
yet.

Two changes, and they need each other:

* Loud. dispatch! reads the result and raises DispatchFailed, so the
  caller gets an exception and Sentry an event. A deployment that has
  switched Temporal off is not a failure and does not raise: it has no
  executor to strand a run from, and no relay either, since the relay is
  itself a Temporal schedule.

* Durable. The run is enrolled in the outbox in the same write that
  creates it, and WorkflowRunRelay re-drives anything still undispatched
  past the grace window, beside the existing event relay on the same
  minutely cron. So a Temporal outage costs a delay, not the work.

Re-dispatch is safe because the execution id is per-run and duplicates are
now rejected, with a rejected duplicate reported as success — which also
means a finished run can never be silently re-run.

The relay deliberately ignores runs the worker has already advanced and
runs cancelled while undispatched: the first needs nothing, the second
must never be started.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit bc0e9c3 into develop Sep 12, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant