durable-execution - v0.32.0
    Preparing search index...

    Type Alias ParentTaskOptions<TInput, TRunOutput, TOutput, TFinalizeTaskRunOutput>

    ParentTaskOptions: CommonTaskOptions & {
        runParent: (
            ctx: TaskRunContext,
            input: TInput,
        ) =>
            | { output: TRunOutput; children?: ReadonlyArray<ChildTask> }
            | Promise<{ output: TRunOutput; children?: ReadonlyArray<ChildTask> }>
            | Effect.Effect<
                { output: TRunOutput; children?: ReadonlyArray<ChildTask> },
                unknown,
                never,
            >;
        finalize?:
            | (
                (
                    input: DefaultParentTaskOutput<TRunOutput>,
                ) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, unknown, never>
            )
            | FinalizeTaskOptions<TRunOutput, TOutput, TFinalizeTaskRunOutput>;
    }

    Options for a parent task that can be run using a durable executor. It is similar to TaskOptions but it returns children tasks to be run in parallel after the run function completes, along with the output of the parent task.

    The runParent function is similar to the run function in TaskOptions, but the output is of the form { output: TRunOutput, children: ReadonlyArray<ChildTask> } where the children are the tasks to be run in parallel after the run function completes.

    The finalize function or task is run after the runParent function and all the children tasks finish. It is useful for combining the output of the runParent function and children tasks. It is called even if the children tasks fail. Its input has the following properties:

    • output: The output of the runParent function
    • children: The finished children task executions (includes both successful and failed children)

    Important: The finalize function or task receives outputs from ALL children, including those that have failed. This behaves similar to Promise.allSettled() - you get the results regardless of individual child success or failure. This allows you to implement custom error handling logic.

    If finalize is provided, the output of the whole task is the output of the finalize function or task. If it is not provided, the output of the whole task is of the form { output: TRunOutput, children: ReadonlyArray<FinishedChildTaskExecution> }.

    See the task examples section for more details on creating tasks.

    Type Parameters

    Type Declaration

    • runParent: (
          ctx: TaskRunContext,
          input: TInput,
      ) =>
          | { output: TRunOutput; children?: ReadonlyArray<ChildTask> }
          | Promise<{ output: TRunOutput; children?: ReadonlyArray<ChildTask> }>
          | Effect.Effect<
              { output: TRunOutput; children?: ReadonlyArray<ChildTask> },
              unknown,
              never,
          >

      The task run logic. It is similar to the run function in TaskOptions but it returns the output and children tasks to be run in parallel after the run function completes.

    • Optionalfinalize?:
          | (
              (
                  input: DefaultParentTaskOutput<TRunOutput>,
              ) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, unknown, never>
          )
          | FinalizeTaskOptions<TRunOutput, TOutput, TFinalizeTaskRunOutput>

      Function or task to run after the runParent function and children tasks finish. This is useful for combining the output of the run function and children tasks. It is called even if the children tasks fail.

      If it is a function, it is called without any durability guarantees and retries. It should not be a long running function.

    const extractFileTitle = executor
    .inputSchema(Schema.Struct({ filePath: Schema.String }))
    .task({
    id: 'extractFileTitle',
    timeoutMs: 30_000, // 30 seconds
    run: async (ctx, input) => {
    // ... extract the file title
    return {
    title: 'File Title',
    }
    },
    })

    const summarizeFile = executor
    .validateInput(async (input: { filePath: string }) => {
    // Example validation function - implement your own validation logic
    if (!isValidFilePath(input.filePath)) {
    throw new Error('Invalid file path')
    }
    return {
    filePath: input.filePath,
    }
    })
    .task({
    id: 'summarizeFile',
    timeoutMs: 30_000, // 30 seconds
    run: async (ctx, input) => {
    // ... summarize the file
    return {
    summary: 'File summary',
    }
    },
    })

    const uploadFile = executor
    .inputSchema(Schema.Struct({ filePath: Schema.String, uploadUrl: Schema.String }))
    .parentTask({
    id: 'uploadFile',
    timeoutMs: 60_000, // 1 minute
    runParent: async (ctx, input) => {
    // ... upload file to the given uploadUrl
    // Extract the file title and summarize the file in parallel
    return {
    output: {
    filePath: input.filePath,
    uploadUrl: input.uploadUrl,
    fileSize: 100,
    },
    children: [
    childTask(extractFileTitle, { filePath: input.filePath }),
    childTask(summarizeFile, { filePath: input.filePath }),
    ],
    }
    },
    finalize: {
    id: 'uploadFileFinalize',
    timeoutMs: 60_000, // 1 minute
    run: async (ctx, { output, children }) => {
    // ... combine the output of the run function and children tasks
    return {
    filePath: output.filePath,
    uploadUrl: output.uploadUrl,
    fileSize: 100,
    title: 'File Title',
    summary: 'File summary',
    }
    }
    },
    })