22 * @since 1.0.0
33 */
44import * as Rpc from "@effect/rpc/Rpc"
5+ import * as RpcServer from "@effect/rpc/RpcServer"
56import { DurableDeferred } from "@effect/workflow"
67import * as Activity from "@effect/workflow/Activity"
78import * as DurableClock from "@effect/workflow/DurableClock"
89import * as Workflow from "@effect/workflow/Workflow"
910import { WorkflowEngine , WorkflowInstance } from "@effect/workflow/WorkflowEngine"
1011import * as Arr from "effect/Array"
12+ import * as Cause from "effect/Cause"
1113import * as Context from "effect/Context"
1214import * as DateTime from "effect/DateTime"
1315import * as Duration from "effect/Duration"
1416import * as Effect from "effect/Effect"
1517import type * as Exit from "effect/Exit"
1618import * as Fiber from "effect/Fiber"
19+ import * as FiberId from "effect/FiberId"
1720import * as Layer from "effect/Layer"
1821import * as Option from "effect/Option"
1922import type * as ParseResult from "effect/ParseResult"
2023import * as PrimaryKey from "effect/PrimaryKey"
2124import * as RcMap from "effect/RcMap"
22- import * as Record from "effect/Record"
25+ import type * as Record from "effect/Record"
2326import * as Runtime from "effect/Runtime"
2427import * as Schedule from "effect/Schedule"
2528import * as Schema from "effect/Schema"
@@ -190,16 +193,7 @@ export const make = Effect.gen(function*() {
190193 times : 3 ,
191194 schedule : Schedule . exponential ( 250 )
192195 } ) ,
193- Effect . orDie ,
194- ( effect , { activity, attempt, executionId } ) =>
195- Effect . withSpan ( effect , "WorkflowEngine.resetActivityAttempt" , {
196- captureStackTrace : false ,
197- attributes : {
198- name : activity . name ,
199- executionId,
200- attempt
201- }
202- } )
196+ Effect . orDie
203197 )
204198
205199 const clearClock = Effect . fnUntraced ( function * ( options : {
@@ -260,13 +254,12 @@ export const make = Effect.gen(function*() {
260254 return {
261255 run : ( request : Entity . Request < any > ) => {
262256 const instance = WorkflowInstance . initial ( workflow , executionId )
263- let payload = request . payload
257+ const payload = request . payload
264258 let parent : { workflowName : string ; executionId : string } | undefined
265259 if ( payload [ payloadParentKey ] ) {
266260 parent = payload [ payloadParentKey ]
267- payload = Record . remove ( payload , payloadParentKey )
268261 }
269- return execute ( payload , executionId ) . pipe (
262+ return execute ( workflow . payloadSchema . make ( payload ) , executionId ) . pipe (
270263 Effect . ensuring ( Effect . suspend ( ( ) => {
271264 if ( ! instance . suspended ) {
272265 return parent ? ensureSuccess ( sendResumeParent ( parent ) ) : Effect . void
@@ -291,17 +284,17 @@ export const make = Effect.gen(function*() {
291284 ) as any
292285 } ,
293286
294- activity : Effect . fnUntraced (
295- function * ( request : Entity . Request < any > ) {
296- const activityId = `${ executionId } /${ request . payload . name } `
287+ activity ( request : Entity . Request < any > ) {
288+ const activityId = `${ executionId } /${ request . payload . name } `
289+ const instance = WorkflowInstance . initial ( workflow , executionId )
290+ return Effect . gen ( function * ( ) {
297291 let entry = activities . get ( activityId )
298292 while ( ! entry ) {
299293 const latch = Effect . unsafeMakeLatch ( )
300294 activityLatches . set ( activityId , latch )
301295 yield * latch . await
302296 entry = activities . get ( activityId )
303297 }
304- const instance = WorkflowInstance . initial ( workflow , executionId )
305298 const contextMap = new Map ( entry . runtime . context . unsafeMap )
306299 contextMap . set ( Activity . CurrentAttempt . key , request . payload . attempt )
307300 contextMap . set ( WorkflowInstance . key , instance )
@@ -311,23 +304,29 @@ export const make = Effect.gen(function*() {
311304 runtimeFlags : Runtime . defaultRuntimeFlags
312305 } )
313306 return yield * entry . activity . executeEncoded . pipe (
314- Effect . interruptible ,
315- Effect . onInterrupt ( ( ) => {
316- instance . suspended = true
317- return Effect . void
318- } ) ,
319- Workflow . intoResult ,
320- Effect . provide ( runtime ) ,
321- Effect . ensuring ( Effect . sync ( ( ) => {
322- activities . delete ( activityId )
323- } ) )
307+ Effect . provide ( runtime )
324308 )
325- } ,
326- Rpc . wrap ( {
327- fork : true ,
328- uninterruptible : true
329- } )
330- ) ,
309+ } ) . pipe (
310+ Workflow . intoResult ,
311+ Effect . catchAllCause ( ( cause ) => {
312+ const interruptors = Cause . interruptors ( cause )
313+ // we only want to store explicit interrupts
314+ const ids = Array . from ( interruptors , ( id ) => Array . from ( FiberId . ids ( id ) ) ) . flat ( )
315+ const suspend = ids . includes ( RpcServer . fiberIdClientInterrupt . id ) ||
316+ ids . includes ( RpcServer . fiberIdTransientInterrupt . id )
317+ return suspend ? Effect . succeed ( new Workflow . Suspended ( ) ) : Effect . failCause ( cause )
318+ } ) ,
319+ Effect . provideService ( WorkflowInstance , instance ) ,
320+ Effect . provideService ( Activity . CurrentAttempt , request . payload . attempt ) ,
321+ Effect . ensuring ( Effect . sync ( ( ) => {
322+ activities . delete ( activityId )
323+ } ) ) ,
324+ Rpc . wrap ( {
325+ fork : true ,
326+ uninterruptible : true
327+ } )
328+ )
329+ } ,
331330
332331 deferred : Effect . fnUntraced ( function * ( request : Entity . Request < any > ) {
333332 yield * ensureSuccess ( resume ( workflow , executionId ) )
@@ -407,27 +406,10 @@ export const make = Effect.gen(function*() {
407406 times : 3 ,
408407 schedule : Schedule . exponential ( 250 )
409408 } ) ,
410- Effect . orDie ,
411- ( effect , workflow , executionId ) =>
412- Effect . withSpan ( effect , "WorkflowEngine.interrupt" , {
413- captureStackTrace : false ,
414- attributes : {
415- name : workflow . name ,
416- executionId
417- }
418- } )
409+ Effect . orDie
419410 ) ,
420411
421- resume : ( workflow , executionId ) =>
422- ensureSuccess ( resume ( workflow , executionId ) ) . pipe (
423- Effect . withSpan ( "WorkflowEngine.resume" , {
424- captureStackTrace : false ,
425- attributes : {
426- name : workflow . name ,
427- executionId
428- }
429- } )
430- ) ,
412+ resume : ( workflow , executionId ) => ensureSuccess ( resume ( workflow , executionId ) ) ,
431413
432414 activityExecute : Effect . fnUntraced (
433415 function * ( { activity, attempt } ) {
@@ -460,15 +442,7 @@ export const make = Effect.gen(function*() {
460442 return result
461443 }
462444 } ,
463- Effect . scoped ,
464- ( effect , { activity, attempt } ) =>
465- Effect . withSpan ( effect , "WorkflowEngine.activityExecute" , {
466- captureStackTrace : false ,
467- attributes : {
468- name : activity . name ,
469- attempt
470- }
471- } )
445+ Effect . scoped
472446 ) ,
473447
474448 deferredResult : ( deferred ) =>
@@ -493,13 +467,7 @@ export const make = Effect.gen(function*() {
493467 times : 3 ,
494468 schedule : Schedule . exponential ( 250 )
495469 } ) ,
496- Effect . orDie ,
497- Effect . withSpan ( "WorkflowEngine.deferredResult" , {
498- captureStackTrace : false ,
499- attributes : {
500- name : deferred . name
501- }
502- } )
470+ Effect . orDie
503471 ) ,
504472
505473 deferredDone : Effect . fnUntraced (
@@ -512,15 +480,7 @@ export const make = Effect.gen(function*() {
512480 } , { discard : true } )
513481 )
514482 } ,
515- Effect . scoped ,
516- ( effect , { deferredName, executionId } ) =>
517- Effect . withSpan ( effect , "WorkflowEngine.deferredDone" , {
518- captureStackTrace : false ,
519- attributes : {
520- name : deferredName ,
521- executionId
522- }
523- } )
483+ Effect . scoped
524484 ) ,
525485
526486 scheduleClock ( options ) {
0 commit comments