better cleanup of storage state

This commit is contained in:
xenofem 2022-04-28 06:26:44 -04:00
parent fa1917ac17
commit 32738e4515
2 changed files with 55 additions and 36 deletions

View file

@ -1,6 +1,6 @@
use std::{collections::HashSet, io::Write, task::Waker};
use actix::{Actor, ActorContext, AsyncContext, Handler, Message, StreamHandler};
use actix::{fut::future::ActorFutureExt, Actor, ActorContext, AsyncContext, Handler, Message, StreamHandler};
use actix_http::ws::{CloseReason, Item};
use actix_web_actors::ws::{self, CloseCode};
use bytes::Bytes;
@ -9,7 +9,7 @@ use rand::distributions::{Alphanumeric, DistString};
use serde::Deserialize;
use time::OffsetDateTime;
use crate::{file::LiveWriter, DownloadableFile, UploadedFile};
use crate::{file::LiveWriter, DownloadableFile, UploadedFile, storage_dir};
const MAX_FILES: usize = 256;
const FILENAME_DATE_FORMAT: &[time::format_description::FormatItem] =
@ -115,7 +115,6 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
Err(e) => {
error!("Websocket error: {}", e);
self.cleanup_after_error(ctx);
ctx.stop();
return;
}
};
@ -128,7 +127,6 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
description: Some(e.to_string()),
}));
self.cleanup_after_error(ctx);
ctx.stop();
}
Ok(true) => {
info!("Finished uploading data");
@ -140,9 +138,9 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Uploader {
let data = self.app_data.clone();
let filename = self.storage_filename.clone();
ctx.wait(actix::fut::wrap_future(async move {
debug!("Spawned future to remove uploader from entry {} before stopping", filename);
data.write().await.remove_uploader(&filename);
}));
ctx.stop();
}).map(|_, _, ctx: &mut Self::Context| ctx.stop()));
}
_ => (),
}
@ -191,7 +189,7 @@ impl Uploader {
}
let storage_filename = Alphanumeric.sample_string(&mut rand::thread_rng(), 8);
self.storage_filename = storage_filename.clone();
let storage_path = super::storage_dir().join(storage_filename.clone());
let storage_path = storage_dir().join(storage_filename.clone());
info!("storing to: {:?}", storage_path);
let writer = super::file::LiveFileWriter::new(&storage_path)?;
let addr = Some(ctx.address());
@ -225,6 +223,7 @@ impl Uploader {
self.writer = Some(writer);
let data = self.app_data.clone();
ctx.spawn(actix::fut::wrap_future(async move {
debug!("Spawned future to add entry {} to state", storage_filename);
data.write()
.await
.add_file(storage_filename, downloadable_file)
@ -275,10 +274,15 @@ impl Uploader {
}
fn cleanup_after_error(&mut self, ctx: &mut <Self as Actor>::Context) {
info!("Cleaning up after failed upload of {}", self.storage_filename);
let data = self.app_data.clone();
let filename = self.storage_filename.clone();
ctx.spawn(actix::fut::wrap_future(async move {
ctx.wait(actix::fut::wrap_future(async move {
debug!("Spawned future to remove entry {} from state", filename);
data.write().await.remove_file(&filename).await.unwrap();
}));
}).map(|_, _, ctx: &mut <Self as Actor>::Context| ctx.stop()));
if let Err(e) = std::fs::remove_file(storage_dir().join(&self.storage_filename)) {
error!("Failed to remove file {}: {}", self.storage_filename, e);
}
}
}