Skip to content

Commit

Permalink
feat: disable downloader with console
Browse files Browse the repository at this point in the history
  • Loading branch information
Devdutt Shenoi committed May 2, 2024
1 parent b9c79d6 commit 7ba5382
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 4 additions & 2 deletions uplink/src/collector/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ use std::{io::Write, path::PathBuf};

use crate::{base::bridge::BridgeTx, config::DownloaderConfig, Action, ActionResponse, Config};

pub static STOP_DOWNLOAD: AtomicBool = AtomicBool::new(false);
// Stops from downloading if true
pub static DOWNLOADER_DISABLED: AtomicBool = AtomicBool::new(false);

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -256,7 +257,8 @@ impl FileDownloader {
// Download and store to disk by streaming as chunks
loop {
// Checks if downloader is disabled by user or not
if STOP_DOWNLOAD.load(Ordering::Acquire) {
if DOWNLOADER_DISABLED.load(Ordering::Acquire) {
// async to ensure download can be cancelled during sleep
sleep(Duration::from_secs(1)).await;
}
let Some(item) = stream.next().await else { break };
Expand Down
34 changes: 33 additions & 1 deletion uplink/src/console.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::atomic::Ordering;

use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::post, Router};
use log::info;
use uplink::base::CtrlTx;
use uplink::{base::CtrlTx, collector::downloader::DOWNLOADER_DISABLED};

use crate::ReloadHandle;

Expand All @@ -18,6 +20,8 @@ pub async fn start(port: u16, reload_handle: ReloadHandle, ctrl_tx: CtrlTx) {
let app = Router::new()
.route("/logs", post(reload_loglevel))
.route("/shutdown", post(shutdown))
.route("/disable_downloader", post(disable_downloader))
.route("/enable_downloader", post(enable_downloader))
.with_state(state);

axum::Server::bind(&address.parse().unwrap()).serve(app.into_make_service()).await.unwrap();
Expand All @@ -38,3 +42,31 @@ async fn shutdown(State(state): State<StateHandle>) -> impl IntoResponse {

StatusCode::OK
}

// Stops downloader from downloading even if it was already stopped
async fn disable_downloader() -> impl IntoResponse {
info!("Downloader stopped");
// Shouldn't panic as always sets to true
if DOWNLOADER_DISABLED
.fetch_update(Ordering::Release, Ordering::Acquire, |_| Some(true))
.unwrap()
{
StatusCode::ACCEPTED
} else {
StatusCode::OK
}
}

// Start downloader back up even if it was already not stopped
async fn enable_downloader() -> impl IntoResponse {
info!("Downloader started");
// Shouldn't panic as always sets to true
if DOWNLOADER_DISABLED
.fetch_update(Ordering::Release, Ordering::Acquire, |_| Some(false))
.unwrap()
{
StatusCode::OK
} else {
StatusCode::ACCEPTED
}
}

0 comments on commit 7ba5382

Please sign in to comment.