Skip to main content
DeployStack Satellite implements a centralized job management system for recurring background tasks. The system provides a consistent pattern for cron-like operations with automatic error handling, execution metrics, and lifecycle management.

Architecture Overview

The job system consists of three core components:

BaseJob Abstract Class

All jobs extend BaseJob, which provides:
  • Automatic Interval Execution: Jobs run on configured intervals
  • Immediate First Run: Execute immediately on start, then follow interval
  • Error Handling: Automatic error catching with structured logging
  • Execution Metrics: Track execution count, timing, and errors
  • Lifecycle Management: Start/stop methods with state tracking

JobManager

The JobManager provides centralized control:
  • Job Registry: Register and track all jobs
  • Lifecycle Control: Start/stop all jobs or individual jobs
  • Status Monitoring: Query job statistics and execution state
  • Graceful Shutdown: Stop all jobs cleanly on satellite shutdown

Current Jobs

Creating a New Job

Add a new background job in three steps:

Step 1: Create Job File

Create src/jobs/process-health-job.ts:

Step 2: Export from Index

Add to src/jobs/index.ts:

Step 3: Register in Server

Add to src/server.ts:
That’s it! Your job will start running immediately and then execute every 2 minutes automatically.

Job Intervals

Common interval values in milliseconds:

Environment-Configurable Intervals

Make job intervals configurable:
Add to .env.example:

Jobs with Dependencies

If your job needs access to services, inject them via constructor:
Register with dependencies:

Job Lifecycle

Initialization Flow

Job Execution Flow

Monitoring and Observability

Structured Logging

All job events are logged with structured data:

Job Statistics

Query job statistics via JobManager:
Get all job statistics:

Error Handling

Automatic Error Recovery

The BaseJob class automatically handles errors:

Custom Error Handling

Add custom error handling for specific scenarios:

Timeout Protection

Add timeouts for long-running operations:

Best Practices

1. Keep Jobs Focused

Each job should have a single responsibility: Good:
Bad:

2. Choose Appropriate Intervals

  • High-frequency (30s-1m): Health checks, critical monitoring
  • Medium (5m-15m): Cleanup tasks, periodic updates
  • Low (1h+): Reports, analytics, maintenance

3. Document Job Purpose

Add clear comments explaining what the job does:

4. Use Structured Logging

Always log with operation context:

Common Job Patterns

Health Check Pattern

Cleanup Pattern

Metrics Collection Pattern

Troubleshooting

Job Not Starting

Check if the job is registered:
Verify registration in code:

Job Failing Repeatedly

Check error logs:
Review error count in statistics:

Performance Issues

Monitor execution time:
If execution time approaches interval:
  • Increase the interval
  • Optimize job logic
  • Consider breaking into smaller jobs

Job Not Executing on Time

Verify interval configuration:
Check system clock drift if timing is critical.

Future Enhancements

Planned improvements to the job system:
  • Job dependencies (Job B waits for Job A completion)
  • Conditional execution (skip job if condition not met)
  • Job state persistence (resume after satellite restart)
  • Distributed coordination (multi-satellite job scheduling)
  • Retry logic with exponential backoff
  • Dynamic interval adjustment based on load
  • Prometheus metrics export
  • Web UI for job management

Implementation Status

Current Features:
  • ✅ BaseJob abstract class with interval management
  • ✅ JobManager for centralized control
  • ✅ Automatic error handling and logging
  • ✅ Execution metrics tracking
  • ✅ HeartbeatJob integration
  • ✅ Template job for reference
In Development:
  • 🚧 Job priority levels
  • 🚧 Job status API endpoint
  • 🚧 Advanced monitoring features
The job system is production-ready and actively used for the heartbeat service. The pattern has proven stable and is ready for additional jobs.