Skip to content

Commit

Permalink
clean up version_manager instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
dprats committed Nov 27, 2024
1 parent 6caaf60 commit 487a6fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion clients/cli/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let updater_config = UpdaterConfig::new(args.updater_mode, args.hostname);

// b. runs the CLI's auto updater in a separate thread continuously in intervals
updater::spawn_auto_update_thread(&updater_config);
updater::spawn_auto_update_thread(&updater_config).expect("Failed to spawn auto-update thread");

let k = 4;
// TODO(collinjackson): Get parameters from a file or URL.
Expand Down
22 changes: 14 additions & 8 deletions clients/cli/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ use crate::utils::updater::{UpdaterConfig, VersionManager, VersionStatus, BLUE,
// 1. The update checker can continuously monitor for new versions without interrupting the main CLI operations
// 2. The main thread remains free to handle its primary responsibility (proving transactions)
// 3. Users don't have to wait for update checks to complete before using the CLI
pub fn spawn_auto_update_thread(updater_config: &UpdaterConfig) {
pub fn spawn_auto_update_thread(
updater_config: &UpdaterConfig,
) -> Result<(), Box<dyn std::error::Error>> {
println!(
"{}[auto-updater thread]{} Starting periodic CLI updates...",
BLUE, RESET
);

// Initialize version manager
let version_manager = VersionManager::new(updater_config.clone()).unwrap();
let version_manager = Arc::new(version_manager);
// Create a thread-safe version manager that can be shared across threads
let version_manager: Arc<VersionManager> = Arc::new(
VersionManager::new(updater_config.clone()).expect("Failed to initialize version manager"),
);

// Create a reference for the new thread (original stays with main thread)
let version_manager_thread: Arc<VersionManager> = version_manager.clone();

// Clone Arcs for the update checker thread
let version_manager = version_manager.clone();
let update_interval = updater_config.update_interval;

// Spawn the update checker thread
Expand All @@ -42,12 +46,12 @@ pub fn spawn_auto_update_thread(updater_config: &UpdaterConfig) {

// Infinite loop to check for updates
loop {
match version_manager.as_ref().update_version_status() {
match version_manager_thread.as_ref().update_version_status() {
// Got the latest version info with no error....
Ok(version_info) => match version_info {
// ... there is an update available, try to apply it
VersionStatus::UpdateAvailable(new_version) => {
if let Err(e) = version_manager.apply_update(&new_version) {
if let Err(e) = version_manager_thread.apply_update(&new_version) {
eprintln!(
"{}[auto-updater thread]{} Failed to update CLI: {}",
BLUE, RESET, e
Expand Down Expand Up @@ -75,4 +79,6 @@ pub fn spawn_auto_update_thread(updater_config: &UpdaterConfig) {
thread::sleep(Duration::from_secs(update_interval));
}
});

Ok(())
}

0 comments on commit 487a6fc

Please sign in to comment.