Skip to content

Commit 24ca9ef

Browse files
committed
moss: Fix percentage calculation, improved callbacks
1 parent 9e0b7a3 commit 24ca9ef

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

moss/src/client/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,14 @@ impl Client {
443443
);
444444

445445
postblit::execute_triggers(scope, &triggers, |progress| {
446-
progress_bar.inc(progress.completed);
446+
progress_bar.inc(1);
447447
info!(
448-
progress = (total_items + progress.completed) as f32 / total_items as f32,
449-
current = total_items + progress.in_progress as u64,
448+
progress = progress.completed as f32 / total_items as f32,
449+
current = progress.completed,
450450
total = total_items,
451451
event_type = "progress_update",
452452
"Executing {:?}",
453-
progress.items
453+
progress.item
454454
);
455455
})?;
456456

moss/src/client/postblit.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use std::{
1212
path::{Path, PathBuf},
1313
process,
14+
sync::atomic::{AtomicUsize, Ordering},
1415
};
1516

1617
use crate::Installation;
@@ -107,10 +108,9 @@ pub(super) struct TriggerRunner {
107108

108109
/// Progress callback handler
109110
#[derive(Debug, Clone)]
110-
pub struct Progress {
111-
pub in_progress: usize,
111+
pub struct Progress<'a> {
112112
pub completed: u64,
113-
pub items: Vec<String>,
113+
pub item: &'a str,
114114
}
115115

116116
/// Load all triggers matching the given scope and staging filesystem, return in batches
@@ -167,7 +167,7 @@ pub(super) fn triggers<'a>(
167167
pub fn execute_triggers(
168168
scope: TriggerScope<'_>,
169169
triggers: &[Vec<TriggerRunner>],
170-
on_progress: impl Fn(Progress) + Send + Sync,
170+
on_progress: impl Fn(Progress<'_>) + Send + Sync,
171171
) -> Result<(), Error> {
172172
match scope {
173173
TriggerScope::Transaction(install, scope) => {
@@ -193,7 +193,7 @@ fn execute_transaction_triggers<P>(
193193
on_progress: P,
194194
) -> Result<(), Error>
195195
where
196-
P: Fn(Progress) + Send + Sync,
196+
P: Fn(Progress<'_>) + Send + Sync,
197197
{
198198
let trigger_scope = TriggerScope::Transaction(install, scope);
199199
// TODO: Add caching support via /var/
@@ -221,7 +221,7 @@ fn execute_system_triggers<P>(
221221
on_progress: P,
222222
) -> Result<(), Error>
223223
where
224-
P: Fn(Progress) + Send + Sync,
224+
P: Fn(Progress<'_>) + Send + Sync,
225225
{
226226
let trigger_scope = TriggerScope::System(install, scope);
227227

@@ -253,23 +253,22 @@ impl TriggerRunner {
253253
/// Internal executor for triggers.
254254
fn execute_triggers_directly<P>(triggers: &[Vec<TriggerRunner>], on_progress: P) -> Result<(), Error>
255255
where
256-
P: Fn(Progress) + Send + Sync,
256+
P: Fn(Progress<'_>) + Send + Sync,
257257
{
258258
let rayon_runtime = rayon::ThreadPoolBuilder::new().build().expect("rayon runtime");
259-
let total = 0;
259+
260+
let counter = AtomicUsize::new(0);
261+
260262
rayon_runtime.install(|| {
261263
triggers.iter().try_for_each(|batch| {
262264
batch.par_iter().try_for_each(|trigger| {
265+
let completed = counter.fetch_add(1, Ordering::Relaxed);
263266
(on_progress)(Progress {
264-
in_progress: batch.len(),
265-
completed: total + 1,
266-
items: batch
267-
.iter()
268-
.map(|t| match t.handler() {
269-
Handler::Run { run, .. } => run.clone(),
270-
Handler::Delete { .. } => "delete operation".to_owned(),
271-
})
272-
.collect(),
267+
completed: completed as u64,
268+
item: match trigger.handler() {
269+
Handler::Run { run, .. } => run,
270+
Handler::Delete { .. } => "delete operation",
271+
},
273272
});
274273
execute_trigger_directly(trigger.trigger())
275274
})

0 commit comments

Comments
 (0)