|
| 1 | +use std::sync::Mutex; |
| 2 | +use std::collections::HashMap; |
| 3 | +use chrono::{DateTime, UTC}; |
| 4 | +use std::sync::Arc; |
| 5 | +use std::time::Duration; |
| 6 | +use std::sync::mpsc::Sender; |
| 7 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 8 | +use std::thread::{self, JoinHandle}; |
| 9 | +use super::{ExecutionUpdate, ExecutionState, Transition, HeartbeatTaskData, get_task_snapshot}; |
| 10 | + |
| 11 | +// Shared state for streaming logs |
| 12 | +pub struct TaskStreamState { |
| 13 | + pub start_time: DateTime<UTC>, |
| 14 | + pub stdout_buffer: Arc<Mutex<String>>, |
| 15 | + pub stderr_buffer: Arc<Mutex<String>>, |
| 16 | +} |
| 17 | + |
| 18 | +// Global state - tracks running tasks with log buffers |
| 19 | +lazy_static! { |
| 20 | + static ref RUNNING_TASKS: Mutex<HashMap<String, TaskStreamState>> = |
| 21 | + Mutex::new(HashMap::new()); |
| 22 | +} |
| 23 | + |
| 24 | +// Called by executor when task starts |
| 25 | +// Returns Arc clones to log buffers for the streaming threads |
| 26 | +pub fn register_task_start(name: String, start: DateTime<UTC>) |
| 27 | + -> (Arc<Mutex<String>>, Arc<Mutex<String>>) |
| 28 | +{ |
| 29 | + let stdout_buffer = Arc::new(Mutex::new(String::new())); |
| 30 | + let stderr_buffer = Arc::new(Mutex::new(String::new())); |
| 31 | + |
| 32 | + RUNNING_TASKS.lock() |
| 33 | + .unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 34 | + .insert(name, TaskStreamState { |
| 35 | + start_time: start, |
| 36 | + stdout_buffer: stdout_buffer.clone(), |
| 37 | + stderr_buffer: stderr_buffer.clone(), |
| 38 | + }); |
| 39 | + |
| 40 | + (stdout_buffer, stderr_buffer) |
| 41 | +} |
| 42 | + |
| 43 | +// Called by executor when task completes |
| 44 | +pub fn unregister_task_completion(name: &str) { |
| 45 | + RUNNING_TASKS.lock() |
| 46 | + .unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 47 | + .remove(name); |
| 48 | +} |
| 49 | + |
| 50 | +// Snapshot with cumulative logs (for heartbeats) |
| 51 | +pub struct TaskHeartbeatData { |
| 52 | + pub name: String, |
| 53 | + pub start_time: DateTime<UTC>, |
| 54 | + pub elapsed: Duration, |
| 55 | + pub stdout: String, // Cumulative stdout up to now |
| 56 | + pub stderr: String, // Cumulative stderr up to now |
| 57 | +} |
| 58 | + |
| 59 | +// Get snapshot for heartbeat with cumulative logs |
| 60 | +pub fn get_running_tasks_with_logs() -> Vec<TaskHeartbeatData> { |
| 61 | + let now = UTC::now(); |
| 62 | + let tasks = RUNNING_TASKS.lock() |
| 63 | + .unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 64 | + |
| 65 | + tasks.iter() |
| 66 | + .map(|(name, state)| { |
| 67 | + let stdout = state.stdout_buffer.lock() |
| 68 | + .unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 69 | + .clone(); |
| 70 | + let stderr = state.stderr_buffer.lock() |
| 71 | + .unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 72 | + .clone(); |
| 73 | + |
| 74 | + let elapsed_chrono = now - state.start_time; |
| 75 | + let elapsed_std = elapsed_chrono.to_std() |
| 76 | + .unwrap_or(Duration::from_secs(0)); |
| 77 | + |
| 78 | + TaskHeartbeatData { |
| 79 | + name: name.clone(), |
| 80 | + start_time: state.start_time, |
| 81 | + elapsed: elapsed_std, |
| 82 | + stdout, |
| 83 | + stderr, |
| 84 | + } |
| 85 | + }) |
| 86 | + .collect() |
| 87 | +} |
| 88 | + |
| 89 | +// Spawn heartbeat timer thread |
| 90 | +pub fn spawn_heartbeat_thread( |
| 91 | + interval: Duration, |
| 92 | + progress_channel: Sender<ExecutionUpdate>, |
| 93 | + shutdown_flag: Arc<AtomicBool>, |
| 94 | + shared_snapshot: Arc<Mutex<super::TaskSnapshot>>, |
| 95 | +) -> JoinHandle<()> { |
| 96 | + thread::Builder::new() |
| 97 | + .name("heartbeat".to_string()) |
| 98 | + .spawn(move || { |
| 99 | + loop { |
| 100 | + thread::sleep(interval); |
| 101 | + |
| 102 | + // Check shutdown first |
| 103 | + if shutdown_flag.load(Ordering::SeqCst) { |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + let running = get_running_tasks_with_logs(); |
| 108 | + if running.is_empty() { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + // Convert to HeartbeatTaskData for the transition |
| 113 | + let heartbeat_data: Vec<HeartbeatTaskData> = running.iter() |
| 114 | + .map(|t| HeartbeatTaskData { |
| 115 | + task_name: t.name.clone(), |
| 116 | + elapsed_seconds: t.elapsed.as_secs(), |
| 117 | + stdout: t.stdout.clone(), |
| 118 | + stderr: t.stderr.clone(), |
| 119 | + }) |
| 120 | + .collect(); |
| 121 | + |
| 122 | + // Get live snapshot from shared state |
| 123 | + let snapshot = shared_snapshot.lock() |
| 124 | + .unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 125 | + .clone(); |
| 126 | + |
| 127 | + // Create heartbeat ExecutionUpdate |
| 128 | + let update = ExecutionUpdate::new( |
| 129 | + ExecutionState::Running, |
| 130 | + snapshot, |
| 131 | + Transition::Heartbeat(heartbeat_data) |
| 132 | + ); |
| 133 | + |
| 134 | + match progress_channel.send(update) { |
| 135 | + Ok(_) => {}, |
| 136 | + Err(e) => { |
| 137 | + warn!("Heartbeat send failed: {}, exiting", e); |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + }) |
| 143 | + .expect("Failed to spawn heartbeat thread") |
| 144 | +} |
0 commit comments