(
executionId: string,
payload: object,
parent: string | undefined,
entry: Registered
)
| 500 | // --- Drive logic ------------------------------------------------------ |
| 501 | |
| 502 | const drive = ( |
| 503 | executionId: string, |
| 504 | payload: object, |
| 505 | parent: string | undefined, |
| 506 | entry: Registered |
| 507 | ): Effect.Effect<void> => |
| 508 | Effect.gen(function*() { |
| 509 | let local = locals.get(executionId) |
| 510 | if (local?.fiber) { |
| 511 | const polled = local.fiber.pollUnsafe() |
| 512 | const stillRunning = !polled |
| 513 | const completedNotResume = polled && polled._tag === "Success" && polled.value._tag === "Complete" |
| 514 | if (stillRunning || completedNotResume) return |
| 515 | } |
| 516 | |
| 517 | const stateOpt = yield* readExec(executionId) |
| 518 | if (Option.isNone(stateOpt) || stateOpt.value.status === "complete") return |
| 519 | |
| 520 | const claimed = yield* tryClaim(stateOpt.value) |
| 521 | const state = Option.isSome(claimed) ? claimed.value : stateOpt.value |
| 522 | |
| 523 | const instance = WorkflowInstance.initial(entry.workflow, executionId) |
| 524 | instance.interrupted = state.interrupted |
| 525 | if (!local) { |
| 526 | local = { instance, fiber: undefined, parent } |
| 527 | locals.set(executionId, local) |
| 528 | } else { |
| 529 | local.instance = instance |
| 530 | } |
| 531 | |
| 532 | const onComplete = Effect.fnUntraced(function*(result: Workflow.Result<unknown, unknown>) { |
| 533 | const current = yield* readExec(executionId) |
| 534 | if (Option.isNone(current) || current.value.status === "complete") return |
| 535 | const isComplete = result._tag === "Complete" |
| 536 | const completedResult = isComplete ? yield* encodeResult(entry.workflow, result) : undefined |
| 537 | yield* replaceExec(current.value, { |
| 538 | status: isComplete ? "complete" : current.value.status, |
| 539 | suspended: result._tag === "Suspended", |
| 540 | interrupted: instance.interrupted, |
| 541 | completedResult, |
| 542 | worker: isComplete ? undefined : current.value.worker, |
| 543 | leaseExpiresAt: isComplete ? undefined : current.value.leaseExpiresAt |
| 544 | }) |
| 545 | .pipe(Effect.catchTag("OptimisticConcurrencyException", () => Effect.void)) |
| 546 | if (parent && isComplete) { |
| 547 | yield* Effect.forkIn(driveById(parent), scope) |
| 548 | } |
| 549 | }) |
| 550 | |
| 551 | local.fiber = yield* entry.execute(payload, executionId).pipe( |
| 552 | Effect.onExit(() => { |
| 553 | if (!instance.interrupted) return Effect.void |
| 554 | instance.suspended = false |
| 555 | return Effect.withFiber((fiber) => Effect.interruptible(Fiber.interrupt(fiber))) |
| 556 | }), |
| 557 | Workflow.intoResult, |
| 558 | Effect.provideService(WorkflowInstance, instance), |
| 559 | Effect.provideService(WorkflowEngine, engine), |
no test coverage detected
searching dependent graphs…