TaskManager

Struct TaskManager 

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

Task manager is an asynchronous task supervisor that stores all spawned tasks, controls its states and provides an api for task management.

Implementations§

Source§

impl TaskManager

Source

pub fn builder() -> TaskManagerBuilder

Returns a task manager builder.

Examples found in repository?
examples/task_manager.rs (line 24)
23async fn main() {
24    let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26    let mut task_keys = Vec::new();
27    for i in 0..10 {
28        let task_key = task_manager.try_spawn(outer(i)).unwrap();
29        task_keys.push(task_key)
30    }
31
32    tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34    for task_key in task_keys {
35        if task_manager.cancel_task(task_key).is_ok() {
36            println!("task-{} canceled", task_key)
37        } else {
38            println!("task-{} already finished", task_key)
39        }
40    }
41
42    task_manager.join(true).await;
43}
Source

pub fn new( max_tasks: usize, capacity: usize, completion_events_buffer_size: usize, ) -> TaskManager

Creates a new task manager instance.

Source

pub fn size(&self) -> usize

Returns manager task count.

Source

pub fn try_spawn<F>(&mut self, future: F) -> Option<usize>
where F: Future<Output = ()> + Send + 'static,

Spawns a new asynchronous task wrapping it to be supervised by the task manager. Method can return None if task manager is full and task can not be spawned yet otherwise it returns task key that can be used to cancel this task.

Examples found in repository?
examples/task_manager.rs (line 28)
23async fn main() {
24    let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26    let mut task_keys = Vec::new();
27    for i in 0..10 {
28        let task_key = task_manager.try_spawn(outer(i)).unwrap();
29        task_keys.push(task_key)
30    }
31
32    tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34    for task_key in task_keys {
35        if task_manager.cancel_task(task_key).is_ok() {
36            println!("task-{} canceled", task_key)
37        } else {
38            println!("task-{} already finished", task_key)
39        }
40    }
41
42    task_manager.join(true).await;
43}
Source

pub async fn process(&mut self, resume_panic: bool)

Runs manager processing loop handling task events. Method is cancellation safe and can be used in tokio::select! macro. If resume_panic argument is true and any of the tasks panic method resumes the panic on the current task. It is useful in test environment when you want your application to be panicked if any of the spawned tasks panic.

Examples found in repository?
examples/task_manager.rs (line 32)
23async fn main() {
24    let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26    let mut task_keys = Vec::new();
27    for i in 0..10 {
28        let task_key = task_manager.try_spawn(outer(i)).unwrap();
29        task_keys.push(task_key)
30    }
31
32    tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34    for task_key in task_keys {
35        if task_manager.cancel_task(task_key).is_ok() {
36            println!("task-{} canceled", task_key)
37        } else {
38            println!("task-{} already finished", task_key)
39        }
40    }
41
42    task_manager.join(true).await;
43}
Source

pub fn detach( &mut self, task_key: usize, ) -> Result<TaskHandle<()>, TaskManagerError>

Detaches a task from the manager. The task is not longer supervised by the manager.

Source

pub fn cancel(self)

Cancels all supervised tasks.

Source

pub fn abort(self)

Aborts all supervised tasks, consuming self.

Source

pub async fn join(self, resume_panic: bool)

Waits until all the tasks are completed consuming self. If resume_panic argument is true and any of the tasks panic method resumes the panic on the current task. It is useful in test environment when you want your application to be panicked if any of the spawned tasks panic.

Examples found in repository?
examples/task_manager.rs (line 42)
23async fn main() {
24    let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26    let mut task_keys = Vec::new();
27    for i in 0..10 {
28        let task_key = task_manager.try_spawn(outer(i)).unwrap();
29        task_keys.push(task_key)
30    }
31
32    tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34    for task_key in task_keys {
35        if task_manager.cancel_task(task_key).is_ok() {
36            println!("task-{} canceled", task_key)
37        } else {
38            println!("task-{} already finished", task_key)
39        }
40    }
41
42    task_manager.join(true).await;
43}
Source

pub fn cancel_task(&mut self, task_key: usize) -> Result<(), TaskManagerError>

Cancels a particular task by task key returned by TaskManager::try_spawn method. If task not found (task key is wrong or task already finished) method returns TaskManagerError::TaskNotFound error.

Examples found in repository?
examples/task_manager.rs (line 35)
23async fn main() {
24    let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26    let mut task_keys = Vec::new();
27    for i in 0..10 {
28        let task_key = task_manager.try_spawn(outer(i)).unwrap();
29        task_keys.push(task_key)
30    }
31
32    tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34    for task_key in task_keys {
35        if task_manager.cancel_task(task_key).is_ok() {
36            println!("task-{} canceled", task_key)
37        } else {
38            println!("task-{} already finished", task_key)
39        }
40    }
41
42    task_manager.join(true).await;
43}
Source

pub fn abort_task(&mut self, task_key: usize) -> Result<(), TaskManagerError>

Aborts a task by a task key. The task is removed from the storage so that it can’t be accessed anymore.

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.