| 356 | } |
| 357 | |
| 358 | pub async fn run(&mut self) { |
| 359 | if let Err(err) = self.init().await { |
| 360 | error!("Encountered error during init: {}", err); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | async fn shutdown_signal() { |
| 365 | futures_util::future::select( |
| 366 | tokio::signal::ctrl_c().map(|_| ()).boxed(), |
| 367 | #[cfg(unix)] |
| 368 | tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) |
| 369 | .unwrap() |
| 370 | .recv() |
| 371 | .map(|_| ()) |
| 372 | .boxed(), |
| 373 | #[cfg(not(unix))] |
| 374 | futures_util::future::pending::<()>(), |
| 375 | ) |
| 376 | .await; |
| 377 | } |
| 378 | |
| 379 | loop { |
| 380 | if let Err(err) = self.do_sync().await { |
| 381 | warn!("Encountered error during sync: {}", err); |
| 382 | } |
| 383 | |
| 384 | match futures_util::future::select( |
| 385 | tokio::time::sleep(Duration::from_secs_f64(self.app_state.config.sync_interval)) |
| 386 | .boxed(), |
| 387 | shutdown_signal().boxed(), |
| 388 | ) |
| 389 | .await |
| 390 | { |
| 391 | futures_util::future::Either::Left(_) => {} |
| 392 | futures_util::future::Either::Right(_) => break, |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | log::info!("Shutting down..."); |
| 397 | for source in &mut self.sources { |
| 398 | _ = source.shutdown().await.log_error(std::module_path!(), |e| { |
| 399 | format!("Failed to gracefully shutdown source: {}", e) |
| 400 | }); |
| 401 | } |
| 402 | } |
| 403 | } |