Struct BackgroundTask

Source
pub struct BackgroundTask { /* private fields */ }
Expand description

§A helper to periodically run a background task until shutdown is requested

Can be used to spawn a separate thread or as a building block for some other (synchronous/blocking) abstraction.

The implementation is little more than a Condvar and some helper methods.

Implementations§

Source§

impl BackgroundTask

Source

pub fn request_start(&self) -> Result<(), Error>

Mark the task as ready to run

Source

pub fn request_stop_and_notify(&self) -> Result<(), Error>

Request the task to stop

Source

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()?;
}
Source

pub fn spawn<F>( self: &Arc<Self>, interval: Duration, func: F, ) -> Result<JoinHandle<Result<(), Error>>, Error>
where F: FnMut() -> Result<(), Error> + 'static + Send,

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BackgroundTask

Source§

fn default() -> BackgroundTask

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.