/** * @since 4.0.0 */ import type { NonEmptyReadonlyArray } from "../../Array.ts" import * as Cause from "../../Cause.ts" import * as Context from "../../Context.ts" import * as Effect from "../../Effect.ts" import * as Effectable from "../../Effectable.ts" import { dual } from "../../Function.ts" import * as Schedule from "../../Schedule.ts" import * as Schema from "../../Schema.ts" import type { Scope } from "../../Scope.ts" import type * as Types from "../../Types.ts" import * as DurableDeferred from "./DurableDeferred.ts" import { makeHashDigest } from "./internal/crypto.ts" import * as Workflow from "./Workflow.ts" import type { WorkflowEngine, WorkflowInstance } from "./WorkflowEngine.ts" const TypeId = "~effect/workflow/Activity" /** * @since 4.0.0 * @category Models */ export interface Activity< Success extends Schema.Top = Schema.Void, Error extends Schema.Top = Schema.Never, R = never > extends Effect.Effect< Success["Type"], Error["Type"], Success["DecodingServices"] | Error["DecodingServices"] | R | WorkflowEngine | WorkflowInstance > { readonly [TypeId]: typeof TypeId readonly name: string readonly successSchema: Success readonly errorSchema: Error readonly exitSchema: Schema.Exit readonly annotations: Context.Context annotate( key: Context.Key, value: S ): Activity annotateMerge( annotations: Context.Context ): Activity readonly execute: Effect.Effect< Success["Type"], Error["Type"], | Success["DecodingServices"] | Success["EncodingServices"] | Error["DecodingServices"] | Error["EncodingServices"] | R | Scope | WorkflowEngine | WorkflowInstance > readonly executeEncoded: Effect.Effect< unknown, unknown, | Success["DecodingServices"] | Success["EncodingServices"] | Error["DecodingServices"] | Error["EncodingServices"] | R | Scope | WorkflowEngine | WorkflowInstance > } /** * @since 4.0.0 * @category Models */ export interface Any { readonly [TypeId]: typeof TypeId readonly name: string readonly executeEncoded: Effect.Effect readonly annotations: Context.Context } /** * @since 4.0.0 * @category Models */ export interface AnyWithProps { readonly [TypeId]: typeof TypeId readonly name: string readonly successSchema: Schema.Top readonly errorSchema: Schema.Top readonly executeEncoded: Effect.Effect } /** * @since 4.0.0 * @category Constructors */ export const make = < R, Success extends Schema.Top = Schema.Void, Error extends Schema.Top = Schema.Never >(options: { readonly name: string readonly success?: Success | undefined readonly error?: Error | undefined readonly execute: Effect.Effect readonly interruptRetryPolicy?: Schedule.Schedule> | undefined readonly annotations?: Context.Context | undefined }): Activity> => { const successSchema = options.success ?? (Schema.Void as any as Success) const errorSchema = options.error ?? (Schema.Never as any as Error) const successSchemaJson = Schema.toCodecJson(successSchema) const errorSchemaJson = Schema.toCodecJson(errorSchema) // oxlint-disable-next-line prefer-const let execute!: Effect.Effect const executeWithoutInterrupt = retryOnInterrupt( options.name, options.interruptRetryPolicy )(options.execute) const self: Activity> = { ...Effectable.Prototype>({ label: "Activity", evaluate(_) { return execute } }), [TypeId]: TypeId, name: options.name, successSchema, errorSchema, exitSchema: Schema.Exit(successSchemaJson, errorSchemaJson, Schema.Defect), annotations: options.annotations ?? Context.empty(), annotate(tag: Context.Key, value: any) { return make({ ...options, annotations: Context.add(self.annotations, tag, value) }) }, annotateMerge(context: Context.Context) { return make({ ...options, annotations: Context.merge(self.annotations, context) }) }, execute: executeWithoutInterrupt, executeEncoded: Effect.matchEffect(executeWithoutInterrupt, { onFailure: (error) => Effect.flatMap(Effect.orDie(Schema.encodeEffect(errorSchemaJson)(error)), Effect.fail), onSuccess: (value) => Effect.orDie(Schema.encodeEffect(successSchemaJson)(value)) }) } as any execute = makeExecute(self) return self } const interruptRetryPolicy = Schedule.exponential(4.0, 1.5).pipe( Schedule.either(Schedule.spaced("10 seconds")), Schedule.either(Schedule.recurs(10)), Schedule.satisfiesInputType>(), Schedule.while((meta) => Effect.succeed(Cause.hasInterrupts(meta.input))) ) const retryOnInterrupt = ( name: string, policy: Schedule.Schedule> = interruptRetryPolicy ) => (effect: Effect.Effect): Effect.Effect => effect.pipe( Effect.sandbox, Effect.retry(policy), Effect.catch((cause) => { if (!Cause.hasInterrupts(cause)) return Effect.failCause(cause) return Effect.die(`Activity "${name}" interrupted and retry attempts exhausted`) }) ) /** * @since 4.0.0 * @category Error handling */ export const retry: { /** * @since 4.0.0 * @category Error handling */ , "schedule">, O>>(options: O): (self: Effect.Effect) => Effect.Retry.Return /** * @since 4.0.0 * @category Error handling */ , "schedule">, O>>(self: Effect.Effect, options: O): Effect.Retry.Return } = dual( 2, (effect: Effect.Effect, options: {}) => Effect.suspend(() => { let attempt = 1 return Effect.suspend(() => Effect.provideService(effect, CurrentAttempt, attempt++)).pipe(Effect.retry(options)) }) ) /** * @since 4.0.0 * @category Attempts */ export const CurrentAttempt = Context.Reference( "effect/workflow/Activity/CurrentAttempt", { defaultValue: () => 1 } ) /** * @since 4.0.0 * @category Idempotency */ export const idempotencyKey: ( name: string, options?: { readonly includeAttempt?: boolean | undefined } | undefined ) => Effect.Effect = Effect.fnUntraced(function*(name: string, options?: { readonly includeAttempt?: boolean | undefined }) { const instance = yield* InstanceTag let key = `${instance.executionId}` if (options?.includeAttempt) { const attempt = yield* CurrentAttempt key += `-${attempt}` } key += `-${name}` return yield* makeHashDigest(key) }) /** * @since 4.0.0 * @category Racing */ export const raceAll = >( name: string, activities: Activities ): Effect.Effect< Activities[number] extends Activity ? _A["Type"] : never, Activities[number] extends Activity ? _E["Type"] : never, | (Activities[number] extends Activity ? Success["DecodingServices"] | Error["DecodingServices"] | R : never) | WorkflowEngine | WorkflowInstance > => DurableDeferred.raceAll({ name: `Activity/${name}`, success: Schema.Union( activities.map((activity) => (activity as any).successSchema) ), error: Schema.Union( activities.map((activity) => (activity as any).errorSchema) ), effects: activities.map((activity) => (activity as any).asEffect()) as any }) as any // ----------------------------------------------------------------------------- // internal // ----------------------------------------------------------------------------- const EngineTag = Context.Service( "effect/workflow/WorkflowEngine" satisfies typeof WorkflowEngine.key ) const InstanceTag = Context.Service( "effect/workflow/WorkflowEngine/WorkflowInstance" satisfies typeof WorkflowInstance.key ) const makeExecute = Effect.fnUntraced(function*< R, Success extends Schema.Top = typeof Schema.Void, Error extends Schema.Top = typeof Schema.Never >(activity: Activity) { const engine = yield* EngineTag const instance = yield* InstanceTag const attempt = yield* CurrentAttempt yield* Effect.annotateCurrentSpan({ executionId: instance.executionId }) const result = yield* Workflow.wrapActivityResult( engine.activityExecute(activity, attempt), (_) => _._tag === "Suspended" ) if (result._tag === "Suspended") { return yield* Workflow.suspend(instance) } return yield* result.exit }, (effect, activity) => Effect.withSpan(effect, activity.name, { captureStackTrace: false }))