Two-Phase Job Pattern

Overview

Pulse supports a two-phase job pattern for complex workflows that need to spawn child tasks and aggregate results.

Job Phases

Phase 1: Ingest

Phase 2: Aggregate

Implementation

Jobs track their phase using JobMetadata:

type JobMetadata struct {
    Phase string `json:"phase,omitempty"` // "ingest" or "aggregate"
    // Other metadata fields as needed
}

Phase Detection

if job.Metadata != nil && job.Metadata.Phase == "aggregate" {
    // Aggregate phase logic
} else {
    // Ingest phase logic (default)
}

Parent-Child Relationships

Jobs maintain parent-child relationships through:

Example Workflow

1. Parent job starts (ingest phase)
   ↓
2. Creates N child jobs
   ↓
3. Parent pauses/waits
   ↓
4. Children complete
   ↓
5. Parent resumes (aggregate phase)
   ↓
6. Parent aggregates results
   ↓
7. Parent completes

Use Cases

Configuration

No special configuration required. The pattern is implemented through job handler logic and JobMetadata.

Best Practices

  1. Always set phase explicitly in JobMetadata when using this pattern
  2. Track child job IDs in parent payload for monitoring
  3. Handle partial failures - some children may fail
  4. Set reasonable timeouts for aggregate phase
  5. Use retry logic appropriately for each phase

Related Documentation