|
| 1 | +use std::fs::{self, File}; |
| 2 | +use std::time::{self, Duration}; |
| 3 | + |
| 4 | +use daemonize::Daemonize; |
| 5 | +use netpulse::records::{Check, CheckFlag}; |
| 6 | +use netpulse::store::Store; |
| 7 | +use netpulse::{DAEMON_LOG_ERR, DAEMON_LOG_INF}; |
| 8 | + |
1 | 9 | fn main() { |
2 | | - todo!() |
| 10 | + let path = Store::path(); |
| 11 | + let parent_path = path.parent().expect("store file has no parent directory"); |
| 12 | + println!("Parent: {parent_path:?}"); |
| 13 | + |
| 14 | + let logfile = File::create(DAEMON_LOG_INF).expect("could not open info logfile"); |
| 15 | + let errfile = File::create(DAEMON_LOG_ERR).expect("could not open error logfile"); |
| 16 | + |
| 17 | + fs::create_dir_all(parent_path).expect("could not create the store directory"); |
| 18 | + |
| 19 | + let daemonize = Daemonize::new() |
| 20 | + .pid_file("/run/netpulse.pid") |
| 21 | + .chown_pid_file(true) |
| 22 | + .working_directory(parent_path) |
| 23 | + .user("netpulse") |
| 24 | + .group("netpulse") |
| 25 | + .stdout(logfile) |
| 26 | + .stderr(errfile) |
| 27 | + .umask(0o027); // rwxr-x--- |
| 28 | + |
| 29 | + eprintln!("daemon setup done"); |
| 30 | + |
| 31 | + match daemonize.start() { |
| 32 | + Ok(_) => daemon(), |
| 33 | + Err(e) => { |
| 34 | + eprintln!("Error starting daemon: {:#}", e); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + eprintln!("end?") |
| 39 | +} |
| 40 | + |
| 41 | +fn daemon() { |
| 42 | + println!("starting daemon..."); |
| 43 | + let mut store = Store::load_or_create().expect("boom"); |
| 44 | + loop { |
| 45 | + store.add_check(Check::new( |
| 46 | + time::SystemTime::now(), |
| 47 | + CheckFlag::Success, |
| 48 | + None, |
| 49 | + )); |
| 50 | + store.save().expect("could not save"); |
| 51 | + std::thread::sleep(Duration::from_secs(5)); |
| 52 | + } |
3 | 53 | } |
0 commit comments