package workerruntime import ( "context" "time" "github.com/itworx/pulse/internal/systemstatus" ) // The standard job schedule. // // Intervals are chosen from what each job observes and how expensive it is, and // every timeout stays well inside its lease so a slow run cannot be duplicated // by another worker before it finishes: // // - discovery: inventory changes slowly and one pass reads a full container // snapshot for up to 150 containers, so a minute is responsive enough // without re-reading the same snapshot repeatedly; // - alert evaluation: rules declare their own evaluation interval (5s // minimum, 60s typically). Scanning every 15 seconds means a rule is picked // up within 15 seconds of becoming due, while the per-rule lease keyed on // the rule's own interval prevents evaluating it more often than declared; // - probe execution: probe intervals start at 5 seconds, so a 15 second scan // keeps a due probe close to its schedule; the batch itself is bounded to // 300 probes with bounded concurrency; // - notification drain: the outbox retry backoff starts at one second, so a // 10 second drain delivers promptly without polling the database hard. const ( DiscoveryInterval = time.Minute DiscoveryTimeout = 45 * time.Second AlertEvaluationInterval = 15 * time.Second AlertEvaluationTimeout = 60 * time.Second ProbeExecutionInterval = 15 * time.Second ProbeExecutionTimeout = 2 * time.Minute NotificationInterval = 10 * time.Second NotificationDrainTimeout = DrainDeadline ) // Job names, stable because they are persisted in job_runs. const ( JobDiscovery = "discovery" JobAlertEvaluation = "alert-evaluation" JobProbeExecution = "probe-execution" JobNotificationDrain = "notification-drain" ) // ScheduleRuns holds the work each scheduled job performs. A nil entry is still // scheduled, but reports Disabled with a reason, so an unconfigured capability // is visible rather than silently absent. type ScheduleRuns struct { Discovery RunFunc AlertEvaluation RunFunc ProbeExecution RunFunc NotificationDrain RunFunc } // Schedule returns the standard worker job set. func Schedule(runs ScheduleRuns) []Job { return []Job{ {Name: JobDiscovery, Component: systemstatus.ComponentWorker, Interval: DiscoveryInterval, Timeout: DiscoveryTimeout, Run: orDisabled(runs.Discovery, "discovery_not_configured")}, {Name: JobAlertEvaluation, Component: systemstatus.ComponentWorker, Interval: AlertEvaluationInterval, Timeout: AlertEvaluationTimeout, Run: orDisabled(runs.AlertEvaluation, "alert_evaluation_not_configured")}, {Name: JobProbeExecution, Component: systemstatus.ComponentProbes, Interval: ProbeExecutionInterval, Timeout: ProbeExecutionTimeout, Run: orDisabled(runs.ProbeExecution, "probe_execution_not_configured")}, {Name: JobNotificationDrain, Component: systemstatus.ComponentNotifications, Interval: NotificationInterval, Timeout: NotificationDrainTimeout, Run: orDisabled(runs.NotificationDrain, "notifications_not_configured")}, } } func orDisabled(run RunFunc, reason string) RunFunc { if run != nil { return run } return func(context.Context) (Outcome, error) { return Outcome{Disabled: true, Reason: reason}, nil } }