SHARE
TWEET

Untitled

a guest Jan 24th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn service_thread() {
  2.     let one_day = Duration::from_secs(24 * 3600);
  3.     let five_minutes = Duration::from_secs(5 * 60);
  4.  
  5.     loop {
  6.         thread::sleep(try_update(five_minutes, one_day));
  7.     }
  8. }
  9.  
  10. fn try_update(sleep_after_fail: Duration, sleep_after_success: Duration) -> Duration {
  11.     let update_file_path = get_update_file_path();
  12.     let update_file_path = match update_file_path {
  13.         Ok(value) => value,
  14.         Err(_) => return sleep_after_fail,
  15.     };
  16.  
  17.     let file = fs::File::open(update_file_path);
  18.     let mut file = match file {
  19.         Ok(value) => value,
  20.         Err(_) => return sleep_after_fail,
  21.     };
  22.  
  23.     let update_status = UpdateStatus::load(&file);
  24.     let update_status = match update_status {
  25.         Ok(value) => value,
  26.         Err(_) => {
  27.             file.set_len(0);
  28.             UpdateStatus::from_result(update_status).store(&mut file);
  29.             return sleep_after_fail;
  30.         }
  31.     };
  32.  
  33.     let passed_since_update = update_status.last_updated.elapsed();
  34.     let passed_since_update = match passed_since_update {
  35.         Ok(value) => value,
  36.         Err(_) => {
  37.             file.set_len(0);
  38.             UpdateStatus::from_result(passed_since_update).store(&mut file);
  39.             return sleep_after_fail;
  40.         }
  41.     };
  42.  
  43.     if update_status.success && passed_since_update < sleep_after_success {
  44.         thread::sleep(sleep_after_success - passed_since_update);
  45.     }
  46.  
  47.     let update_status = UpdateStatus::from_result(update());
  48.     if update_status.success {
  49.         return sleep_after_success;
  50.     };
  51.  
  52.     sleep_after_fail
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top