@@ -52,6 +52,10 @@ struct Opts {
52
52
53
53
#[ derive( Subcommand ) ]
54
54
enum Commands {
55
+ /// Daemon subcommand
56
+ /// Starts aw-sync as a daemon, which will sync every 5 minutes.
57
+ Daemon { } ,
58
+
55
59
/// Sync subcommand (basic)
56
60
///
57
61
/// Pulls remote buckets then pushes local buckets.
@@ -125,7 +129,12 @@ fn main() -> Result<(), Box<dyn Error>> {
125
129
let client = AwClient :: new ( & opts. host , port, "aw-sync" ) ?;
126
130
127
131
// if opts.command is None, then we're using the default subcommand (Sync)
128
- match opts. command . unwrap_or ( Commands :: Sync { host : None } ) {
132
+ match opts. command . unwrap_or ( Commands :: Daemon { } ) {
133
+ // Start daemon
134
+ Commands :: Daemon { } => {
135
+ info ! ( "Starting daemon..." ) ;
136
+ daemon ( & client) ?;
137
+ }
129
138
// Perform basic sync
130
139
Commands :: Sync { host } => {
131
140
// Pull
@@ -144,7 +153,7 @@ fn main() -> Result<(), Box<dyn Error>> {
144
153
145
154
// Push
146
155
info ! ( "Pushing local data" ) ;
147
- sync_wrapper:: push ( & client)
156
+ sync_wrapper:: push ( & client) ?
148
157
}
149
158
// Perform two-way sync
150
159
Commands :: SyncAdvanced {
@@ -178,12 +187,12 @@ fn main() -> Result<(), Box<dyn Error>> {
178
187
start : start_date,
179
188
} ;
180
189
181
- sync:: sync_run ( & client, & sync_spec, mode)
190
+ sync:: sync_run ( & client, & sync_spec, mode) ?
182
191
}
183
192
184
193
// List all buckets
185
- Commands :: List { } => sync:: list_buckets ( & client) ,
186
- } ? ;
194
+ Commands :: List { } => sync:: list_buckets ( & client) ? ,
195
+ }
187
196
188
197
// Needed to give the datastores some time to commit before program is shut down.
189
198
// 100ms isn't actually needed, seemed to work fine with as little as 10ms, but I'd rather give
@@ -192,3 +201,17 @@ fn main() -> Result<(), Box<dyn Error>> {
192
201
193
202
Ok ( ( ) )
194
203
}
204
+
205
+ fn daemon ( client : & AwClient ) -> Result < ( ) , Box < dyn Error > > {
206
+ loop {
207
+ info ! ( "Pulling from all hosts" ) ;
208
+ sync_wrapper:: pull_all ( client) ?;
209
+
210
+ info ! ( "Pushing local data" ) ;
211
+ sync_wrapper:: push ( client) ?;
212
+
213
+ info ! ( "Sync pass done, sleeping for 5 minutes" ) ;
214
+
215
+ std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 300 ) ) ;
216
+ }
217
+ }
0 commit comments