Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: report job count only after it was persisted #593

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions daemon/station_reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ impl StationReporter {

print_event(&event);
}

fn update_jobs_completed(&self, total: u64) {
// IMPORTANT: We must update the persisted state first and report the job counter to Station
// only after the persisted state was successfully updated. Otherwise, when the computer is
// out of disk space, Station will remember the higher job count we reported before
// crashing due to the `unwrap()` call below, but we will report a lower value after Station
// restarts us and we load the old counter from the state file.
let state = State {
total_jobs_completed: total,
};
state
.store(&self.state_file)
// NOTE(bajtos) We are intentionally calling unwrap() to crash the process in case
// we cannot store the state into the file.
.unwrap();

self.print_jobs_completed(total)
}
}

fn print_event(data: &serde_json::Value) {
Expand Down Expand Up @@ -136,19 +154,9 @@ impl Reporter for StationReporter {
}

fn job_completed(&self) {
let total_jobs_completed = self
.tracker
self.tracker
.borrow_mut()
.job_completed(|n| self.print_jobs_completed(n));

let state = State {
total_jobs_completed,
};
state
.store(&self.state_file)
// NOTE(bajtos) We are intentionally calling unwrap() to crash the process in case
// we cannot store the state into the file.
.unwrap();
.job_completed(|n| self.update_jobs_completed(n));
}
}

Expand Down
Loading