|
1 | 1 | //! Tick loop: load jobs.json, find due jobs, execute (inbound to agent or direct sendMessage). |
| 2 | +
|
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use tokio::sync::mpsc; |
| 6 | + |
| 7 | +use crate::telegram::{InboundMsg, OutboundMsg}; |
| 8 | +use crate::tools::cron::{CronStore, JobAction}; |
| 9 | + |
| 10 | +fn unix_now() -> u64 { |
| 11 | + std::time::SystemTime::now() |
| 12 | + .duration_since(std::time::UNIX_EPOCH) |
| 13 | + .map(|d| d.as_secs()) |
| 14 | + .unwrap_or(0) |
| 15 | +} |
| 16 | + |
| 17 | +/// Run one tick: find due jobs, send to channels, mark fired. Used by runner and tests. |
| 18 | +pub async fn tick_once( |
| 19 | + store: &CronStore, |
| 20 | + inbound_tx: &mpsc::Sender<InboundMsg>, |
| 21 | + outbound_tx: &mpsc::Sender<OutboundMsg>, |
| 22 | + now: u64, |
| 23 | +) { |
| 24 | + let due = store.find_due(now); |
| 25 | + for job in due { |
| 26 | + match job.action { |
| 27 | + JobAction::Agent => { |
| 28 | + let msg = InboundMsg { |
| 29 | + chat_id: job.chat_id, |
| 30 | + user_id: 0, |
| 31 | + text: job.message.clone(), |
| 32 | + channel: "cron".to_string(), |
| 33 | + }; |
| 34 | + if inbound_tx.try_send(msg).is_err() { |
| 35 | + eprintln!("cron runner: inbound channel full, dropping agent job {}", job.id); |
| 36 | + } |
| 37 | + } |
| 38 | + JobAction::Direct => { |
| 39 | + let msg = OutboundMsg { |
| 40 | + chat_id: job.chat_id, |
| 41 | + text: job.message.clone(), |
| 42 | + channel: "cron".to_string(), |
| 43 | + }; |
| 44 | + if outbound_tx.try_send(msg).is_err() { |
| 45 | + eprintln!("cron runner: outbound channel full, dropping direct job {}", job.id); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + store.mark_fired(&job.id, now); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +async fn tick_loop( |
| 54 | + store: Arc<CronStore>, |
| 55 | + inbound_tx: mpsc::Sender<InboundMsg>, |
| 56 | + outbound_tx: mpsc::Sender<OutboundMsg>, |
| 57 | + tick_secs: u64, |
| 58 | +) { |
| 59 | + let mut interval = tokio::time::interval(std::time::Duration::from_secs(tick_secs)); |
| 60 | + interval.tick().await; |
| 61 | + loop { |
| 62 | + interval.tick().await; |
| 63 | + let now = unix_now(); |
| 64 | + tick_once(&store, &inbound_tx, &outbound_tx, now).await; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Spawns the cron runner task. Returns the join handle (caller may ignore). |
| 69 | +pub fn spawn_cron_runner( |
| 70 | + store: Arc<CronStore>, |
| 71 | + inbound_tx: mpsc::Sender<InboundMsg>, |
| 72 | + outbound_tx: mpsc::Sender<OutboundMsg>, |
| 73 | + tick_interval_secs: u64, |
| 74 | +) -> tokio::task::JoinHandle<()> { |
| 75 | + tokio::spawn(async move { |
| 76 | + tick_loop(store, inbound_tx, outbound_tx, tick_interval_secs).await; |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use super::*; |
| 83 | + use crate::tools::cron::{CronStore, Schedule}; |
| 84 | + |
| 85 | + #[tokio::test] |
| 86 | + async fn tick_fires_due_direct_job() { |
| 87 | + let dir = std::env::temp_dir().join("icrab_cron_runner_direct"); |
| 88 | + let _ = std::fs::remove_dir_all(&dir); |
| 89 | + std::fs::create_dir_all(&dir).unwrap(); |
| 90 | + let store = CronStore::empty(&dir); |
| 91 | + store |
| 92 | + .add( |
| 93 | + None, |
| 94 | + "Reminder".to_string(), |
| 95 | + JobAction::Direct, |
| 96 | + Schedule::Once { at_unix: 100 }, |
| 97 | + 12345, |
| 98 | + ) |
| 99 | + .unwrap(); |
| 100 | + let (inbound_tx, _inbound_rx) = mpsc::channel(8); |
| 101 | + let (outbound_tx, mut outbound_rx) = mpsc::channel(8); |
| 102 | + tick_once(&store, &inbound_tx, &outbound_tx, 500).await; |
| 103 | + let msg = outbound_rx.try_recv().unwrap(); |
| 104 | + assert_eq!(msg.chat_id, 12345); |
| 105 | + assert_eq!(msg.text, "Reminder"); |
| 106 | + assert_eq!(msg.channel, "cron"); |
| 107 | + let job = store.get("job-1").unwrap(); |
| 108 | + assert!(job.last_run.is_some()); |
| 109 | + assert!(!job.enabled); |
| 110 | + let _ = std::fs::remove_dir_all(&dir); |
| 111 | + } |
| 112 | + |
| 113 | + #[tokio::test] |
| 114 | + async fn tick_fires_due_agent_job() { |
| 115 | + let dir = std::env::temp_dir().join("icrab_cron_runner_agent"); |
| 116 | + let _ = std::fs::remove_dir_all(&dir); |
| 117 | + std::fs::create_dir_all(&dir).unwrap(); |
| 118 | + let store = CronStore::empty(&dir); |
| 119 | + store |
| 120 | + .add( |
| 121 | + None, |
| 122 | + "Agent task".to_string(), |
| 123 | + JobAction::Agent, |
| 124 | + Schedule::Once { at_unix: 100 }, |
| 125 | + 999, |
| 126 | + ) |
| 127 | + .unwrap(); |
| 128 | + let (inbound_tx, mut inbound_rx) = mpsc::channel(8); |
| 129 | + let (outbound_tx, _outbound_rx) = mpsc::channel(8); |
| 130 | + tick_once(&store, &inbound_tx, &outbound_tx, 500).await; |
| 131 | + let msg = inbound_rx.try_recv().unwrap(); |
| 132 | + assert_eq!(msg.chat_id, 999); |
| 133 | + assert_eq!(msg.text, "Agent task"); |
| 134 | + assert_eq!(msg.channel, "cron"); |
| 135 | + assert_eq!(msg.user_id, 0); |
| 136 | + let _ = std::fs::remove_dir_all(&dir); |
| 137 | + } |
| 138 | + |
| 139 | + #[tokio::test] |
| 140 | + async fn tick_skips_not_due() { |
| 141 | + let dir = std::env::temp_dir().join("icrab_cron_runner_skip"); |
| 142 | + let _ = std::fs::remove_dir_all(&dir); |
| 143 | + std::fs::create_dir_all(&dir).unwrap(); |
| 144 | + let store = CronStore::empty(&dir); |
| 145 | + store |
| 146 | + .add( |
| 147 | + None, |
| 148 | + "Later".to_string(), |
| 149 | + JobAction::Direct, |
| 150 | + Schedule::Once { at_unix: 99999 }, |
| 151 | + 1, |
| 152 | + ) |
| 153 | + .unwrap(); |
| 154 | + let (inbound_tx, _inbound_rx) = mpsc::channel(8); |
| 155 | + let (outbound_tx, mut outbound_rx) = mpsc::channel(8); |
| 156 | + tick_once(&store, &inbound_tx, &outbound_tx, 500).await; |
| 157 | + assert!(outbound_rx.try_recv().is_err()); |
| 158 | + let _ = std::fs::remove_dir_all(&dir); |
| 159 | + } |
| 160 | +} |
0 commit comments