diff --git a/api/src/services/gtfs_pull.rs b/api/src/services/gtfs_pull.rs index 47e1794..a6ded89 100644 --- a/api/src/services/gtfs_pull.rs +++ b/api/src/services/gtfs_pull.rs @@ -112,8 +112,16 @@ impl GtfsPullService { } pub fn update_gtfs_data(state: Arc>) -> anyhow::Result<()> { - let mut l_state = state.lock().unwrap(); - let files = l_state.gtfs_files.clone(); + let (files, tmp_dir) = { + let l_state = state.lock().unwrap(); + (l_state.gtfs_files.clone(), l_state.tmp_dir.clone()) + }; + + let mut td = TransitData { + routes: HashMap::new(), + agencies: HashMap::new(), + trips: HashMap::new() + }; for gtfs_file in files.iter() { let gtfs = if let Some(subzip) = gtfs_file.source.subzip.clone() { @@ -121,9 +129,9 @@ impl GtfsPullService { let res = reqwest::blocking::get(gtfs_file.source.uri.clone())?; let outer_archive = res.bytes()?; let mut archive = ZipArchive::new(Cursor::new(outer_archive))?; - archive.extract(l_state.tmp_dir.clone())?; + archive.extract(tmp_dir.clone())?; - let mut file_path = l_state.tmp_dir.clone(); + let mut file_path = tmp_dir.clone(); file_path.push(subzip.clone()); gtfs_structures::Gtfs::new(file_path.to_str().unwrap())? @@ -136,7 +144,7 @@ impl GtfsPullService { for agency in >fs.agencies { if let Some(a_id) = &agency.id { - l_state.transit_data.agencies.insert(a_id.clone(), libseptastic::agency::Agency{ + td.agencies.insert(a_id.clone(), libseptastic::agency::Agency{ id: a_id.clone(), name: agency.name.clone() }); @@ -149,7 +157,7 @@ impl GtfsPullService { for route in >fs.routes { let agency = route.1.agency_id.as_ref() - .and_then(|agency_id| l_state.transit_data.agencies.get(agency_id)) + .and_then(|agency_id| td.agencies.get(agency_id)) .map(|agency| agency.clone()); let global_rt_id = match &agency { @@ -168,7 +176,7 @@ impl GtfsPullService { } }; - l_state.transit_data.routes.insert(global_rt_id.clone(), libseptastic::route::Route{ + td.routes.insert(global_rt_id.clone(), libseptastic::route::Route{ name: rt_name, short_name: match route.1.short_name.clone() { Some(x) => x, @@ -220,19 +228,23 @@ impl GtfsPullService { service_id: trip.1.service_id.clone() }; - if let Some(trip_arr) = l_state.transit_data.trips.get_mut(&global_rt_id) { + if let Some(trip_arr) = td.trips.get_mut(&global_rt_id) { trip_arr.push(trip); } else { - l_state.transit_data.trips.insert(global_rt_id, vec![trip]); + td.trips.insert(global_rt_id, vec![trip]); } } info!("Added {} routes", gtfs.routes.len()); } - l_state.ready = true; - info!("Finished initial sync, ready state is true"); + let mut l_state = state.lock().unwrap(); + l_state.transit_data = td; + if !l_state.ready { + l_state.ready = true; + info!("Finished initial sync, ready state is true"); + } Ok(()) }