Fix site downloading
All checks were successful
Build / build (push) Successful in 1m56s

This commit is contained in:
Moritz Ruth 2025-03-01 00:46:26 +01:00
parent abbaaf8381
commit cd0c4487c6
Signed by: moritzruth
GPG key ID: C9BBAB79405EE56D

View file

@ -3,6 +3,7 @@ use async_std::io::{BufReader, BufWriter, WriteExt};
use async_std::net::TcpStream;
use async_std::stream::StreamExt;
use async_std::sync::RwLock;
use async_std::task::JoinHandle;
use async_std::{fs, task};
use camino::{Utf8Path, Utf8PathBuf};
use color_eyre::Result;
@ -12,7 +13,6 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use async_std::task::JoinHandle;
#[derive(Serialize, Deserialize)]
pub struct SiteState {
@ -152,9 +152,12 @@ async fn handle_download(ureq_agent: Arc<ureq::Agent>, sites: &Sites, caddy_cont
// Download
let archive_file_path = site.path.join(format!("{}.zip", current_version.id));
let mut response = task::spawn_blocking({
let response = task::spawn_blocking({
let url = current_version.download_url.clone();
move || ureq_agent.get(url).call() }).await?;
move || ureq_agent.get(url).call()
})
.await?;
let status = response.status();
if !status.is_success() {
return Err(eyre!("Download request failed with status code {status}"));
@ -164,18 +167,21 @@ async fn handle_download(ureq_agent: Arc<ureq::Agent>, sites: &Sites, caddy_cont
let archive_file_path = archive_file_path.to_owned().into_std_path_buf();
move || -> std::io::Result<std::fs::File> {
let mut writer = std::io::BufWriter::new(std::fs::OpenOptions::new()
let mut writer = std::io::BufWriter::new(
std::fs::OpenOptions::new()
.truncate(true)
.create(true)
.write(true)
.read(true)
.open(archive_file_path)?);
.open(archive_file_path)?,
);
std::io::copy(&mut response.body_mut().as_reader(), &mut writer)?;
std::io::copy(&mut response.into_body().into_reader(), &mut writer)?;
Ok(writer.into_inner()?)
}
}).await?;
})
.await?;
let extraction_directory_path = site.path.join(&current_version.id);
let _ = fs::remove_dir_all(extraction_directory_path.as_std_path()).await;
@ -207,7 +213,7 @@ async fn handle_download(ureq_agent: Arc<ureq::Agent>, sites: &Sites, caddy_cont
// Cleanup
fs::remove_file(archive_file_path.as_std_path()).await?;
if let Some(old_active_version) = old_active_version {
fs::remove_dir_all(site.path.join(old_active_version.id).into_std_path_buf()).await?;
let _ = fs::remove_dir_all(site.path.join(old_active_version.id).into_std_path_buf()).await;
}
info!("Cleanup finished for {domain}");
@ -215,13 +221,18 @@ async fn handle_download(ureq_agent: Arc<ureq::Agent>, sites: &Sites, caddy_cont
Ok(())
}
async fn sites_worker(mut download_tasks_receiver: async_std::channel::Receiver<String>, sites: Arc<Sites>, mut caddy_controller: CaddyController) -> Result<!> {
let mut ureq_agent = Arc::new(
ureq::Agent::new_with_config(ureq::Agent::config_builder()
async fn sites_worker(
mut download_tasks_receiver: async_std::channel::Receiver<String>,
sites: Arc<Sites>,
mut caddy_controller: CaddyController,
) -> Result<!> {
let ureq_agent = Arc::new(ureq::Agent::new_with_config(
ureq::Agent::config_builder()
.timeout_resolve(Some(Duration::from_secs(10)))
.timeout_connect(Some(Duration::from_secs(30)))
.timeout_global(Some(Duration::from_mins(15)))
.build())
);
.build(),
));
loop {
let domain = download_tasks_receiver.recv().await.unwrap();