From e2d2100ce9ba7ac61136bd3260eaaeb6647d6da4 Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 4 Sep 2024 15:52:57 +0300 Subject: [PATCH] local logs (nw) --- .gitignore | 1 + service-checker.sh | 13 ------------- src/main.rs | 28 +++++++++++++++++++++------- src/utils.rs | 2 +- 4 files changed, 23 insertions(+), 21 deletions(-) delete mode 100644 service-checker.sh diff --git a/.gitignore b/.gitignore index ea8c4bf..2a0038a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.idea \ No newline at end of file diff --git a/service-checker.sh b/service-checker.sh deleted file mode 100644 index 223ee28..0000000 --- a/service-checker.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -hostname=$1 -port=$2 - - if nc -z -w1 $hostname $port > /dev/null 2>&1; then - echo "Service $hostname:$port is running" - exit 0 - else - echo "Service $hostname:$port is unreachable" - exit 1 - fi - diff --git a/src/main.rs b/src/main.rs index a7a9261..6caa083 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,12 @@ mod prcs; mod utils; mod services; +// use tokio::fs::File; +use std::fs::{File, OpenOptions}; use tokio::sync::mpsc; use std::sync::Arc; use std::io::Write; +use std::process::Command; use chrono::Local; use env_logger::Builder; use log::{error, LevelFilter}; @@ -19,7 +22,16 @@ use utils::*; #[tokio::main(flavor = "multi_thread")] async fn main() { - // building logger with current output format + if Command::new("sh").args(["-c", "'mkdir /var/log/runner-rs/'"]).output().is_err() { + println!("Error: Run runner in sudo mode"); + } + + let log_target = Box::new(OpenOptions::new().create_new(true).append(true).open("/var/log/runner-rs/runner.log").unwrap_or_else(|_| { + println!("Error: Cannot create local log-file in /var/log/runner-rs, returning..."); + std::process::exit(1); + })); + + // building logger with current output format Builder::new() .format(move |buf, record|{ writeln!(buf, @@ -30,18 +42,20 @@ async fn main() { record.args(), ) }) + .target(env_logger::Target::Stdout) + .target(env_logger::Target::Pipe(log_target)) .filter(None, LevelFilter::Info) .init(); log::info!("Runner is configurating..."); // setting up redis connection \ - // then conf checks to choose the most actual \ + // then conf checks to choose the most actual \ let processes: Processes = get_actual_config().unwrap_or_else(|| { error!("No actual configuration for runner. Stopping..."); std::process::exit(101); }); - + log::info!("Current runner configuration: {}", &processes.date_of_creation); log::info!("Runner is ready. Initializing..."); @@ -53,13 +67,13 @@ async fn main() { for proc in processes.processes.iter() { log::info!("Process '{}' on stage: {}. Depends on {} file(s), {} service(s)", - proc.name, - proc.path, - proc.dependencies.files.len(), + proc.name, + proc.path, + proc.dependencies.files.len(), proc.dependencies.services.len() ); - // creating msg channel + // creating msg channel // can or should be executed in new thread let (tx, mut rx) = mpsc::channel::(1); diff --git a/src/utils.rs b/src/utils.rs index b28832d..ee18711 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,7 +21,7 @@ use crate::services::service_handler; static GET_ID_CMD : &'static str = r"cat /proc/self/mountinfo | grep '/docker/containers/' | head -1 | awk -F '/' '{print \$6}'"; -/// # async func to run 3 main daemons (now its more like tree-form than classiacl 0.1.0 form ) +/// # async func to run 3 main daemons (now it's more like tree-form than classical 0.1.0 form ) /// > hint : give mpsc with capacity 1 to jump over potential errors during running process /// ** in [developing](https://github.com/prplV/runner-rs "REPOSITORY") ** pub async fn run_daemons(