Execution Hooks

Attach custom behavior to workflow execution without changing logic. Add logging, metrics, or tracing at key lifecycle moments.

Overview

Hooks let you observe and extend workflow execution. They're perfect for:

  • Logging task starts, completions, and failures
  • Collecting metrics on execution time and costs
  • Distributed tracing for debugging
  • Custom error handling and recovery

Using the Default Hook

Mosaic provides a default hook that logs execution events:

hooks-default.ts

Creating Custom Hooks

Create your own hooks to add custom behavior:

hooks-custom.ts

Combining Multiple Hooks

Use ClusterHook to combine multiple hooks:

hooks-cluster.ts

Tip

Hooks execute in order. Place logging hooks first so you can see events even if later hooks throw errors.

Hook Lifecycle Events

onWorkflowStart

Called when a workflow begins execution. Useful for initializing resources.

onTaskStart

Called before each task executes. Track task starts, set up context.

onTaskComplete

Called when a task completes successfully. Record metrics, log results.

onTaskError

Called when a task fails. Handle errors, trigger alerts, implement retry logic.

onWorkflowComplete

Called when the entire workflow finishes. Clean up resources, summarize results.

Complete Example: Logging Hook

logging-hook.ts

Continue Learning