/// Control scraping of regions in regular intervals
Control scraping of regions in regular intervals
Control scraping of regions in regular intervals
fn start_schedule_loop(client: esi::Client, uni: universe::Universe, order_store: store::Store) {
    thread::spawn(move || {
        // Build initial schedule
        let regions = uni.get_market_regions();
        let mut schedule: FnvHashMap<RegionID, DateTime<Utc>> = FnvHashMap::default();
        let mut rng = thread_rng();
        for region in regions {
            let random: i64 = rng.gen_range(0, 300);
            let random_time = Utc::now() + Duration::seconds(random);
            schedule.insert(region, random_time);
        }
        // Convert number to milliseconds
        let milliseconds = |milliseconds| StandardDuration::from_millis(milliseconds);
        // Define tick intervals
        let hourly = milliseconds(60 * 60 * 1_000);
        let five_times_per_second = milliseconds(200);
        // Create channels
        let region_update = channel::tick(hourly); // hourly (backend refreshes daily)
        let run_regions = channel::tick(five_times_per_second);
        let (send_reschedule, receive_reschedule) =
            channel::unbounded::<(RegionID, DateTime<Utc>)>();
        loop {
            select! {
                recv(region_update) -> _message => {
                    let regions = uni.get_market_regions();
                    for region in regions {
                        let random: i64 = rng.gen_range(0, 300);
                        let random_time = Utc::now() + Duration::seconds(random);
                        schedule.entry(region).or_insert(random_time);
                    }
                },
                recv(run_regions) -> _message => {
                    let now = &Utc::now();
                    let mut fetch: Vec<RegionID> = Vec::new();
                    for (region, time) in &schedule {
                        if time < now {
                            fetch.push(*region);
                        }
                    }
                    for region in fetch {
                        // Run in 10 minutes unless re-scheduled by self
                        schedule.insert(region, *now + Duration::seconds(300));
                        update_region(region, client.clone(), uni.clone(), order_store.clone(), send_reschedule.clone());
                    }
                },
                recv(receive_reschedule) -> data => {
                    if let Ok((region_id, run_at)) = data {
                        schedule.insert(region_id, run_at);
                    }
                },
            }
        }
    });
}
fn start_schedule_loop ( client : esi :: Client , uni : universe :: Universe , order_store : store :: Store ) { thread :: spawn ( move | | { // Build initial schedule let regions = uni . get_market_regions ( ) ; let mut schedule : FnvHashMap < RegionID , DateTime < Utc > > = FnvHashMap :: default ( ) ; let mut rng = thread_rng ( ) ; for region in regions { let random : i64 = rng . gen_range ( 0 , 300 ) ; let random_time = Utc :: now ( ) + Duration :: seconds ( random ) ; schedule . insert ( region , random_time ) ; } // Convert number to milliseconds let milliseconds = | milliseconds | StandardDuration :: from_millis ( milliseconds ) ; // Define tick intervals let hourly = milliseconds ( 60 * 60 * 1_000 ) ; let five_times_per_second = milliseconds ( 200 ) ; // Create channels let region_update = channel :: tick ( hourly ) ; // hourly (backend refreshes daily) let run_regions = channel :: tick ( five_times_per_second ) ; let ( send_reschedule , receive_reschedule ) = channel :: unbounded :: < ( RegionID , DateTime < Utc > ) > ( ) ; loop { select ! { recv ( region_update ) _message { let regions uni get_market_regions ( ) for region in regions { let random i64 rng gen_range ( 0 300 ) let random_time Utc now ( ) Duration seconds ( random ) schedule entry ( region ) or_insert ( random_time ) } } recv ( run_regions ) _message { let now Utc now ( ) let mut fetch Vec RegionID Vec new ( ) for ( region time ) in schedule { if time now { fetch push ( region ) } } for region in fetch { // Run in 10 minutes unless re-scheduled by self schedule insert ( region now Duration seconds ( 300 ) ) update_region ( region client clone ( ) uni clone ( ) order_store clone ( ) send_reschedule clone ( ) ) } } recv ( receive_reschedule ) data { if let Ok ( ( region_id run_at ) ) data { schedule insert ( region_id run_at ) } } } } } ) ; }
Control scraping of regions in regular intervals
Control scraping of regions in regular intervals
client
esi::Client
uni
universe::Universe
order_store
store::Store