| 315 | * @category Constructors |
| 316 | */ |
| 317 | export const makeUnsafe = (options: Encoded): WorkflowEngine["Type"] => |
| 318 | WorkflowEngine.of({ |
| 319 | register: Effect.fnUntraced(function*(workflow, execute) { |
| 320 | const context = yield* Effect.context<WorkflowEngine>() |
| 321 | yield* options.register(workflow, (payload, executionId) => |
| 322 | Effect.suspend(() => |
| 323 | execute(payload, executionId) |
| 324 | ).pipe( |
| 325 | Effect.mapInputContext( |
| 326 | (input) => Context.merge(context, input) as Context.Context<any> |
| 327 | ) |
| 328 | )) |
| 329 | }), |
| 330 | execute: Effect.fnUntraced(function*< |
| 331 | Name extends string, |
| 332 | Payload extends Workflow.AnyStructSchema, |
| 333 | Success extends Schema.Schema.Any, |
| 334 | Error extends Schema.Schema.All, |
| 335 | const Discard extends boolean = false |
| 336 | >( |
| 337 | self: Workflow.Workflow<Name, Payload, Success, Error>, |
| 338 | opts: { |
| 339 | readonly executionId: string |
| 340 | readonly payload: Payload["Type"] |
| 341 | readonly discard?: Discard | undefined |
| 342 | readonly suspendedRetrySchedule?: |
| 343 | | Schedule.Schedule<any, unknown> |
| 344 | | undefined |
| 345 | } |
| 346 | ) { |
| 347 | const payload = opts.payload |
| 348 | const executionId = opts.executionId |
| 349 | const suspendedRetrySchedule = opts.suspendedRetrySchedule ?? defaultRetrySchedule |
| 350 | yield* Effect.annotateCurrentSpan({ executionId }) |
| 351 | let result: Workflow.Result<Success["Type"], Error["Type"]> | undefined |
| 352 | |
| 353 | // link interruption with parent workflow |
| 354 | const parentInstance = yield* Effect.serviceOption(WorkflowInstance) |
| 355 | if (Option.isSome(parentInstance)) { |
| 356 | const instance = parentInstance.value |
| 357 | yield* Effect.addFinalizer(() => { |
| 358 | if (!instance.interrupted || result?._tag === "Complete") { |
| 359 | return Effect.void |
| 360 | } |
| 361 | return options.interrupt(self, executionId) |
| 362 | }) |
| 363 | } |
| 364 | |
| 365 | if (opts.discard) { |
| 366 | yield* options.execute(self, { |
| 367 | executionId, |
| 368 | payload: payload as object, |
| 369 | discard: true, |
| 370 | parent: Option.getOrUndefined(parentInstance) |
| 371 | }) |
| 372 | return executionId |
| 373 | } |
| 374 | |