agentmux_srv_core/backend/jobs/
cap_alert.rs1use super::caps::Refusal;
11use super::RunLedger;
12use crate::backend::reactive::sanitize::quote_in_system_note;
13use crate::backend::storage::cron::CronJob;
14
15pub fn alert_key(job_id: &str) -> String {
17 format!("job:{job_id}:cap")
18}
19
20pub fn note_text(job_name: &str, refusal: Refusal) -> String {
23 if refusal == Refusal::Rate {
24 return format!(
25 "[AgentMux] Job \"{}\" skipped events: {}. It isn't paused. The limit is the trigger's max_per_hour, and \
26 a source that sends more than it is stuck or looping.",
27 quote_in_system_note(job_name),
28 refusal.words()
29 );
30 }
31 format!(
32 "[AgentMux] Job \"{}\" skipped a run: {}. It isn't paused and runs again tomorrow (the day ends at the \
33 operator's local midnight). Only the operator raises a cap, in Settings → Jobs.",
34 quote_in_system_note(job_name),
35 refusal.words()
36 )
37}
38
39impl RunLedger {
40 pub(super) fn alert_cap(&self, job: &CronJob, refusal: Refusal) {
42 #[cfg(any(test, feature = "test-support"))]
43 self.cap_alerts.lock().unwrap().push((job.id.clone(), refusal.reason().to_string()));
44 #[cfg(not(any(test, feature = "test-support")))]
45 {
46 use super::incidents::owning_leader;
47 if let Some(leader) = owning_leader(self.mstore.as_ref(), job) {
48 let note = note_text(&job.name, refusal);
49 if let Err(e) = crate::backend::reactive::get_global_handler().deliver_system_note(&leader.uid, ¬e) {
50 tracing::info!(job_id = %job.id, error = %e, "jobs: the owning leader didn't get the cap note");
51 }
52 }
53 match crate::backend::notify::router::get(&self.broker) {
54 Some(router) => {
55 let title = format!("{} ({})", job.name, refusal.label());
56 router.job_failed(&alert_key(&job.id), &title, format!("Skipped a run: {}.", refusal.words()));
57 }
58 None => tracing::info!(job_id = %job.id, "jobs: no notification router yet; the cap alert is not shown"),
59 }
60 }
61 }
62}