Skip to main content

text_document_common/
long_operation.rs

1// Generated by Qleany v1.4.8 from long_operation.tera
2//! This module provides a framework for managing long-running operations with the ability to track
3//! status, progress, and enable cancellation. It includes the infrastructure for defining, executing,
4//! and monitoring such operations. For undoable operations, it is recommended to use the Undo/Redo framework.
5//!
6//! # Components:
7//!
8//! - **OperationStatus**: Enum representing the state of an operation.
9//! - **OperationProgress**: Struct holding details about the progress of an operation.
10//! - **LongOperation**: Trait that must be implemented by any long-running operation.
11//! - **LongOperationManager**: Manager that orchestrates the execution, tracking, and cleanup of multiple operations.
12//!
13//! # Usage:
14//!
15//! 1. Implement the `LongOperation` trait for your task.
16//! 2. Use `LongOperationManager` to start, track, and manage your operations.
17//! 3. Access methods like:
18//!     - `start_operation` to start new operations.
19//!     - `get_operation_status`, `get_operation_progress` to query operation details.
20//!     - `cancel_operation` to cancel operations.
21//!     - `cleanup_finished_operations` to remove completed or cancelled operations.
22//!
23//! # Example:
24//!
25//! ```rust,ignore
26//! // Define your long-running operation
27//! use std::sync::Arc;
28//! use std::sync::atomic::{AtomicBool, Ordering};
29//! use std::thread;
30//! use std::time::Duration;
31//! use common::long_operation::{LongOperation, LongOperationManager, OperationProgress};
32//!
33//! pub struct MyOperation {
34//!     pub total_steps: usize,
35//! }
36//!
37//! impl LongOperation for MyOperation {
38//!     fn execute(
39//!         &self,
40//!         progress_callback: Box<dyn Fn(OperationProgress) + Send>,
41//!         cancel_flag: Arc<AtomicBool>,
42//!     ) -> Result<(), String> {
43//!         for i in 0..self.total_steps {
44//!             if cancel_flag.load(Ordering::Relaxed) {
45//!                 return Err("Operation cancelled".to_string());
46//!             }
47//!             thread::sleep(Duration::from_millis(500));
48//!             progress_callback(OperationProgress::new(
49//!                 (i as f32 / self.total_steps as f32) * 100.0,
50//!                 Some(format!("Step {}/{}", i + 1, self.total_steps)),
51//!             ));
52//!         }
53//!         Ok(())
54//!     }
55//! }
56//!
57//! let manager = LongOperationManager::new();
58//! let my_operation = MyOperation { total_steps: 5 };
59//! let operation_id = manager.start_operation(my_operation);
60//!
61//! while let Some(status) = manager.get_operation_status(&operation_id) {
62//!     println!("{:?}", status);
63//!     thread::sleep(Duration::from_millis(100));
64//! }
65//! ```
66//!
67//! # Notes:
68//!
69//! - Thread-safety is ensured through the use of `Arc<Mutex<T>>` and `AtomicBool`.
70//! - Operations run in their own threads, ensuring non-blocking execution.
71//! - Proper cleanup of finished operations is encouraged using `cleanup_finished_operations`.
72//!
73//! # Definitions:
74//!
75//! ## `OperationStatus`
76//! Represents the state of an operation. Possible states are:
77//! - `Running`: Operation is ongoing.
78//! - `Completed`: Operation finished successfully.
79//! - `Cancelled`: Operation was cancelled by the user.
80//! - `Failed(String)`: Operation failed with an error message.
81//!
82//! ## `OperationProgress`
83//! Describes the progress of an operation, including:
84//! - `percentage` (0.0 to 100.0): Indicates completion progress.
85//! - `message`: Optional user-defined progress description.
86//!
87//! ## `LongOperation` Trait
88//! Any custom long-running operation must implement this trait:
89//! - `execute`: Defines the operation logic, accepting a progress callback and cancellation flag.
90//!
91//! ## `LongOperationManager`
92//! Provides APIs to manage operations, including:
93//! - `start_operation`: Starts a new operation and returns its unique ID.
94//! - `get_operation_status`: Queries the current status of an operation.
95//! - `get_operation_progress`: Retrieves the progress of an operation.
96//! - `cancel_operation`: Cancels an operation.
97//! - `cleanup_finished_operations`: Removes completed or cancelled operations to free resources.
98//!
99//! ## Example Operation: FileProcessingOperation
100//! Represents a long-running operation to process files. Demonstrates typical usage of the framework.
101//!
102//! - **Fields**:
103//!     - `file_path`: Path of the file to process.
104//!     - `total_files`: Number of files to process.
105//! - **Behavior**:
106//!   Simulates file processing with periodic progress updates. Supports cancellation.
107//!
108//!
109
110// Generated by Qleany. Edit at your own risk! Be careful when regenerating this file
111// as changes will be lost.
112
113use crate::event::{Event, EventHub, LongOperationEvent, Origin};
114use anyhow::Result;
115use std::collections::HashMap;
116use std::sync::{
117    Arc, Mutex,
118    atomic::{AtomicBool, Ordering},
119};
120use std::thread;
121
122// Status of a long operation
123#[derive(Debug, Clone, PartialEq)]
124pub enum OperationStatus {
125    Running,
126    Completed,
127    Cancelled,
128    Failed(String),
129}
130
131// Progress information
132#[derive(Debug, Clone)]
133pub struct OperationProgress {
134    pub percentage: f32, // 0.0 to 100.0
135    pub message: Option<String>,
136}
137
138impl OperationProgress {
139    pub fn new(percentage: f32, message: Option<String>) -> Self {
140        Self {
141            percentage: percentage.clamp(0.0, 100.0),
142            message,
143        }
144    }
145}
146
147// Trait that long operations must implement
148pub trait LongOperation: Send + 'static {
149    type Output: Send + Sync + 'static + serde::Serialize;
150
151    fn execute(
152        &self,
153        progress_callback: Box<dyn Fn(OperationProgress) + Send>,
154        cancel_flag: Arc<AtomicBool>,
155    ) -> Result<Self::Output>;
156}
157
158// Trait for operation handles (type-erased)
159trait OperationHandleTrait: Send {
160    fn get_status(&self) -> OperationStatus;
161    fn get_progress(&self) -> OperationProgress;
162    fn cancel(&self);
163    fn is_finished(&self) -> bool;
164}
165
166// Concrete handle implementation
167struct OperationHandle {
168    status: Arc<Mutex<OperationStatus>>,
169    progress: Arc<Mutex<OperationProgress>>,
170    cancel_flag: Arc<AtomicBool>,
171    _join_handle: thread::JoinHandle<()>,
172}
173
174/// Lock a mutex, recovering from poisoning by returning the inner value.
175///
176/// If a thread panicked while holding the lock, the data may be in an
177/// inconsistent state, but for status/progress tracking this is preferable
178/// to propagating the panic.
179fn lock_or_recover<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
180    mutex
181        .lock()
182        .unwrap_or_else(|poisoned| poisoned.into_inner())
183}
184
185impl OperationHandleTrait for OperationHandle {
186    fn get_status(&self) -> OperationStatus {
187        lock_or_recover(&self.status).clone()
188    }
189
190    fn get_progress(&self) -> OperationProgress {
191        lock_or_recover(&self.progress).clone()
192    }
193
194    fn cancel(&self) {
195        self.cancel_flag.store(true, Ordering::Relaxed);
196        let mut status = lock_or_recover(&self.status);
197        if matches!(*status, OperationStatus::Running) {
198            *status = OperationStatus::Cancelled;
199        }
200    }
201
202    fn is_finished(&self) -> bool {
203        matches!(
204            self.get_status(),
205            OperationStatus::Completed | OperationStatus::Cancelled | OperationStatus::Failed(_)
206        )
207    }
208}
209
210// Manager for long operations
211pub struct LongOperationManager {
212    operations: Arc<Mutex<HashMap<String, Box<dyn OperationHandleTrait>>>>,
213    next_id: Arc<Mutex<u64>>,
214    results: Arc<Mutex<HashMap<String, String>>>, // Store serialized results
215    event_hub: Option<Arc<EventHub>>,
216}
217
218impl LongOperationManager {
219    pub fn new() -> Self {
220        Self {
221            operations: Arc::new(Mutex::new(HashMap::new())),
222            next_id: Arc::new(Mutex::new(0)),
223            results: Arc::new(Mutex::new(HashMap::new())),
224            event_hub: None,
225        }
226    }
227
228    /// Inject the event hub to allow sending long operation related events
229    pub fn set_event_hub(&mut self, event_hub: &Arc<EventHub>) {
230        self.event_hub = Some(Arc::clone(event_hub));
231    }
232
233    /// Start a new long operation and return its ID
234    pub fn start_operation<Op: LongOperation>(&self, operation: Op) -> String {
235        let id = {
236            let mut next_id = lock_or_recover(&self.next_id);
237            *next_id += 1;
238            format!("op_{}", *next_id)
239        };
240
241        // Emit started event
242        if let Some(event_hub) = &self.event_hub {
243            event_hub.send_event(Event {
244                origin: Origin::LongOperation(LongOperationEvent::Started),
245                ids: vec![],
246                data: Some(id.clone()),
247            });
248        }
249
250        let status = Arc::new(Mutex::new(OperationStatus::Running));
251        let progress = Arc::new(Mutex::new(OperationProgress::new(0.0, None)));
252        let cancel_flag = Arc::new(AtomicBool::new(false));
253
254        let status_clone = status.clone();
255        let progress_clone = progress.clone();
256        let cancel_flag_clone = cancel_flag.clone();
257        let results_clone = self.results.clone();
258        let id_clone = id.clone();
259        let event_hub_opt = self.event_hub.clone();
260
261        let join_handle = thread::spawn(move || {
262            let progress_callback = {
263                let progress = progress_clone.clone();
264                let event_hub_opt = event_hub_opt.clone();
265                let id_for_cb = id_clone.clone();
266                Box::new(move |prog: OperationProgress| {
267                    *lock_or_recover(&progress) = prog.clone();
268                    if let Some(event_hub) = &event_hub_opt {
269                        let payload = serde_json::json!({
270                            "id": id_for_cb,
271                            "percentage": prog.percentage,
272                            "message": prog.message,
273                        })
274                        .to_string();
275                        event_hub.send_event(Event {
276                            origin: Origin::LongOperation(LongOperationEvent::Progress),
277                            ids: vec![],
278                            data: Some(payload),
279                        });
280                    }
281                }) as Box<dyn Fn(OperationProgress) + Send>
282            };
283
284            let operation_result = operation.execute(progress_callback, cancel_flag_clone.clone());
285
286            let final_status = if cancel_flag_clone.load(Ordering::Relaxed) {
287                OperationStatus::Cancelled
288            } else {
289                match &operation_result {
290                    Ok(result) => {
291                        // Store the result
292                        if let Ok(serialized) = serde_json::to_string(result) {
293                            let mut results = lock_or_recover(&results_clone);
294                            results.insert(id_clone.clone(), serialized);
295                        }
296                        OperationStatus::Completed
297                    }
298                    Err(e) => OperationStatus::Failed(e.to_string()),
299                }
300            };
301
302            // Emit final status event
303            if let Some(event_hub) = &event_hub_opt {
304                let (event, data) = match &final_status {
305                    OperationStatus::Completed => (
306                        LongOperationEvent::Completed,
307                        serde_json::json!({"id": id_clone}).to_string(),
308                    ),
309                    OperationStatus::Cancelled => (
310                        LongOperationEvent::Cancelled,
311                        serde_json::json!({"id": id_clone}).to_string(),
312                    ),
313                    OperationStatus::Failed(err) => (
314                        LongOperationEvent::Failed,
315                        serde_json::json!({"id": id_clone, "error": err}).to_string(),
316                    ),
317                    OperationStatus::Running => (
318                        LongOperationEvent::Progress,
319                        serde_json::json!({"id": id_clone}).to_string(),
320                    ),
321                };
322                event_hub.send_event(Event {
323                    origin: Origin::LongOperation(event),
324                    ids: vec![],
325                    data: Some(data),
326                });
327            }
328
329            *lock_or_recover(&status_clone) = final_status;
330        });
331
332        let handle = OperationHandle {
333            status,
334            progress,
335            cancel_flag,
336            _join_handle: join_handle,
337        };
338
339        lock_or_recover(&self.operations).insert(id.clone(), Box::new(handle));
340
341        id
342    }
343
344    /// Get the status of an operation
345    pub fn get_operation_status(&self, id: &str) -> Option<OperationStatus> {
346        let operations = lock_or_recover(&self.operations);
347        operations.get(id).map(|handle| handle.get_status())
348    }
349
350    /// Get the progress of an operation
351    pub fn get_operation_progress(&self, id: &str) -> Option<OperationProgress> {
352        let operations = lock_or_recover(&self.operations);
353        operations.get(id).map(|handle| handle.get_progress())
354    }
355
356    /// Cancel an operation
357    pub fn cancel_operation(&self, id: &str) -> bool {
358        let operations = lock_or_recover(&self.operations);
359        if let Some(handle) = operations.get(id) {
360            handle.cancel();
361            // Emit cancelled event immediately
362            if let Some(event_hub) = &self.event_hub {
363                let payload = serde_json::json!({"id": id}).to_string();
364                event_hub.send_event(Event {
365                    origin: Origin::LongOperation(LongOperationEvent::Cancelled),
366                    ids: vec![],
367                    data: Some(payload),
368                });
369            }
370            true
371        } else {
372            false
373        }
374    }
375
376    /// Check if an operation is finished
377    pub fn is_operation_finished(&self, id: &str) -> Option<bool> {
378        let operations = lock_or_recover(&self.operations);
379        operations.get(id).map(|handle| handle.is_finished())
380    }
381
382    /// Remove finished operations from memory
383    pub fn cleanup_finished_operations(&self) {
384        let mut operations = lock_or_recover(&self.operations);
385        operations.retain(|_, handle| !handle.is_finished());
386    }
387
388    /// Get list of all operation IDs
389    pub fn list_operations(&self) -> Vec<String> {
390        let operations = lock_or_recover(&self.operations);
391        operations.keys().cloned().collect()
392    }
393
394    /// Get summary of all operations
395    pub fn get_operations_summary(&self) -> Vec<(String, OperationStatus, OperationProgress)> {
396        let operations = lock_or_recover(&self.operations);
397        operations
398            .iter()
399            .map(|(id, handle)| (id.clone(), handle.get_status(), handle.get_progress()))
400            .collect()
401    }
402
403    /// Store an operation result
404    pub fn store_operation_result<T: serde::Serialize>(&self, id: &str, result: T) -> Result<()> {
405        let serialized = serde_json::to_string(&result)?;
406        let mut results = lock_or_recover(&self.results);
407        results.insert(id.to_string(), serialized);
408        Ok(())
409    }
410
411    /// Get an operation result
412    pub fn get_operation_result(&self, id: &str) -> Option<String> {
413        let results = lock_or_recover(&self.results);
414        results.get(id).cloned()
415    }
416}
417
418impl Default for LongOperationManager {
419    fn default() -> Self {
420        Self::new()
421    }
422}
423
424#[cfg(test)]
425mod tests {
426    use super::*;
427    use anyhow::anyhow;
428    use std::time::Duration;
429
430    // Example implementation of a long operation
431    pub struct FileProcessingOperation {
432        pub file_path: String,
433        pub total_files: usize,
434    }
435
436    impl LongOperation for FileProcessingOperation {
437        type Output = ();
438
439        fn execute(
440            &self,
441            progress_callback: Box<dyn Fn(OperationProgress) + Send>,
442            cancel_flag: Arc<AtomicBool>,
443        ) -> Result<Self::Output> {
444            for i in 0..self.total_files {
445                // Check if operation was cancelled
446                if cancel_flag.load(Ordering::Relaxed) {
447                    return Err(anyhow!("Operation was cancelled".to_string()));
448                }
449
450                // Simulate work
451                thread::sleep(Duration::from_millis(500));
452
453                // Report progress
454                let percentage = (i as f32 / self.total_files as f32) * 100.0;
455                progress_callback(OperationProgress::new(
456                    percentage,
457                    Some(format!("Processing file {} of {}", i + 1, self.total_files)),
458                ));
459            }
460
461            // Final progress
462            progress_callback(OperationProgress::new(100.0, Some("Completed".to_string())));
463            Ok(())
464        }
465    }
466
467    #[test]
468    fn test_operation_manager() {
469        let manager = LongOperationManager::new();
470
471        let operation = FileProcessingOperation {
472            file_path: "/tmp/test".to_string(),
473            total_files: 5,
474        };
475
476        let op_id = manager.start_operation(operation);
477
478        // Check initial status
479        assert_eq!(
480            manager.get_operation_status(&op_id),
481            Some(OperationStatus::Running)
482        );
483
484        // Wait a bit and check progress
485        thread::sleep(Duration::from_millis(100));
486        let progress = manager.get_operation_progress(&op_id);
487        assert!(progress.is_some());
488
489        // Test cancellation
490        assert!(manager.cancel_operation(&op_id));
491        thread::sleep(Duration::from_millis(100));
492        assert_eq!(
493            manager.get_operation_status(&op_id),
494            Some(OperationStatus::Cancelled)
495        );
496    }
497}