fix multi sync issue
All checks were successful
Create and publish a Docker image / build-and-push-image (push) Successful in 9m26s

This commit is contained in:
Nicholas Orlowsky 2025-11-19 18:39:42 -05:00
parent 2d8f131b91
commit 798ae4aa1a
No known key found for this signature in database
GPG key ID: A9F3BA4C0AA7A70B

View file

@ -112,8 +112,16 @@ impl GtfsPullService {
} }
pub fn update_gtfs_data(state: Arc<Mutex<GtfsPullServiceState>>) -> anyhow::Result<()> { pub fn update_gtfs_data(state: Arc<Mutex<GtfsPullServiceState>>) -> anyhow::Result<()> {
let mut l_state = state.lock().unwrap(); let (files, tmp_dir) = {
let files = l_state.gtfs_files.clone(); 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() { for gtfs_file in files.iter() {
let gtfs = if let Some(subzip) = gtfs_file.source.subzip.clone() { 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 res = reqwest::blocking::get(gtfs_file.source.uri.clone())?;
let outer_archive = res.bytes()?; let outer_archive = res.bytes()?;
let mut archive = ZipArchive::new(Cursor::new(outer_archive))?; 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()); file_path.push(subzip.clone());
gtfs_structures::Gtfs::new(file_path.to_str().unwrap())? gtfs_structures::Gtfs::new(file_path.to_str().unwrap())?
@ -136,7 +144,7 @@ impl GtfsPullService {
for agency in &gtfs.agencies { for agency in &gtfs.agencies {
if let Some(a_id) = &agency.id { 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(), id: a_id.clone(),
name: agency.name.clone() name: agency.name.clone()
}); });
@ -149,7 +157,7 @@ impl GtfsPullService {
for route in &gtfs.routes { for route in &gtfs.routes {
let agency = route.1.agency_id.as_ref() 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()); .map(|agency| agency.clone());
let global_rt_id = match &agency { 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, name: rt_name,
short_name: match route.1.short_name.clone() { short_name: match route.1.short_name.clone() {
Some(x) => x, Some(x) => x,
@ -220,19 +228,23 @@ impl GtfsPullService {
service_id: trip.1.service_id.clone() 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); trip_arr.push(trip);
} else { } 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()); info!("Added {} routes", gtfs.routes.len());
} }
l_state.ready = true; let mut l_state = state.lock().unwrap();
info!("Finished initial sync, ready state is true");
l_state.transit_data = td;
if !l_state.ready {
l_state.ready = true;
info!("Finished initial sync, ready state is true");
}
Ok(()) Ok(())
} }