SHA256
Saving more work.
This commit is contained in:
+131
-34
@@ -18,22 +18,23 @@
|
||||
hive-up daemon logic
|
||||
*/
|
||||
|
||||
use std::fs;
|
||||
use std::process::Command;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
use time_format;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use std::os::unix::net::UnixDatagram;
|
||||
|
||||
use crate::hive_config::HiveConfig;
|
||||
use crate::hive_db;
|
||||
use crate::hive_db::{self, HiveDb};
|
||||
use crate::hive_config;
|
||||
use crate::hive_system;
|
||||
use crate::hive_intercom::{self, IntercomError, BzzReq, BzzResp};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DaemonError {
|
||||
IoError(std::io::Error),
|
||||
DBError(hive_db::DBError),
|
||||
ConfigError(hive_config::ConfigError),
|
||||
ExecCommandError(hive_system::ExecCommandError),
|
||||
TimestampError,
|
||||
}
|
||||
@@ -51,6 +52,13 @@ impl From<hive_db::DBError> for DaemonError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<hive_config::ConfigError> for DaemonError {
|
||||
fn from(err: hive_config::ConfigError) -> Self {
|
||||
DaemonError::ConfigError(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<hive_system::ExecCommandError> for DaemonError {
|
||||
fn from(err: hive_system::ExecCommandError) -> Self {
|
||||
DaemonError::ExecCommandError(err)
|
||||
@@ -61,12 +69,9 @@ impl std::fmt::Display for DaemonError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
DaemonError::IoError(e) => write!(f, "IO Error: {}", e),
|
||||
DaemonError::DBError(e) => {
|
||||
write!(f, "Database error: {}", e)
|
||||
},
|
||||
DaemonError::ExecCommandError(e) => {
|
||||
write!(f, "Exec Command Error: {}", e)
|
||||
},
|
||||
DaemonError::DBError(e) => write!(f, "Database error: {}", e),
|
||||
DaemonError::ConfigError(e) => write!(f, "Config Error: {}", e),
|
||||
DaemonError::ExecCommandError(e) => write!(f, "Exec Command Error: {}", e),
|
||||
DaemonError::TimestampError => write!(f, "Timestamp Error"),
|
||||
}
|
||||
}
|
||||
@@ -82,41 +87,133 @@ fn get_timestamp_sec() -> u64 {
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
/// bloking function. Bloaks for configured socker recv timeout
|
||||
fn process_remoute(bzz_db: &mut HiveDb, listening_socket: &UnixDatagram, recv_buf: &mut [u8]) -> Result<(), DaemonError> {
|
||||
// waiting on socket recv_from untill timeout (also using this as a timer)
|
||||
match hive_intercom::socket_recv(&listening_socket, recv_buf) {
|
||||
// we get some request to process
|
||||
Ok((bzz_req, client_addr)) => {
|
||||
match bzz_req {
|
||||
// return awailable updates number
|
||||
BzzReq::UpdatesAvailable => {
|
||||
let resp = BzzResp::UpdatesAvailable(bzz_db.updates_available);
|
||||
if let Err(ex) = hive_intercom::socket_send(&listening_socket,&client_addr, resp) {
|
||||
println!("Error while ansvering cli request: {}", ex);
|
||||
}
|
||||
},
|
||||
// we should apply updates if any on reboot
|
||||
BzzReq::ScheduleUpdate => {
|
||||
// but only if we have actually any updates
|
||||
if bzz_db.updates_available > 0 {
|
||||
bzz_db.update_on_reboot = true;
|
||||
// keep it simple Kira
|
||||
// lets just return err from the loop if something wrong with db
|
||||
hive_db::write_db(&bzz_db)?;
|
||||
// signal remoute that evertyhing go OK
|
||||
if let Err(ex) = hive_intercom::socket_send(&listening_socket,&client_addr, BzzResp::Finish) {
|
||||
println!("Error while ansvering cli request: {}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
// if time out - do nothing
|
||||
Err(IntercomError::Timeout) => {
|
||||
Ok(())
|
||||
}
|
||||
// if some actual IO error then return it
|
||||
Err(IntercomError::IoError(ex)) => Err(ex.into()),
|
||||
// if other kinds of errors - log and continue
|
||||
Err(ex) => {
|
||||
println!("Got error while listenin to unix socket: {}", ex);
|
||||
print!("Sleeping for 1 secong and continue...");
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// doing what daemons usually beezz
|
||||
fn do_local_work(config: &HiveConfig, bzz_db: &mut HiveDb)-> Result<(), DaemonError> {
|
||||
// if updates check is turned on
|
||||
if config.update.updates_check_interval != 0 {
|
||||
let timestamp = get_timestamp_sec();
|
||||
|
||||
// calculate how many hours passed sinse last update
|
||||
let hours_since_update_check = timestamp
|
||||
.checked_sub(bzz_db.update_check_timestamp)
|
||||
.ok_or(DaemonError::TimestampError)?
|
||||
/ 3600;
|
||||
|
||||
// if more than updates_check_interval specifiyed in config - check for updates
|
||||
if hours_since_update_check > config.update.updates_check_interval {
|
||||
// perform update check
|
||||
println!("Cheking for updates...");
|
||||
// now that may fail because of bad internet and idealy we
|
||||
// want to just log error and retry in other time
|
||||
bzz_db.updates_available = match hive_system::check_updates() {
|
||||
Ok(updates_num) => updates_num,
|
||||
Err(ex) => {
|
||||
println!("Error while shecking for updates: {}", ex);
|
||||
0u32
|
||||
}
|
||||
};
|
||||
hive_db::write_db(&bzz_db)?;
|
||||
println!("Found {} packages that needs to be updated.", bzz_db.updates_available);
|
||||
|
||||
// if auto download for updates is enabled and there is some updates - download them
|
||||
if config.update.download_updates && bzz_db.updates_available != 0 {
|
||||
println!("Downloadin updates...");
|
||||
match hive_system::download_updates() {
|
||||
Ok(()) => {println!("Updates downloaded...");},
|
||||
Err(ex) => {println!("Error downloading updates: {}", ex);}
|
||||
}
|
||||
}
|
||||
|
||||
// updating las tcheck for updates timestamp in db
|
||||
bzz_db.update_check_timestamp = get_timestamp_sec();
|
||||
hive_db::write_db(&bzz_db)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
config: HiveConfig,
|
||||
mut bzz_config: HiveConfig,
|
||||
term_flag: Arc<AtomicBool>,
|
||||
reload_flag: Arc<AtomicBool>,
|
||||
) -> Result<(), DaemonError> {
|
||||
let duration = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time went backwards");
|
||||
|
||||
// reading db from disk
|
||||
let mut bzz_db = hive_db::read_db()?;
|
||||
|
||||
//duration.as_secs()
|
||||
let keep_going = true;
|
||||
// stop flag
|
||||
let mut keep_going = true;
|
||||
|
||||
//last_check.
|
||||
// init udp unix socket for communication with cli and such
|
||||
let listening_socket = hive_intercom::socket_bind(1)?;
|
||||
let mut recv_buf: Vec<u8> = Vec::with_capacity(1024);
|
||||
|
||||
// main loop (I love loops) (No please just do recursion properly)
|
||||
while keep_going {
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
process_remoute(&mut bzz_db, &listening_socket, recv_buf.as_mut_slice())?;
|
||||
do_local_work(&bzz_config, &mut bzz_db)?;
|
||||
// check termination flags
|
||||
if term_flag.load(Ordering::Relaxed) {
|
||||
println!("SIGTERM recieved - wrapping things up.");
|
||||
keep_going = false;
|
||||
}
|
||||
|
||||
// if updates check is turned on
|
||||
if config.update.updates_check_interval != 0 {
|
||||
let timestamp = get_timestamp_sec();
|
||||
let hours_since_update_check = timestamp
|
||||
.checked_sub(bzz_db.last_update_check)
|
||||
.ok_or(DaemonError::TimestampError)?
|
||||
/ 3600;
|
||||
|
||||
if hours_since_update_check > config.update.updates_check_interval {
|
||||
// perform update check
|
||||
let packages_num = hive_system::check_updates()?;
|
||||
// updating las tupdate timestamp in db
|
||||
bzz_db.last_update_check = get_timestamp_sec();
|
||||
hive_db::write_db(&bzz_db)?;
|
||||
}
|
||||
// checking reload signal flag
|
||||
if reload_flag.load(Ordering::Relaxed) {
|
||||
println!("SIGHUP recieved - reloading config and DB.");
|
||||
bzz_config = hive_config::read_config()?;
|
||||
bzz_db = hive_db::read_db()?;
|
||||
println!("Reloaded!");
|
||||
}
|
||||
}
|
||||
|
||||
println!("Exiting...");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user