pub trait TaskWithCleanup:
Sized
+ Send
+ 'static {
// Required methods
fn main_task(
&mut self,
) -> impl Future<Output = Result<(), impl Error + 'static>> + Send;
fn cleanup(
self,
) -> impl Future<Output = Result<(), impl Error + 'static>> + Send;
}Expand description
Model for a pair of asynchronous tasks: one to run first and until there
is an external indication to stop and another one to run instead after
that. For use with AssemblyRuntime::set_task_with_cleanup.
Usage:
use comprehensive::v1::{AssemblyRuntime, Resource, TaskWithCleanup, resource};
use std::sync::Arc;
struct NeedsToTakeActionOnShutdownTask {
private_state: i32,
}
impl TaskWithCleanup for NeedsToTakeActionOnShutdownTask {
#[allow(refining_impl_trait)]
async fn main_task(&mut self) -> Result<(), std::convert::Infallible> {
loop {
// THIS CODE MUST BE CANCEL-SAFE
// [...]
}
}
#[allow(refining_impl_trait)]
async fn cleanup(self) -> Result<(), std::convert::Infallible> {
// [...]
Ok(())
}
}
struct NeedsToTakeActionOnShutdown;
#[resource]
impl Resource for NeedsToTakeActionOnShutdown {
fn new(
_: comprehensive::NoDependencies,
a: comprehensive::NoArgs,
api: &mut AssemblyRuntime<'_>,
) -> Result<Arc<Self>, std::convert::Infallible> {
api.set_task_with_cleanup(NeedsToTakeActionOnShutdownTask {
private_state: 42,
});
Ok(Arc::new(Self))
}
}Required Methods§
Sourcefn main_task(
&mut self,
) -> impl Future<Output = Result<(), impl Error + 'static>> + Send
fn main_task( &mut self, ) -> impl Future<Output = Result<(), impl Error + 'static>> + Send
Main task from a main+cleanup pair. This task runs when the assembly first starts running and continues either until it returns or until a signal to shut down the assembly is received and all of the resources that depend on this one have already shut down.
This task must be cancel-safe because it will be dropped immediately when it is time to run the cleanup task instead!
If this returns an error then the entire Assembly will exit.
Sourcefn cleanup(
self,
) -> impl Future<Output = Result<(), impl Error + 'static>> + Send
fn cleanup( self, ) -> impl Future<Output = Result<(), impl Error + 'static>> + Send
Cleanup task from a main+cleanup pair. This task runs after the assembly has received a shutdown signal and the main task has been dropped. It should do work necessary to sanitise state before quitting but should not count on being allowed to run for too long.
If the main task returns successfully before any shutdown signal is received then we assume there is no cleanup required, and this will not run at all.
If this returns an error then the entire Assembly will exit.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.