added update runner thread

This commit is contained in:
J-onasJones 2024-02-12 03:46:08 +01:00
parent bdfd5a74a6
commit 7bee1f5bae
3 changed files with 82 additions and 46 deletions

View file

@ -1,4 +1,7 @@
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;
@ -10,10 +13,38 @@ 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();
server::serve().await;
// 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);
}

View file

@ -2,13 +2,18 @@ mod builtin;
mod debug;
mod kcomebacks;
mod projects;
mod update;
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;
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 update::get_run_routes as get_v1_updates_routes;
pub use run::get_run_routes as get_v1_updates_routes;
use warp::Filter;

View file

@ -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<impl warp::Reply, warp::Rejection> {
}
fn setup() -> Result<(), git2::Error> {
pub 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 @@ fn setup() -> Result<(), git2::Error> {
// run_command with python file and args as parameters
fn run_kcomebacks_command() -> Result<(), std::io::Error> {
let (tx, mut rx) = mpsc::channel(1);
pub 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 @@ 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();
});
@ -171,8 +171,8 @@ fn run_kcomebacks_command() -> Result<(), std::io::Error> {
Ok(())
}
fn run_projects_command() -> Result<(), std::io::Error> {
let (tx, mut rx) = mpsc::channel(1);
pub 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 @@ 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();
});
@ -211,8 +211,8 @@ fn run_projects_command() -> Result<(), std::io::Error> {
Ok(())
}
fn run_likedsongs_command() -> Result<(), std::io::Error> {
let (tx, mut rx) = mpsc::channel(1);
pub 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 @@ 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();
});