terraphim_update 1.16.34

Shared auto-update functionality for Terraphim AI binaries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Tokio-based update scheduler
//!
//! This module provides a scheduler for periodic update checks
//! using tokio's interval timer.

use crate::config::UpdateConfig;
use anyhow::{Context, Result};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};

/// Notification type sent from scheduler to application
#[derive(Debug, Clone)]
pub enum UpdateNotification {
    /// An update is available
    UpdateAvailable {
        current_version: String,
        latest_version: String,
    },
    /// Update check failed
    CheckFailed { error: String },
    /// Scheduler stopped
    Stopped,
}

/// Update scheduler that runs periodic update checks
///
/// Uses tokio::time::interval for scheduling and sends notifications
/// through a channel when updates are available.
pub struct UpdateScheduler {
    config: Arc<UpdateConfig>,
    update_check_fn: Arc<dyn Fn() -> Result<UpdateCheckResult> + Send + Sync>,
    handle: Option<JoinHandle<()>>,
    notification_sender: Option<mpsc::UnboundedSender<UpdateNotification>>,
    is_running: bool,
}

/// Result of an update check
#[derive(Debug, Clone)]
pub enum UpdateCheckResult {
    /// No update available
    UpToDate,
    /// Update available with version info
    UpdateAvailable {
        current_version: String,
        latest_version: String,
    },
    /// Check failed with error
    Failed { error: String },
}

impl UpdateScheduler {
    /// Create a new update scheduler
    ///
    /// # Arguments
    /// * `config` - Update configuration
    /// * `update_check_fn` - Function to call for update checks
    ///
    /// # Returns
    /// * New UpdateScheduler instance
    ///
    /// # Example
    /// ```no_run
    /// use terraphim_update::scheduler::{UpdateScheduler, UpdateCheckResult};
    /// use terraphim_update::config::UpdateConfig;
    /// use std::sync::Arc;
    ///
    /// let config = UpdateConfig::default();
    /// let scheduler = UpdateScheduler::new(
    ///     Arc::new(config),
    ///     Arc::new(|| Ok(UpdateCheckResult::UpToDate))
    /// );
    /// ```
    pub fn new(
        config: Arc<UpdateConfig>,
        update_check_fn: Arc<dyn Fn() -> Result<UpdateCheckResult> + Send + Sync>,
    ) -> Self {
        Self {
            config,
            update_check_fn,
            handle: None,
            notification_sender: None,
            is_running: false,
        }
    }

    /// Create a channel for receiving update notifications
    ///
    /// Returns a receiver that will receive notifications when updates
    /// are available or checks fail.
    ///
    /// # Returns
    /// * `Ok(mpsc::UnboundedReceiver<UpdateNotification>)` - Notification receiver
    /// * `Err(anyhow::Error)` - Error if already started
    pub fn create_notification_channel(
        &mut self,
    ) -> Result<mpsc::UnboundedReceiver<UpdateNotification>> {
        if self.is_running {
            anyhow::bail!("Cannot create channel after scheduler is started");
        }

        let (sender, receiver) = mpsc::unbounded_channel();
        self.notification_sender = Some(sender);
        Ok(receiver)
    }

    /// Start the scheduler
    ///
    /// Begins periodic update checks based on the configured interval.
    /// Sends notifications through the created channel when updates are available.
    ///
    /// # Returns
    /// * `Ok(())` - Successfully started
    /// * `Err(anyhow::Error)` - Error starting scheduler
    ///
    /// # Example
    /// ```no_run
    /// # use terraphim_update::scheduler::{UpdateScheduler, UpdateCheckResult};
    /// # use terraphim_update::config::UpdateConfig;
    /// # use std::sync::Arc;
    /// #
    /// # async fn test() -> Result<(), anyhow::Error> {
    /// let config = UpdateConfig::default();
    /// let mut scheduler = UpdateScheduler::new(
    ///     Arc::new(config),
    ///     Arc::new(|| Ok(UpdateCheckResult::UpToDate))
    /// );
    ///
    /// let mut receiver = scheduler.create_notification_channel()?;
    /// scheduler.start().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn start(&mut self) -> Result<()> {
        if self.is_running {
            warn!("Scheduler is already running");
            return Ok(());
        }

        if !self.config.auto_update_enabled {
            info!("Auto-update is disabled, scheduler not starting");
            return Ok(());
        }

        let sender = self
            .notification_sender
            .clone()
            .context("No notification channel created. Call create_notification_channel() first")?;

        let config = Arc::clone(&self.config);
        let check_fn = Arc::clone(&self.update_check_fn);

        info!(
            "Starting update scheduler with interval: {:?}",
            config.auto_update_check_interval
        );

        let handle = tokio::spawn(async move {
            let mut interval = tokio::time::interval(config.auto_update_check_interval);

            // Immediate first check
            let _ = Self::perform_check(&check_fn, &sender).await;

            loop {
                interval.tick().await;

                debug!("Performing scheduled update check");
                if let Err(e) = Self::perform_check(&check_fn, &sender).await {
                    error!("Error in scheduled check: {}", e);
                }
            }
        });

        self.handle = Some(handle);
        self.is_running = true;

        info!("Update scheduler started");
        Ok(())
    }

    /// Stop the scheduler
    ///
    /// Gracefully stops the scheduler, waiting for any ongoing
    /// update check to complete.
    ///
    /// # Returns
    /// * `Ok(())` - Successfully stopped
    /// * `Err(anyhow::Error)` - Error stopping scheduler
    pub async fn stop(&mut self) -> Result<()> {
        if !self.is_running {
            debug!("Scheduler is not running");
            return Ok(());
        }

        info!("Stopping update scheduler");

        if let Some(handle) = self.handle.take() {
            handle.abort();
            let _ = tokio::time::timeout(Duration::from_secs(5), handle).await;
        }

        self.is_running = false;

        if let Some(sender) = &self.notification_sender {
            let _ = sender.send(UpdateNotification::Stopped);
        }

        info!("Update scheduler stopped");
        Ok(())
    }

    /// Check if scheduler is running
    pub fn is_running(&self) -> bool {
        self.is_running
    }

    /// Check if an update check should be performed based on interval
    ///
    /// This is a helper for manual check logic that respects the configured interval.
    ///
    /// # Arguments
    /// * `last_check_time` - Timestamp of the last update check
    ///
    /// # Returns
    /// * `true` - Check should be performed
    /// * `false` - Check should be skipped
    pub fn should_check(&self, last_check_time: Option<std::time::Instant>) -> bool {
        if !self.config.auto_update_enabled {
            return false;
        }

        match last_check_time {
            Some(last) => {
                let elapsed = last.elapsed();
                elapsed >= self.config.auto_update_check_interval
            }
            None => true,
        }
    }

    /// Perform a single update check
    ///
    /// Internal method called by the scheduler loop.
    async fn perform_check(
        check_fn: &Arc<dyn Fn() -> Result<UpdateCheckResult> + Send + Sync>,
        sender: &mpsc::UnboundedSender<UpdateNotification>,
    ) -> Result<()> {
        let result = tokio::task::spawn_blocking({
            let check_fn = Arc::clone(check_fn);
            move || check_fn()
        })
        .await
        .context("Failed to spawn blocking task for update check")?;

        let check_result = result.context("Update check function failed")?;

        match check_result {
            UpdateCheckResult::UpdateAvailable {
                current_version,
                latest_version,
            } => {
                info!(
                    "Update available: {} -> {}",
                    current_version, latest_version
                );
                sender
                    .send(UpdateNotification::UpdateAvailable {
                        current_version,
                        latest_version,
                    })
                    .context("Failed to send update notification")?;
            }
            UpdateCheckResult::UpToDate => {
                debug!("Up to date, no notification needed");
            }
            UpdateCheckResult::Failed { error } => {
                warn!("Update check failed: {}", error);
                sender
                    .send(UpdateNotification::CheckFailed { error })
                    .context("Failed to send error notification")?;
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::Instant;

    #[test]
    fn test_scheduler_creation() {
        let config = UpdateConfig::default();
        let check_count = Arc::new(AtomicUsize::new(0));
        let check_count_clone = Arc::clone(&check_count);

        let check_fn = Arc::new(move || {
            check_count_clone.fetch_add(1, Ordering::SeqCst);
            Ok(UpdateCheckResult::UpToDate)
        });

        let scheduler = UpdateScheduler::new(Arc::new(config), check_fn);
        assert!(!scheduler.is_running());
    }

    #[test]
    fn test_should_check_with_interval() {
        let config = UpdateConfig {
            auto_update_enabled: true,
            auto_update_check_interval: Duration::from_secs(60),
        };

        let check_fn = Arc::new(|| Ok(UpdateCheckResult::UpToDate));
        let scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        // Should check when no last check time
        assert!(scheduler.should_check(None));

        // Should not check when interval hasn't elapsed
        let recent = Instant::now() - Duration::from_secs(30);
        assert!(!scheduler.should_check(Some(recent)));

        // Should check when interval has elapsed
        let old = Instant::now() - Duration::from_secs(120);
        assert!(scheduler.should_check(Some(old)));
    }

    #[test]
    fn test_should_check_when_disabled() {
        let config = UpdateConfig {
            auto_update_enabled: false,
            auto_update_check_interval: Duration::from_secs(60),
        };

        let check_fn = Arc::new(|| Ok(UpdateCheckResult::UpToDate));
        let scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        // Should not check when auto-update is disabled
        assert!(!scheduler.should_check(None));
    }

    #[tokio::test]
    async fn test_scheduler_starts_and_stops() {
        let config = UpdateConfig {
            auto_update_enabled: true,
            auto_update_check_interval: Duration::from_millis(100),
        };

        let check_fn = Arc::new(|| Ok(UpdateCheckResult::UpToDate));
        let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        let mut receiver = scheduler.create_notification_channel().unwrap();
        scheduler.start().await.unwrap();
        assert!(scheduler.is_running());

        // Wait a bit for at least one check
        tokio::time::sleep(Duration::from_millis(200)).await;

        scheduler.stop().await.unwrap();
        assert!(!scheduler.is_running());

        // Should receive stopped notification
        let notification = receiver.try_recv().unwrap();
        matches!(notification, UpdateNotification::Stopped);
    }

    #[tokio::test]
    async fn test_scheduler_with_short_interval() {
        let config = UpdateConfig {
            auto_update_enabled: true,
            auto_update_check_interval: Duration::from_millis(50),
        };

        let check_fn = Arc::new(|| Ok(UpdateCheckResult::UpToDate));
        let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        let _receiver = scheduler.create_notification_channel().unwrap();
        scheduler.start().await.unwrap();

        // Wait for a few checks
        tokio::time::sleep(Duration::from_millis(150)).await;

        scheduler.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_scheduler_notification_update_available() {
        let config = UpdateConfig {
            auto_update_enabled: true,
            auto_update_check_interval: Duration::from_secs(1000),
        };

        let check_fn = Arc::new(|| {
            Ok(UpdateCheckResult::UpdateAvailable {
                current_version: "1.0.0".to_string(),
                latest_version: "1.1.0".to_string(),
            })
        });

        let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        let mut receiver = scheduler.create_notification_channel().unwrap();
        scheduler.start().await.unwrap();

        // Wait for initial check
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Should receive update notification
        let notification = receiver.try_recv().unwrap();
        match notification {
            UpdateNotification::UpdateAvailable {
                current_version,
                latest_version,
            } => {
                assert_eq!(current_version, "1.0.0");
                assert_eq!(latest_version, "1.1.0");
            }
            _ => panic!("Expected UpdateAvailable notification"),
        }

        scheduler.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_scheduler_notification_check_failed() {
        let config = UpdateConfig {
            auto_update_enabled: true,
            auto_update_check_interval: Duration::from_secs(1000),
        };

        let check_fn = Arc::new(|| {
            Ok(UpdateCheckResult::Failed {
                error: "Network error".to_string(),
            })
        });

        let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        let mut receiver = scheduler.create_notification_channel().unwrap();
        scheduler.start().await.unwrap();

        // Wait for initial check
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Should receive error notification
        let notification = receiver.try_recv().unwrap();
        match notification {
            UpdateNotification::CheckFailed { error } => {
                assert_eq!(error, "Network error");
            }
            _ => panic!("Expected CheckFailed notification"),
        }

        scheduler.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_scheduler_disabled_does_not_start() {
        let config = UpdateConfig {
            auto_update_enabled: false,
            auto_update_check_interval: Duration::from_millis(50),
        };

        let check_fn = Arc::new(|| Ok(UpdateCheckResult::UpToDate));
        let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        let _receiver = scheduler.create_notification_channel().unwrap();
        scheduler.start().await.unwrap();

        assert!(!scheduler.is_running());

        scheduler.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_scheduler_already_running() {
        let config = UpdateConfig {
            auto_update_enabled: true,
            auto_update_check_interval: Duration::from_millis(50),
        };

        let check_fn = Arc::new(|| Ok(UpdateCheckResult::UpToDate));
        let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        let _receiver = scheduler.create_notification_channel().unwrap();
        scheduler.start().await.unwrap();

        // Starting again should be a no-op
        let result = scheduler.start().await;
        assert!(result.is_ok());
        assert!(scheduler.is_running());

        scheduler.stop().await.unwrap();
    }

    #[tokio::test]
    async fn test_scheduler_stop_when_not_running() {
        let config = UpdateConfig::default();
        let check_fn = Arc::new(|| Ok(UpdateCheckResult::UpToDate));
        let mut scheduler = UpdateScheduler::new(Arc::new(config), check_fn);

        // Stopping when not running should be a no-op
        let result = scheduler.stop().await;
        assert!(result.is_ok());
        assert!(!scheduler.is_running());
    }
}