Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.36 KB | None | 0 0
  1. struct DoCache {
  2.     droplets: String, // Vec<Droplet>,
  3. }
  4. fn do_caching_loop(mut do_cache: Arc<RwLock<DoCache>>) {
  5.     let auth_token = "SECRET";
  6.     let domgr = DoManager::with_token(&auth_token);
  7.     loop {
  8.         info!("Getting Droplets");
  9.         let result: String = domgr.droplets().retrieve_json().unwrap();
  10.         let mut modify_data = do_cache.write().unwrap();
  11.         (*modify_data).droplets = result.clone();
  12.         std::thread::sleep(std::time::Duration::from_millis(50000));
  13.     }
  14. }
  15.  
  16. struct State {
  17.     do_cache: Arc<RwLock<DoCache>>,
  18.     threads: Vec<std::thread::JoinHandle<()>>,
  19. }
  20. impl State {
  21.     fn getDroplets(&self) -> String {
  22.         info!("Get Droplets A");
  23.         let cache = self.do_cache.read();
  24.         let mut do_cache = cache.unwrap();
  25.         (*do_cache).droplets.clone()
  26.  
  27.     }
  28. }
  29. fn main() {
  30.     init();
  31.  
  32.     let mut do_cache = Arc::new(RwLock::new(DoCache { droplets: String::from("") })); // vec![] }));
  33.     let mut state = State {
  34.         do_cache: do_cache.clone(),
  35.         threads: Vec::new(),
  36.     };
  37.     info!("Spawning DO Cache");
  38.     state.threads.push(std::thread::spawn(move || {
  39.         do_caching_loop(do_cache.clone());
  40.     }));
  41.     info!("Serving");
  42.  
  43.     Iron::new(move |_: &mut Request| Ok(Response::with((status::Ok, state.getDroplets()))))
  44.         .http("localhost:80")
  45.         .unwrap();
  46.  
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement