Skip to content

Commit 0222cd3

Browse files
committed
Update daemon
1 parent 9826ca0 commit 0222cd3

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/utils/daemon.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
//! This daemon will start web server, track new packages and build them
44
55

6-
use std::env;
7-
use libc::fork;
6+
use std::{env, thread};
87
use std::process::exit;
98
use std::fs::File;
109
use std::io::Write;
11-
use std::thread;
1210
use std::time::Duration;
1311
use std::path::PathBuf;
12+
use libc::fork;
1413
use time;
1514
use DocBuilderOptions;
1615
use DocBuilder;
@@ -29,27 +28,20 @@ pub fn start_daemon() {
2928
env::var(v).expect("Environment variable not found");
3029
}
3130

32-
info!("Starting cratesfyi {} daemon", ::BUILD_VERSION);
31+
// check paths once
32+
opts().check_paths().unwrap();
3333

3434
// fork the process
3535
let pid = unsafe { fork() };
3636
if pid > 0 {
3737
let mut file = File::create(DAEMON_PID_FILE_PATH).expect("Failed to create pid file");
3838
writeln!(&mut file, "{}", pid).expect("Failed to write pid");
3939

40-
info!("cratesfyi daemon started on: {}", pid);
40+
info!("cratesfyi {} daemon started on: {}", ::BUILD_VERSION, pid);
4141
exit(0);
4242
}
4343

4444

45-
fn opts() -> DocBuilderOptions {
46-
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX")
47-
.expect("CRATESFYI_PREFIX environment variable not found"));
48-
let opts = DocBuilderOptions::from_prefix(prefix);
49-
opts.check_paths().unwrap();
50-
opts
51-
}
52-
5345
// check new crates every 5 minutes
5446
thread::spawn(move || {
5547
loop {
@@ -67,11 +59,29 @@ pub fn start_daemon() {
6759
thread::spawn(move || {
6860
loop {
6961
thread::sleep(Duration::from_secs(180));
62+
63+
let mut opts = opts();
64+
opts.skip_if_exists = true;
65+
66+
// check lock file
67+
if opts.prefix.join("cratesfyi.lock").exists() {
68+
warn!("Lock file exits, skipping building new crates");
69+
}
70+
71+
let mut doc_builder = DocBuilder::new(opts);
72+
if let Err(e) = doc_builder.load_cache() {
73+
error!("Failed to load cache: {}", e);
74+
continue;
75+
}
76+
7077
debug!("Building new crates");
71-
let mut doc_builder = DocBuilder::new(opts());
7278
if let Err(e) = doc_builder.build_packages_queue() {
7379
error!("Failed build new crates: {}", e);
7480
}
81+
82+
if let Err(e) = doc_builder.save_cache() {
83+
error!("Failed to save cache: {}", e);
84+
}
7585
}
7686
});
7787

@@ -96,3 +106,11 @@ pub fn start_daemon() {
96106
info!("Starting web server");
97107
::start_web_server(None);
98108
}
109+
110+
111+
112+
fn opts() -> DocBuilderOptions {
113+
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX")
114+
.expect("CRATESFYI_PREFIX environment variable not found"));
115+
DocBuilderOptions::from_prefix(prefix)
116+
}

0 commit comments

Comments
 (0)