use super::*;
pub(super) async fn next_maintenance_deadline(
ctx: &EngineLoopContext,
running_downloads: &[(GroupId, RunningDownload)],
) -> Option<Instant> {
let timeout_deadline = running_downloads
.iter()
.filter_map(|(_, running)| {
running
.timeout
.and_then(|timeout| running.last_activity.checked_add(timeout))
})
.min();
let has_pending_downloads = !ctx.group_man.download_finished() || !running_downloads.is_empty();
let save_deadline = if let Some(auto_save) = &ctx.auto_save {
let save = auto_save.lock().await;
save.next_deadline(has_pending_downloads)
} else {
None
};
[timeout_deadline, save_deadline]
.into_iter()
.flatten()
.chain(ctx.server_stat_next_save)
.min()
}
pub(super) async fn wait_for_deadline(deadline: Option<Instant>) {
match deadline {
Some(deadline) => {
tokio::time::sleep_until(tokio::time::Instant::from_std(deadline)).await;
}
None => std::future::pending::<()>().await,
}
}
pub(super) async fn run_deadline_maintenance(
ctx: &mut EngineLoopContext,
running_downloads: &mut [(GroupId, RunningDownload)],
) {
let now = Instant::now();
let mut timed_out = Vec::new();
for (gid, rd) in running_downloads.iter_mut() {
let last_network_activity = ctx
.group_man
.find_group(*gid)
.map(|group| group.recover().last_network_activity());
if let Some(last_network_activity) = last_network_activity
&& last_network_activity > rd.last_activity
{
rd.last_activity = last_network_activity;
}
if let Some(timeout) = rd.timeout
&& now.duration_since(rd.last_activity) >= timeout
{
rd.timeout = None;
timed_out.push(*gid);
}
}
if !timed_out.is_empty() {
let man = &ctx.group_man;
for gid in timed_out {
if let Some(group) = man.find_group(gid) {
let request_context = group.recover().latest_connection_context();
let uris = group.recover().get_all_uris();
if let Some(uri) = uris.first()
&& let Ok(parsed) = reqwest::Url::parse(uri)
&& let Some(host) = parsed.host_str()
{
let protocol = parsed.scheme().to_ascii_lowercase();
ctx.server_stat_man
.mark_failure_with_protocol(host, &protocol, 408);
if let Some(context) = request_context
&& group.recover().options().async_dns
{
let dns = ctx.dns_cache.lock().await;
dns.mark_bad_context(&context);
if !dns.has_good_address(&context.endpoint) {
dns.remove_cached(context.endpoint.hostname(), context.endpoint.port());
}
}
}
}
if man.timeout_group(gid) {
warn!(
gid = gid.value(),
"Download task timed out, requesting graceful halt"
);
}
}
}
let has_pending_downloads = !ctx.group_man.download_finished() || !running_downloads.is_empty();
if let Some(ref auto_save) = ctx.auto_save {
let mut save = auto_save.lock().await;
save.run_due(has_pending_downloads).await;
}
if let (Some(path), Some(interval), Some(deadline)) = (
ctx.server_stat_save_path.as_ref(),
ctx.server_stat_save_interval,
ctx.server_stat_next_save,
) && now >= deadline
{
ctx.server_stat_next_save = Some(now + interval);
match ctx.server_stat_man.save_to_file_async(path).await {
Ok(count) => debug!(count, path = %path.display(), "Saved server statistics"),
Err(error) => warn!(%error, path = %path.display(), "Failed to save server statistics"),
}
}
}
pub(super) async fn run_event_cleanup(ctx: &EngineLoopContext) {
let pruned = ctx.group_man.prune_stopped_results(MAX_STOPPED_RESULTS);
if pruned > 0 {
debug!("Pruned {} excess stopped results", pruned);
}
let stale_stats = ctx
.server_stat_max_age
.map(|max_age| ctx.server_stat_man.remove_stale(max_age))
.unwrap_or(0);
if stale_stats > 0 {
debug!("Removed {} stale server statistics", stale_stats);
}
}
pub(super) async fn request_shutdown_and_wait(
running: &mut RunningDownload,
wait: Duration,
) -> bool {
if let Some(shutdown) = running.shutdown.take() {
shutdown.cancel();
}
match tokio::time::timeout(wait, &mut running._handle).await {
Ok(Ok(())) => true,
Ok(Err(error)) => {
warn!(%error, "Download task panicked during shutdown");
false
}
Err(_) => {
warn!("Download task shutdown timed out");
running._handle.abort();
match (&mut running._handle).await {
Ok(()) => {}
Err(error) if error.is_cancelled() => {}
Err(error) => warn!(%error, "Download task failed while joining after abort"),
}
false
}
}
}
pub(super) async fn cancel_running_file_allocations(
ctx: &EngineLoopContext,
running_downloads: &[(GroupId, RunningDownload)],
) {
for (gid, _) in running_downloads {
let cancelled =
crate::filesystem::file_allocation_man::cancel_gid(&ctx.file_alloc_man, gid.value())
.await;
if cancelled > 0 {
debug!(
gid = gid.value(),
cancelled, "Cancelled file allocations for engine-owned task"
);
}
}
}
pub(super) async fn on_end_of_run(
ctx: &EngineLoopContext,
running_downloads: &mut Vec<(GroupId, RunningDownload)>,
) {
info!("Engine loop cleanup: removing stopped groups and saving state");
cancel_running_file_allocations(ctx, running_downloads).await;
let demoted = {
let man = &ctx.group_man;
man.remove_stopped_groups(Some(&ctx.event_hooks))
};
if !demoted.is_empty() {
info!("Demoted {} final groups on shutdown", demoted.len());
}
for (gid, mut rd) in running_downloads.drain(..) {
let completed = request_shutdown_and_wait(&mut rd, SHUTDOWN_WAIT).await;
debug!(
gid = gid.value(),
completed, "Finished running task shutdown"
);
}
#[cfg(feature = "bittorrent")]
{
ctx.lpd_manager.stop_background_announce();
ctx.lpd_manager.stop_receive_loop().await;
}
if let Some(ref auto_save) = ctx.auto_save {
let mut save = auto_save.lock().await;
save.force_save().await;
}
if let Some(path) = &ctx.server_stat_save_path {
match ctx.server_stat_man.save_to_file_async(path).await {
Ok(count) => {
debug!(count, path = %path.display(), "Saved server statistics on shutdown")
}
Err(error) => {
warn!(%error, path = %path.display(), "Failed to save server statistics on shutdown")
}
}
}
}