Skip to content

Commit 5e8f0c4

Browse files
committed
feat(daemon): first daemon app with made up checks
1 parent a926fe0 commit 5e8f0c4

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/bins/netpulsed.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
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+
19
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+
}
353
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/// How long to wait until considering a connection as timed out, in milliseconds
22
pub const TIMEOUT_MS: u16 = 30_000;
33

4+
pub const DAEMON_PID_FILE: &str = "/run/netpulse.pid";
5+
pub const DAEMON_LOG_ERR: &str = "/var/log/netpulse.err";
6+
pub const DAEMON_LOG_INF: &str = "/var/log/netpulse.log";
7+
48
pub mod errors;
59
pub mod records;
610
pub mod store;

0 commit comments

Comments
 (0)