pub struct BackgroundTask { /* private fields */ }
Expand description
Implementations§
Source§impl BackgroundTask
impl BackgroundTask
Sourcepub fn request_start(&self) -> Result<(), Error>
pub fn request_start(&self) -> Result<(), Error>
Mark the task as ready to run
Sourcepub fn request_stop_and_notify(&self) -> Result<(), Error>
pub fn request_stop_and_notify(&self) -> Result<(), Error>
Request the task to stop
Sourcepub fn should_keep_running(&self, timeout: Duration) -> Result<bool, Error>
pub fn should_keep_running(&self, timeout: Duration) -> Result<bool, Error>
Wait for a stop request for up to timeout
Usable in a loop like:
ⓘ
while task.should_keep_running(timeout)? {
do_things_on_every_timeout()?;
}
Sourcepub fn spawn<F>(
self: &Arc<Self>,
interval: Duration,
func: F,
) -> Result<JoinHandle<Result<(), Error>>, Error>
pub fn spawn<F>( self: &Arc<Self>, interval: Duration, func: F, ) -> Result<JoinHandle<Result<(), Error>>, Error>
Spawn a background thread that calls func
every interval
until shutdown
Ideally, the called closure should not block for any noticeable time, as shutdown requests are not processed while it’s running.
This method does not attempt to compensate for the closure running time and does not
try to guarantee that it’s executed exactly every interval
. If you need precise
intervals between each execution, you should start the thread yourself and calculate
the timeout passed to BackgroundTask::should_keep_running
every time. You will also
need to handle the case when the closure takes longer than the interval:
- just skip the next execution?
- try to catch up by running the closure back-to-back without a delay?
- return an error (and stop the background thread)?
Trait Implementations§
Source§impl Debug for BackgroundTask
impl Debug for BackgroundTask
Source§impl Default for BackgroundTask
impl Default for BackgroundTask
Source§fn default() -> BackgroundTask
fn default() -> BackgroundTask
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !Freeze for BackgroundTask
impl RefUnwindSafe for BackgroundTask
impl Send for BackgroundTask
impl Sync for BackgroundTask
impl Unpin for BackgroundTask
impl UnwindSafe for BackgroundTask
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more