3
3
//! This daemon will start web server, track new packages and build them
4
4
5
5
6
- use std:: env;
7
- use libc:: fork;
6
+ use std:: { env, thread} ;
8
7
use std:: process:: exit;
9
8
use std:: fs:: File ;
10
9
use std:: io:: Write ;
11
- use std:: thread;
12
10
use std:: time:: Duration ;
13
11
use std:: path:: PathBuf ;
12
+ use libc:: fork;
14
13
use time;
15
14
use DocBuilderOptions ;
16
15
use DocBuilder ;
@@ -29,27 +28,20 @@ pub fn start_daemon() {
29
28
env:: var ( v) . expect ( "Environment variable not found" ) ;
30
29
}
31
30
32
- info ! ( "Starting cratesfyi {} daemon" , :: BUILD_VERSION ) ;
31
+ // check paths once
32
+ opts ( ) . check_paths ( ) . unwrap ( ) ;
33
33
34
34
// fork the process
35
35
let pid = unsafe { fork ( ) } ;
36
36
if pid > 0 {
37
37
let mut file = File :: create ( DAEMON_PID_FILE_PATH ) . expect ( "Failed to create pid file" ) ;
38
38
writeln ! ( & mut file, "{}" , pid) . expect ( "Failed to write pid" ) ;
39
39
40
- info ! ( "cratesfyi daemon started on: {}" , pid) ;
40
+ info ! ( "cratesfyi {} daemon started on: {}" , :: BUILD_VERSION , pid) ;
41
41
exit ( 0 ) ;
42
42
}
43
43
44
44
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
-
53
45
// check new crates every 5 minutes
54
46
thread:: spawn ( move || {
55
47
loop {
@@ -67,11 +59,29 @@ pub fn start_daemon() {
67
59
thread:: spawn ( move || {
68
60
loop {
69
61
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
+
70
77
debug ! ( "Building new crates" ) ;
71
- let mut doc_builder = DocBuilder :: new ( opts ( ) ) ;
72
78
if let Err ( e) = doc_builder. build_packages_queue ( ) {
73
79
error ! ( "Failed build new crates: {}" , e) ;
74
80
}
81
+
82
+ if let Err ( e) = doc_builder. save_cache ( ) {
83
+ error ! ( "Failed to save cache: {}" , e) ;
84
+ }
75
85
}
76
86
} ) ;
77
87
@@ -96,3 +106,11 @@ pub fn start_daemon() {
96
106
info ! ( "Starting web server" ) ;
97
107
:: start_web_server ( None ) ;
98
108
}
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