Skip to content

Commit a2dd9d6

Browse files
committed
fix database saving issue
1 parent 8278e2e commit a2dd9d6

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/container.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ impl ContainerManager {
487487
}
488488

489489
/// Check if a container is using the latest version
490+
#[allow(dead_code)]
490491
pub fn is_container_latest_version(container_id: &str) -> io::Result<bool> {
491492
let container_tag = Self::get_container_tag(container_id)?;
492493
let latest_tag = Self::get_latest_image_tag()?;

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ async fn shutdown_procedure(shared_db: SharedUserDB) {
593593
Ok(()) => info!("Traefik instances have been successfully shut down."),
594594
Err(e) => error!("Error shutting down Traefik instances: {}", e),
595595
}
596+
597+
db.write_to_file_immediately();
596598

597599
drop(db);
598600

src/user.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ impl UserDB {
278278
}
279279
Ok(())
280280
}
281+
pub fn write_to_file_immediately(&mut self) {
282+
// Force write to file on drop
283+
let now = Instant::now();
284+
match OpenOptions::new().write(true).create(true).truncate(true).open(&self.file_path) {
285+
Ok(file) => {
286+
let writer = BufWriter::new(file);
287+
if let Err(e) = serde_json::to_writer_pretty(writer, &self) {
288+
error!("Failed to write user database immediately: {}", e);
289+
} else {
290+
info!("User database written to file immediately: {}", self.file_path);
291+
self.last_write_time = Some(now);
292+
}
293+
},
294+
Err(e) => {
295+
error!("Failed to open file for writing immediately: {}", e);
296+
}
297+
}
298+
}
281299

282300
/// Reads the user database from a file and returns a `UserDB` instance.
283301
pub fn read_from_file(file_path: &str) -> Result<UserDB, Box<dyn Error>> {
@@ -303,7 +321,8 @@ impl UserDB {
303321
pub fn find_user_by_token(&self, token: &str) -> Option<&User> {
304322
self.users.values().find(|user| user.token.as_deref() == Some(token))
305323
}
306-
324+
325+
#[allow(dead_code)]
307326
pub fn find_user_by_token_mut(&mut self, token: &str) -> Option<&mut User> {
308327
self.users.values_mut().find(|user| user.token.as_deref() == Some(token))
309328
}

0 commit comments

Comments
 (0)