diff --git a/src/main.rs b/src/main.rs index a55572c..86eb624 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,4 @@ use dotenv::dotenv; -use tokio::time::{sleep, Duration}; -use v1::{run_kcomebacks_command, run_likedsongs_command, run_projects_command}; -use std::fs; pub mod v1; pub mod logger; @@ -13,38 +10,10 @@ pub use logger::Logger; pub use tools::parse_ip; -async fn periodic_script_runner() { - loop { - Logger::info("Running periodic scripts..."); - // Run all Functions - let _ = run_kcomebacks_command(); - let _ = run_projects_command(); - let _ = run_likedsongs_command(); - - // Sleep for 6 hours - sleep(Duration::from_secs(6 * 60 * 60)).await; - } -} - #[tokio::main(flavor = "current_thread")] async fn main() { - // Load .env file + // load .env file dotenv().ok(); - // Start the api - let server_task = tokio::spawn(async { - server::serve().await; - }); - - // periodic script runner - let second_task = tokio::spawn(async { - // check if the local repository exists, if not, clone it - if !fs::metadata("./resources/turbo_octo_potato").is_ok() { - v1::run_setup().unwrap(); - }; - periodic_script_runner().await; - }); - - // Wait for both tasks to complete - let _ = tokio::try_join!(server_task, second_task); + server::serve().await; } diff --git a/src/v1/mod.rs b/src/v1/mod.rs index ff4bf8f..d0ff506 100644 --- a/src/v1/mod.rs +++ b/src/v1/mod.rs @@ -2,18 +2,13 @@ mod builtin; mod debug; mod kcomebacks; mod projects; -mod run; - -pub use run::setup as run_setup; -pub use run::run_kcomebacks_command; -pub use run::run_projects_command; -pub use run::run_likedsongs_command; +mod update; pub use builtin::get_builtin_routes as get_v1_builtin_routes; pub use debug::get_debug_routes as get_v1_debug_routes; pub use kcomebacks::get_kcomebacks_routes as get_v1_kcomebacks_routes; pub use projects::get_project_routes as get_v1_project_routes; -pub use run::get_run_routes as get_v1_updates_routes; +pub use update::get_run_routes as get_v1_updates_routes; use warp::Filter; diff --git a/src/v1/run/mod.rs b/src/v1/update/mod.rs similarity index 75% rename from src/v1/run/mod.rs rename to src/v1/update/mod.rs index 9861d53..15a7f01 100644 --- a/src/v1/run/mod.rs +++ b/src/v1/update/mod.rs @@ -1,10 +1,10 @@ use std::fs; -// use std::io::BufRead; +use std::io::BufRead; use std::process::{Stdio, Command}; use serde::{Deserialize, Serialize}; use serde_json::json; -// use tokio::sync::mpsc; +use tokio::sync::mpsc; use tokio::task; use warp::Filter; use crate::error_responses::InternalServerError; @@ -80,7 +80,7 @@ async fn sync_liked_songs() -> Result { } -pub fn setup() -> Result<(), git2::Error> { +fn setup() -> Result<(), git2::Error> { let repository_url = "https://github.com/JonasunderscoreJones/turbo-octo-potato.git"; let local_directory = "resources/turbo_octo_potato"; @@ -131,8 +131,8 @@ pub fn setup() -> Result<(), git2::Error> { // run_command with python file and args as parameters -pub fn run_kcomebacks_command() -> Result<(), std::io::Error> { - // let (tx, mut rx) = mpsc::channel(1); +fn run_kcomebacks_command() -> Result<(), std::io::Error> { + let (tx, mut rx) = mpsc::channel(1); task::spawn_blocking(move || { let mut child = Command::new("python3") @@ -143,20 +143,20 @@ pub fn run_kcomebacks_command() -> Result<(), std::io::Error> { .spawn() .expect("failed to execute child"); - // let stdout = child.stdout.as_mut().unwrap(); + let stdout = child.stdout.as_mut().unwrap(); - // let mut reader = std::io::BufReader::new(stdout); + let mut reader = std::io::BufReader::new(stdout); - // let mut line = String::new(); + let mut line = String::new(); - // loop { - // let len = reader.read_line(&mut line).unwrap(); - // if len == 0 { - // break; - // } - // tx.blocking_send(line.clone()).unwrap(); - // line.clear(); - // } + loop { + let len = reader.read_line(&mut line).unwrap(); + if len == 0 { + break; + } + tx.blocking_send(line.clone()).unwrap(); + line.clear(); + } child.wait().unwrap(); }); @@ -166,13 +166,13 @@ pub fn run_kcomebacks_command() -> Result<(), std::io::Error> { // Logger::info(&format!("[/v1/kcomebacks/update]: {}", line)); // } // }); - Logger::info("Updating kcomebacks..."); + Logger::info("kcomebacks updated"); Ok(()) } -pub fn run_projects_command() -> Result<(), std::io::Error> { - // let (tx, mut rx) = mpsc::channel(1); +fn run_projects_command() -> Result<(), std::io::Error> { + let (tx, mut rx) = mpsc::channel(1); task::spawn_blocking(move || { let mut child = Command::new("python3") @@ -183,20 +183,20 @@ pub fn run_projects_command() -> Result<(), std::io::Error> { .spawn() .expect("failed to execute child"); - // let stdout = child.stdout.as_mut().unwrap(); + let stdout = child.stdout.as_mut().unwrap(); - // let mut reader = std::io::BufReader::new(stdout); + let mut reader = std::io::BufReader::new(stdout); - // let mut line = String::new(); + let mut line = String::new(); - // loop { - // let len = reader.read_line(&mut line).unwrap(); - // if len == 0 { - // break; - // } - // tx.blocking_send(line.clone()).unwrap(); - // line.clear(); - // } + loop { + let len = reader.read_line(&mut line).unwrap(); + if len == 0 { + break; + } + tx.blocking_send(line.clone()).unwrap(); + line.clear(); + } child.wait().unwrap(); }); @@ -206,13 +206,13 @@ pub fn run_projects_command() -> Result<(), std::io::Error> { // Logger::info(&format!("[/v1/projects/update]: {}", line)); // } // }); - Logger::info("Updating projects..."); + Logger::info("projects updated"); Ok(()) } -pub fn run_likedsongs_command() -> Result<(), std::io::Error> { - // let (tx, mut rx) = mpsc::channel(1); +fn run_likedsongs_command() -> Result<(), std::io::Error> { + let (tx, mut rx) = mpsc::channel(1); task::spawn_blocking(move || { let mut child = Command::new("python3") @@ -222,20 +222,20 @@ pub fn run_likedsongs_command() -> Result<(), std::io::Error> { .spawn() .expect("failed to execute child"); - // let stdout = child.stdout.as_mut().unwrap(); + let stdout = child.stdout.as_mut().unwrap(); - // let mut reader = std::io::BufReader::new(stdout); + let mut reader = std::io::BufReader::new(stdout); - // let mut line = String::new(); + let mut line = String::new(); - // loop { - // let len = reader.read_line(&mut line).unwrap(); - // if len == 0 { - // break; - // } - // tx.blocking_send(line.clone()).unwrap(); - // line.clear(); - // } + loop { + let len = reader.read_line(&mut line).unwrap(); + if len == 0 { + break; + } + tx.blocking_send(line.clone()).unwrap(); + line.clear(); + } child.wait().unwrap(); }); @@ -245,7 +245,7 @@ pub fn run_likedsongs_command() -> Result<(), std::io::Error> { // Logger::info(&format!("[/v1/synclikedsongs]: {}", line)); // } // }); - Logger::info("Syncing liked songs..."); + Logger::info("liked songs synced"); Ok(()) } \ No newline at end of file