Architecture Overview
The job system consists of three core components:BaseJob Abstract Class
All jobs extendBaseJob, 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
TheJobManager 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
Createsrc/jobs/process-health-job.ts:
Step 2: Export from Index
Add tosrc/jobs/index.ts:
Step 3: Register in Server
Add tosrc/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:.env.example:
Jobs with Dependencies
If your job needs access to services, inject them via constructor: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:Error Handling
Automatic Error Recovery
TheBaseJob 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: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:Job Failing Repeatedly
Check error logs:Performance Issues
Monitor execution time:- Increase the interval
- Optimize job logic
- Consider breaking into smaller jobs
Job Not Executing on Time
Verify interval configuration: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
- 🚧 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.

