Remove Context::free_ongoing function

This is now handled better by the Drop from the OngoingGuard returned
by Context::alloc_ongoing.
This commit is contained in:
Floris Bruynooghe
2023-03-30 10:52:47 +02:00
parent 0c5d1832ae
commit 201d05d4fa
4 changed files with 14 additions and 35 deletions

View File

@@ -66,18 +66,16 @@ impl Context {
self.sql.is_open().await, self.sql.is_open().await,
"cannot configure, database not opened." "cannot configure, database not opened."
); );
let cancel_channel = self.alloc_ongoing().await?; let ongoing_guard = self.alloc_ongoing().await?;
let res = self let res = self
.inner_configure() .inner_configure()
.race(cancel_channel.map(|_| { .race(ongoing_guard.map(|_| {
progress!(self, 0); progress!(self, 0);
Ok(()) Ok(())
})) }))
.await; .await;
self.free_ongoing().await;
if let Err(err) = res.as_ref() { if let Err(err) = res.as_ref() {
progress!( progress!(
self, self,

View File

@@ -545,11 +545,6 @@ impl Context {
}) })
} }
pub(crate) async fn free_ongoing(&self) {
let mut s = self.running_state.write().await;
*s = RunningState::Stopped;
}
/// Signal an ongoing process to stop. /// Signal an ongoing process to stop.
pub async fn stop_ongoing(&self) { pub async fn stop_ongoing(&self) {
let mut s = self.running_state.write().await; let mut s = self.running_state.write().await;

View File

@@ -88,18 +88,17 @@ pub async fn imex(
path: &Path, path: &Path,
passphrase: Option<String>, passphrase: Option<String>,
) -> Result<()> { ) -> Result<()> {
let cancel = context.alloc_ongoing().await?; let ongoing_guard = context.alloc_ongoing().await?;
let res = { let res = {
let _guard = context.scheduler.pause(context.clone()).await; let _guard = context.scheduler.pause(context.clone()).await;
imex_inner(context, what, path, passphrase) imex_inner(context, what, path, passphrase)
.race(async { .race(async {
cancel.await; ongoing_guard.await;
Err(format_err!("canceled")) Err(format_err!("canceled"))
}) })
.await .await
}; };
context.free_ongoing().await;
if let Err(err) = res.as_ref() { if let Err(err) = res.as_ref() {
// We are using Anyhow's .context() and to show the inner error, too, we need the {:#}: // We are using Anyhow's .context() and to show the inner error, too, we need the {:#}:

View File

@@ -90,7 +90,7 @@ impl BackupProvider {
.context("Private key not available, aborting backup export")?; .context("Private key not available, aborting backup export")?;
// Acquire global "ongoing" mutex. // Acquire global "ongoing" mutex.
let mut cancel_token = context.alloc_ongoing().await?; let mut ongoing_guard = context.alloc_ongoing().await?;
let paused_guard = context.scheduler.pause(context.clone()).await; let paused_guard = context.scheduler.pause(context.clone()).await;
let context_dir = context let context_dir = context
.get_blobdir() .get_blobdir()
@@ -102,7 +102,7 @@ impl BackupProvider {
warn!(context, "Previous database export deleted"); warn!(context, "Previous database export deleted");
} }
let dbfile = TempPathGuard::new(dbfile); let dbfile = TempPathGuard::new(dbfile);
let res = tokio::select! { let (provider, ticket) = tokio::select! {
biased; biased;
res = Self::prepare_inner(context, &dbfile) => { res = Self::prepare_inner(context, &dbfile) => {
match res { match res {
@@ -113,20 +113,12 @@ impl BackupProvider {
}, },
} }
}, },
_ = &mut cancel_token => Err(format_err!("cancelled")), _ = &mut ongoing_guard => Err(format_err!("cancelled")),
}; }?;
let (provider, ticket) = match res {
Ok((provider, ticket)) => (provider, ticket),
Err(err) => {
context.free_ongoing().await;
return Err(err);
}
};
let handle = { let handle = {
let context = context.clone(); let context = context.clone();
tokio::spawn(async move { tokio::spawn(async move {
let res = Self::watch_provider(&context, provider, cancel_token).await; let res = Self::watch_provider(&context, provider, ongoing_guard).await;
context.free_ongoing().await;
// Explicit drop to move the guards into this future // Explicit drop to move the guards into this future
drop(paused_guard); drop(paused_guard);
@@ -189,7 +181,6 @@ impl BackupProvider {
mut provider: Provider, mut provider: Provider,
mut cancel_token: OngoingGuard, mut cancel_token: OngoingGuard,
) -> Result<()> { ) -> Result<()> {
// _dbfile exists so we can clean up the file once it is no longer needed
let mut events = provider.subscribe(); let mut events = provider.subscribe();
let mut total_size = 0; let mut total_size = 0;
let mut current_size = 0; let mut current_size = 0;
@@ -373,16 +364,12 @@ pub async fn get_backup(context: &Context, qr: Qr) -> Result<()> {
let _guard = context.scheduler.pause(context.clone()).await; let _guard = context.scheduler.pause(context.clone()).await;
// Acquire global "ongoing" mutex. // Acquire global "ongoing" mutex.
let cancel_token = context.alloc_ongoing().await?; let mut cancel_token = context.alloc_ongoing().await?;
let res = tokio::select! { tokio::select! {
biased; biased;
res = get_backup_inner(context, qr) => { res = get_backup_inner(context, qr) => res,
context.free_ongoing().await; _ = &mut cancel_token => Err(format_err!("cancelled")),
res
} }
_ = cancel_token => Err(format_err!("cancelled")),
};
res
} }
async fn get_backup_inner(context: &Context, qr: Qr) -> Result<()> { async fn get_backup_inner(context: &Context, qr: Qr) -> Result<()> {