Maintain an index file of rakepacks and append when a rakepack is finished

This commit is contained in:
Olivier 'reivilibre' 2022-11-26 20:07:12 +00:00
parent 52d0183942
commit bd16f58d9e
1 changed files with 22 additions and 8 deletions

View File

@ -57,12 +57,15 @@ pub fn pack_emitter<T: Serialize + Send + 'static>(
Unit::Count, Unit::Count,
"Records emitted into a pack file" "Records emitted into a pack file"
); );
let pack_index_file = directory.join("index");
loop { loop {
let now = Utc::now(); let now = Utc::now();
// 2022-01-01 01:01:01 // 2022-01-01_01:01:01
let new_pack_file_path = loop { let (pack_name, new_pack_file_path) = loop {
let new_pack_file_path = let pack_name = format!("{}.{}.pack", now.format("%F_%T"), name);
directory.join(format!("{}.{}.pack", now.format("%F_%T"), name)); let new_pack_file_path = directory.join(&pack_name);
if new_pack_file_path.exists() { if new_pack_file_path.exists() {
warn!( warn!(
"{:?} already exists; sleeping to generate new timestamp.", "{:?} already exists; sleeping to generate new timestamp.",
@ -70,11 +73,11 @@ pub fn pack_emitter<T: Serialize + Send + 'static>(
); );
std::thread::sleep(Duration::from_secs(2)); std::thread::sleep(Duration::from_secs(2));
} else { } else {
break new_pack_file_path; break (pack_name, new_pack_file_path);
} }
}; };
if !pack_emitter_to_file( let file_cutoff_reached = pack_emitter_to_file(
&new_pack_file_path, &new_pack_file_path,
&mut rx, &mut rx,
name, name,
@ -82,8 +85,19 @@ pub fn pack_emitter<T: Serialize + Send + 'static>(
settings, settings,
shutdown.clone(), shutdown.clone(),
shutdown_notify.clone(), shutdown_notify.clone(),
)? { )?;
// File wasn't filled; the receiver was exhausted (we're shutting down).
// Add an entry to the index. This essentially marks it as 'done' and enables
// a follower to catch up.
let mut index_file = OpenOptions::new()
.create(true)
.append(true)
.open(&pack_index_file)?;
index_file.write(format!("\n{}", pack_name).as_bytes())?;
index_file.flush()?;
if !file_cutoff_reached {
// File wasn't filled; the receiver was exhausted (that means we're shutting down).
break; break;
} }
} }