Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Check for updates in the foreground, not the background
Browse files Browse the repository at this point in the history
This avoids creating a new OpenSSL context while the main thread exits the process.
  • Loading branch information
jyn514 committed Dec 22, 2021
1 parent d504457 commit 9c1d737
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
22 changes: 2 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use wrangler::cli::{exec, Cli, Command};
use wrangler::commands;
use wrangler::installer;
use wrangler::reporter;
use wrangler::terminal::message::{Message, StdOut};
use wrangler::terminal::styles;
use wrangler::version::background_check_for_updates;
use wrangler::version::check_for_updates;

use anyhow::Result;
use structopt::StructOpt;
Expand All @@ -22,7 +20,6 @@ fn main() -> Result<()> {
}
env_logger::init();

let latest_version_receiver = background_check_for_updates();
if let Ok(me) = env::current_exe() {
// If we're actually running as the installer then execute our
// self-installation, otherwise just continue as usual.
Expand All @@ -36,22 +33,7 @@ fn main() -> Result<()> {
}
}
run()?;
if let Ok(latest_version) = latest_version_receiver.try_recv() {
let latest_version = styles::highlight(latest_version.to_string());
let new_version_available = format!(
"A new version of Wrangler ({}) is available!",
latest_version
);
let update_message = "You can learn more about updating here:".to_string();
let update_docs_url = styles::url(
"https://developers.cloudflare.com/workers/cli-wrangler/install-update#update",
);

StdOut::billboard(&format!(
"{}\n{}\n{}",
new_version_available, update_message, update_docs_url
));
}
check_for_updates();
Ok(())
}

Expand Down
29 changes: 19 additions & 10 deletions src/version/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fs;
use std::path::Path;
use std::str::FromStr;
use std::sync::mpsc;
use std::thread;
use std::time::SystemTime;

use crate::settings::get_wrangler_home_dir;
use crate::terminal::message::{StdOut, Message};
use crate::terminal::styles;

use anyhow::Result;
use reqwest::header::USER_AGENT;
Expand All @@ -14,21 +14,30 @@ use serde::{Deserialize, Serialize};

const ONE_DAY: u64 = 60 * 60 * 24;

pub fn background_check_for_updates() -> mpsc::Receiver<Version> {
let (sender, receiver) = mpsc::channel();

let _detached_thread = thread::spawn(move || match check_wrangler_versions() {
pub fn check_for_updates() {
match check_wrangler_versions() {
Ok(wrangler_versions) => {
// If the wrangler version has not been checked within the last day and the versions
// are different, print out an update message
if wrangler_versions.is_outdated() {
let _ = sender.send(wrangler_versions.latest);
let latest_version = styles::highlight(wrangler_versions.latest.to_string());
let new_version_available = format!(
"A new version of Wrangler ({}) is available!",
latest_version
);
let update_message = "You can learn more about updating here:".to_string();
let update_docs_url = styles::url(
"https://developers.cloudflare.com/workers/cli-wrangler/install-update#update",
);

StdOut::billboard(&format!(
"{}\n{}\n{}",
new_version_available, update_message, update_docs_url
));
}
}
Err(e) => log::debug!("could not determine if update is needed:\n{}", e),
});

receiver
}
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 9c1d737

Please sign in to comment.