Skip to content

Commit

Permalink
sync-ref: re-read config after writing it & before building sql index
Browse files Browse the repository at this point in the history
'osm-gimmisn sync-ref --mode download --url ...' did its download fine,
then failed with:

> failed to open ... for reading

Because it still had the old paths in memory. Add an explicit way to
re-read the config, so 'sync-ref --mode download' can do download + sql
index update in one go, without having to run it twice in a row.

Change-Id: Ia0b3e11336c55dfe4048a875c108524a6405281e
  • Loading branch information
vmiklos committed Oct 30, 2024
1 parent f66ee96 commit fdf10cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use anyhow::Context as _;
use once_cell::unsync::OnceCell;
use std::cell::Ref;
use std::cell::RefCell;
use std::cell::RefMut;
use std::io::Read;
Expand Down Expand Up @@ -163,6 +164,17 @@ impl Ini {
})
}

fn update(
&mut self,
file_system: &Rc<dyn FileSystem>,
config_path: &str,
) -> anyhow::Result<()> {
let data = file_system.read_to_string(config_path)?;
self.config = toml::from_str(&data)?;

Ok(())
}

/// Gets the directory which is writable.
pub fn get_workdir(&self) -> String {
format!("{}/workdir", self.root)
Expand Down Expand Up @@ -229,7 +241,7 @@ impl Ini {
/// Context owns global state which is set up once and then read everywhere.
pub struct Context {
root: String,
ini: Ini,
ini: Rc<RefCell<Ini>>,
network: Rc<dyn Network>,
time: Rc<dyn Time>,
subprocess: Rc<dyn Subprocess>,
Expand All @@ -252,7 +264,11 @@ impl Context {
let unit = Rc::new(StdUnit {});
let file_system: Rc<dyn FileSystem> = Rc::new(StdFileSystem {});
let database: Rc<dyn Database> = Rc::new(StdDatabase {});
let ini = Ini::new(&file_system, &format!("{root}/workdir/wsgi.ini"), &root)?;
let ini = Rc::new(RefCell::new(Ini::new(
&file_system,
&format!("{root}/workdir/wsgi.ini"),
&root,
)?));
let connection = OnceCell::new();
let shutdown = Rc::new(RefCell::new(false));
Ok(Context {
Expand All @@ -275,8 +291,16 @@ impl Context {
}

/// Gets the ini file.
pub fn get_ini(&self) -> &Ini {
&self.ini
pub fn get_ini(&self) -> Ref<'_, Ini> {
self.ini.borrow()
}

/// Re-reads the ini file.
pub fn update_ini(&self) -> anyhow::Result<()> {
self.ini.borrow_mut().update(
&self.file_system,
&format!("{}/workdir/wsgi.ini", self.root),
)
}

/// Gets the network implementation.
Expand Down
1 change: 1 addition & 0 deletions src/sync_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn download(

ctx.get_file_system()
.write_from_string(&config_data, &ctx.get_abspath("workdir/wsgi.ini"))?;
ctx.update_ini()?;
}

stream.write_all("sync-ref: removing old index...\n".as_bytes())?;
Expand Down

0 comments on commit fdf10cc

Please sign in to comment.