SHA256
Quicksave, working on update logic and stuff
This commit is contained in:
+92
-47
@@ -18,17 +18,17 @@
|
||||
hive-up daemon logic
|
||||
*/
|
||||
|
||||
use std::os::unix::net::UnixDatagram;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
use std::os::unix::net::UnixDatagram;
|
||||
|
||||
use crate::hive_config;
|
||||
use crate::hive_config::HiveConfig;
|
||||
use crate::hive_db::{self, HiveDb};
|
||||
use crate::hive_config;
|
||||
use crate::hive_intercom::{self, BzzReq, BzzResp, IntercomError};
|
||||
use crate::hive_system;
|
||||
use crate::hive_intercom::{self, IntercomError, BzzReq, BzzResp};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DaemonError {
|
||||
@@ -58,7 +58,6 @@ impl From<hive_config::ConfigError> for DaemonError {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<hive_system::ExecCommandError> for DaemonError {
|
||||
fn from(err: hive_system::ExecCommandError) -> Self {
|
||||
DaemonError::ExecCommandError(err)
|
||||
@@ -88,54 +87,94 @@ fn get_timestamp_sec() -> u64 {
|
||||
}
|
||||
|
||||
/// bloking function. Bloaks for configured socker recv timeout
|
||||
fn process_remoute(bzz_db: &mut HiveDb, listening_socket: &UnixDatagram, recv_buf: &mut [u8]) -> Result<(), DaemonError> {
|
||||
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 => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
BzzReq::CheckForUpdates => {
|
||||
let resp = BzzResp::Working(1);
|
||||
if let Err(ex) =
|
||||
hive_intercom::socket_send(&listening_socket, &client_addr, resp)
|
||||
{
|
||||
println!("Error while ansvering cli request: {}", ex);
|
||||
} else {
|
||||
// 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
|
||||
);
|
||||
// send evailable updates numbet to client
|
||||
let resp = BzzResp::UpdatesAvailable(bzz_db.updates_available);
|
||||
if let Err(ex) = hive_intercom::socket_send(&listening_socket,&client_addr, resp) {
|
||||
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(())
|
||||
}
|
||||
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> {
|
||||
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();
|
||||
@@ -150,7 +189,7 @@ fn do_local_work(config: &HiveConfig, bzz_db: &mut HiveDb)-> Result<(), DaemonEr
|
||||
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
|
||||
// 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,
|
||||
@@ -160,14 +199,21 @@ fn do_local_work(config: &HiveConfig, bzz_db: &mut HiveDb)-> Result<(), DaemonEr
|
||||
}
|
||||
};
|
||||
hive_db::write_db(&bzz_db)?;
|
||||
println!("Found {} packages that needs to be updated.", bzz_db.updates_available);
|
||||
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);}
|
||||
Ok(()) => {
|
||||
println!("Updates downloaded...");
|
||||
}
|
||||
Err(ex) => {
|
||||
println!("Error downloading updates: {}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,7 +230,6 @@ pub fn run(
|
||||
term_flag: Arc<AtomicBool>,
|
||||
reload_flag: Arc<AtomicBool>,
|
||||
) -> Result<(), DaemonError> {
|
||||
|
||||
// reading db from disk
|
||||
let mut bzz_db = hive_db::read_db()?;
|
||||
|
||||
@@ -194,7 +239,7 @@ pub fn run(
|
||||
// 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 {
|
||||
process_remoute(&mut bzz_db, &listening_socket, recv_buf.as_mut_slice())?;
|
||||
|
||||
Reference in New Issue
Block a user