use super::engine::{
LaneShared, increment_completed, panic_message, process_remote_entry, push_progress,
update_remote,
};
use crate::{
MainWindow,
gui::batch::remote_dispatch::{DispatcherGroup, RemoteStatus},
settings::WorkerSettings,
};
use slint::Weak;
use std::{
panic::{self, AssertUnwindSafe},
sync::atomic::Ordering,
};
use tracing::warn;
fn dispatch_design(
shared: &LaneShared<'_>,
ui_weak: &Weak<MainWindow>,
worker: Option<&WorkerSettings>,
entry_id: i64,
) -> bool {
let mut on_remote = false;
let result = panic::catch_unwind(AssertUnwindSafe(|| {
process_remote_entry(shared.ctx, worker, entry_id, shared.cancel, |title| {
on_remote = true;
update_remote(shared.progress, |remote| remote.item_started(title));
push_progress(
ui_weak,
shared.design_total,
shared.local_lane_total,
shared.progress,
);
})
}));
if on_remote {
update_remote(shared.progress, RemoteStatus::item_ended);
}
match result {
Ok(saved) => saved,
Err(payload) => {
warn!(
"Remote tilt-curve dispatch panicked for entry {entry_id}: {}",
panic_message(&*payload)
);
false
}
}
}
fn settle_design(shared: &LaneShared<'_>, entry_id: i64, saved: bool, fallback_to_local: bool) {
if saved {
shared.tally.computed.fetch_add(1, Ordering::Relaxed);
increment_completed(shared.progress);
} else if fallback_to_local {
shared.queue.return_to_local(entry_id);
} else {
shared.tally.failed.fetch_add(1, Ordering::Relaxed);
increment_completed(shared.progress);
}
}
pub(super) fn run_remote_lane(
shared: &LaneShared<'_>,
ui_weak: &Weak<MainWindow>,
worker: Option<&WorkerSettings>,
fallback_to_local: bool,
dispatchers: &DispatcherGroup<'_>,
) {
let _counted_out_on_drop = dispatchers.guard();
update_remote(shared.progress, RemoteStatus::dispatcher_started);
push_progress(
ui_weak,
shared.design_total,
shared.local_lane_total,
shared.progress,
);
loop {
if shared.cancel.load(Ordering::Relaxed) {
break;
}
let Some(entry_id) = shared.queue.claim_shared() else {
break;
};
let saved = dispatch_design(shared, ui_weak, worker, entry_id);
settle_design(shared, entry_id, saved, fallback_to_local);
push_progress(
ui_weak,
shared.design_total,
shared.local_lane_total,
shared.progress,
);
}
update_remote(shared.progress, RemoteStatus::dispatcher_ended);
push_progress(
ui_weak,
shared.design_total,
shared.local_lane_total,
shared.progress,
);
}