| 392 | }, |
| 393 | |
| 394 | activity(request: Entity.Request<any>) { |
| 395 | const payload = request.payload as { name: string; attempt: number } |
| 396 | const activityId = `${executionId}/${payload.name}` |
| 397 | const instance = WorkflowEngine.WorkflowInstance.initial(workflow, executionId) |
| 398 | interruptedActivities.delete(activityId) |
| 399 | return Effect.gen(function*() { |
| 400 | let entry = activities.get(activityId) |
| 401 | while (!entry) { |
| 402 | const latch = Latch.makeUnsafe() |
| 403 | activityLatches.set(activityId, latch) |
| 404 | yield* latch.await |
| 405 | entry = activities.get(activityId) |
| 406 | } |
| 407 | const context = entry.context.pipe( |
| 408 | Context.add(WorkflowEngine.WorkflowInstance, instance), |
| 409 | Context.add(Activity.CurrentAttempt, payload.attempt) |
| 410 | ) |
| 411 | return yield* entry.activity.executeEncoded.pipe( |
| 412 | Effect.provideContext(context) |
| 413 | ) |
| 414 | }).pipe( |
| 415 | Workflow.intoResult, |
| 416 | Effect.catchCause((cause) => { |
| 417 | // we only want to store interrupts as suspends when the |
| 418 | // client requested it |
| 419 | const suspend = cause.reasons.some((f) => |
| 420 | f._tag === "Interrupt" && f.annotations.has(ClientAbort.key) |
| 421 | ) |
| 422 | if (suspend) { |
| 423 | interruptedActivities.add(activityId) |
| 424 | return Effect.succeed(new Workflow.Suspended({})) |
| 425 | } |
| 426 | return Effect.failCause(cause) |
| 427 | }), |
| 428 | Effect.provideService(WorkflowEngine.WorkflowInstance, instance), |
| 429 | Effect.provideService(Activity.CurrentAttempt, payload.attempt), |
| 430 | Effect.ensuring(Effect.sync(() => { |
| 431 | activities.delete(activityId) |
| 432 | })), |
| 433 | Rpc.wrap({ fork: true, uninterruptible: true }) |
| 434 | ) |
| 435 | }, |
| 436 | |
| 437 | deferred: Effect.fnUntraced(function*(request: Entity.Request<any>) { |
| 438 | const payload = request.payload as any |