TaskRegistry

Struct TaskRegistry 

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

A struct for managing a registry of task handlers.

Implementations§

Source§

impl TaskRegistry

Source

pub fn new() -> Self

Creates a new TaskRegistry.

Examples found in repository?
examples/consumer.rs (line 42)
24async fn main() {
25    let args: Vec<String> = env::args().collect();
26    if args.len() != 2 {
27        eprintln!("Usage: consumer <num_workers>");
28        std::process::exit(1);
29    }
30
31    let num_workers = args[1].parse().expect("Invalid number of tasks");
32    let database_url = "postgresql://postgres:postgres@localhost/queue";
33
34    let pool = postgres_queue::connect(database_url)
35        .await
36        .expect("Failed to connect to the database");
37
38    initialize_database(&pool)
39        .await
40        .expect("Failed to initialize database");
41
42    let mut registry = TaskRegistry::new();
43    registry.register_task("send_email".to_string(), send_email_handler);
44
45    let pool_arc = std::sync::Arc::new(pool);
46
47    // Run the task processor
48    let tasks = registry
49        .run(&pool_arc, num_workers)
50        .await
51        .expect("Failed to run tasks");
52
53    println!("Running {} tasks", tasks.len());
54
55    // Wait for all tasks to complete
56    for task in tasks {
57        task.await.expect("Task failed");
58    }
59
60    println!("All tasks completed.");
61}
Source

pub fn register_task<F, Fut>(&mut self, name: String, handler: F)
where F: Fn(i32, TaskData) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<(), TaskError>> + Send + Sync + 'static,

Registers a task handler with the provided name.

Examples found in repository?
examples/consumer.rs (line 43)
24async fn main() {
25    let args: Vec<String> = env::args().collect();
26    if args.len() != 2 {
27        eprintln!("Usage: consumer <num_workers>");
28        std::process::exit(1);
29    }
30
31    let num_workers = args[1].parse().expect("Invalid number of tasks");
32    let database_url = "postgresql://postgres:postgres@localhost/queue";
33
34    let pool = postgres_queue::connect(database_url)
35        .await
36        .expect("Failed to connect to the database");
37
38    initialize_database(&pool)
39        .await
40        .expect("Failed to initialize database");
41
42    let mut registry = TaskRegistry::new();
43    registry.register_task("send_email".to_string(), send_email_handler);
44
45    let pool_arc = std::sync::Arc::new(pool);
46
47    // Run the task processor
48    let tasks = registry
49        .run(&pool_arc, num_workers)
50        .await
51        .expect("Failed to run tasks");
52
53    println!("Running {} tasks", tasks.len());
54
55    // Wait for all tasks to complete
56    for task in tasks {
57        task.await.expect("Task failed");
58    }
59
60    println!("All tasks completed.");
61}
Source

pub fn handlers(&self) -> &Arc<HashMap<String, TaskHandler>>

Returns a reference to the task handlers.

Source

pub async fn run( &self, pool: &Pool, num_workers: usize, ) -> Result<Vec<JoinHandle<()>>, TaskError>

Runs the task handlers with the provided number of workers.

Examples found in repository?
examples/consumer.rs (line 49)
24async fn main() {
25    let args: Vec<String> = env::args().collect();
26    if args.len() != 2 {
27        eprintln!("Usage: consumer <num_workers>");
28        std::process::exit(1);
29    }
30
31    let num_workers = args[1].parse().expect("Invalid number of tasks");
32    let database_url = "postgresql://postgres:postgres@localhost/queue";
33
34    let pool = postgres_queue::connect(database_url)
35        .await
36        .expect("Failed to connect to the database");
37
38    initialize_database(&pool)
39        .await
40        .expect("Failed to initialize database");
41
42    let mut registry = TaskRegistry::new();
43    registry.register_task("send_email".to_string(), send_email_handler);
44
45    let pool_arc = std::sync::Arc::new(pool);
46
47    // Run the task processor
48    let tasks = registry
49        .run(&pool_arc, num_workers)
50        .await
51        .expect("Failed to run tasks");
52
53    println!("Running {} tasks", tasks.len());
54
55    // Wait for all tasks to complete
56    for task in tasks {
57        task.await.expect("Task failed");
58    }
59
60    println!("All tasks completed.");
61}

Trait Implementations§

Source§

impl Default for TaskRegistry

Source§

fn default() -> Self

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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,