pub struct TaskRegistry { /* private fields */ }
Expand description
A struct for managing a registry of task handlers.
Implementations§
Source§impl TaskRegistry
impl TaskRegistry
Sourcepub fn new() -> Self
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}
Sourcepub fn register_task<F, Fut>(&mut self, name: String, handler: F)
pub fn register_task<F, Fut>(&mut self, name: String, handler: F)
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}
Sourcepub fn handlers(&self) -> &Arc<HashMap<String, TaskHandler>>
pub fn handlers(&self) -> &Arc<HashMap<String, TaskHandler>>
Returns a reference to the task handlers.
Sourcepub async fn run(
&self,
pool: &Pool,
num_workers: usize,
) -> Result<Vec<JoinHandle<()>>, TaskError>
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§
Auto Trait Implementations§
impl Freeze for TaskRegistry
impl !RefUnwindSafe for TaskRegistry
impl Send for TaskRegistry
impl Sync for TaskRegistry
impl Unpin for TaskRegistry
impl !UnwindSafe for TaskRegistry
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