MCPcopy Create free account
hub / github.com/effect-app/libs / drive

Function drive

packages/infra/src/WorkflowEngineCosmos.ts:366–439  ·  view source on GitHub ↗
(
    executionId: string,
    payload: object,
    parent: string | undefined,
    entry: Registered
  )

Source from the content-addressed store, hash-verified

364 // --- Drive logic -------------------------------------------------------
365
366 const drive = (
367 executionId: string,
368 payload: object,
369 parent: string | undefined,
370 entry: Registered
371 ): Effect.Effect<void> =>
372 Effect.gen(function*() {
373 let local = locals.get(executionId)
374 if (local?.fiber) {
375 const polled = local.fiber.pollUnsafe()
376 const stillRunning = !polled
377 const completedNotResume = polled && polled._tag === "Success" && polled.value._tag === "Complete"
378 if (stillRunning || completedNotResume) return
379 }
380
381 const stateOpt = yield* readExec(executionId)
382 if (Option.isNone(stateOpt) || stateOpt.value.status === "complete") return
383
384 // Best-effort claim: takes lease so recovery poller leaves us alone.
385 // Failure is tolerated — local fiber still drives; OCC guards persisted
386 // state so split-brain stays correct.
387 const claimed = yield* tryClaim(stateOpt.value)
388 const state = Option.isSome(claimed) ? claimed.value : stateOpt.value
389
390 const instance = WorkflowInstance.initial(entry.workflow, executionId)
391 instance.interrupted = state.interrupted
392 if (!local) {
393 local = { instance, fiber: undefined, parent }
394 locals.set(executionId, local)
395 } else {
396 local.instance = instance
397 }
398
399 const onComplete = Effect.fnUntraced(function*(result: Workflow.Result<unknown, unknown>) {
400 const current = yield* readExec(executionId)
401 if (Option.isNone(current) || current.value.status === "complete") return
402 const isComplete = result._tag === "Complete"
403 const completedResult = isComplete ? yield* encodeResult(entry.workflow, result) : undefined
404 yield* replaceExec({
405 ...current.value,
406 status: isComplete ? "complete" : current.value.status,
407 suspended: result._tag === "Suspended",
408 // Never downgrade a persisted interrupt: a concurrent `interrupt` may
409 // have set the flag while this driver was suspending. Losing it would
410 // leave a re-drive unable to collapse the suspension.
411 interrupted: instance.interrupted || current.value.interrupted,
412 completedResult,
413 // Release lease on completion so the doc isn't seen as orphaned.
414 worker: isComplete ? undefined : current.value.worker,
415 leaseExpiresAt: isComplete ? undefined : current.value.leaseExpiresAt
416 })
417 .pipe(Effect.catchTag("OptimisticConcurrencyException", () => Effect.void))
418 if (parent && isComplete) {
419 yield* Effect.forkIn(driveById(parent), scope)
420 }
421 })
422
423 local.fiber = yield* entry.execute(payload, executionId).pipe(

Callers 2

driveByIdFunction · 0.70

Calls 13

pollUnsafeMethod · 0.80
initialMethod · 0.80
readExecFunction · 0.70
tryClaimFunction · 0.70
encodeResultFunction · 0.70
replaceExecFunction · 0.70
driveByIdFunction · 0.70
heartbeatFunction · 0.70
getMethod · 0.65
setMethod · 0.65
pipeMethod · 0.65
executeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…