Function initialize_database

Source
pub async fn initialize_database(pool: &Pool) -> Result<(), TaskError>
Expand description

Initializes the task queue database schema.

Examples found in repository?
examples/consumer.rs (line 38)
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}
More examples
Hide additional examples
examples/producer.rs (line 22)
7async fn main() {
8    let args: Vec<String> = env::args().collect();
9    if args.len() != 2 {
10        eprintln!("Usage: producer <num_tasks>");
11        std::process::exit(1);
12    }
13
14    let num_tasks: i32 = args[1].parse().expect("Invalid number of tasks");
15
16    let database_url = "postgresql://postgres:postgres@localhost/queue";
17
18    let pool = connect(database_url)
19        .await
20        .expect("Failed to connect to the database");
21
22    initialize_database(&pool)
23        .await
24        .expect("Failed to initialize database");
25
26    let pool_arc = std::sync::Arc::new(pool);
27
28    // Enqueue tasks
29    let enqueue_tasks: Vec<_> = (0..num_tasks)
30        .map(|_| {
31            let pool = std::sync::Arc::clone(&pool_arc);
32            let task_data = json!({
33                "recipient": "user@example.com",
34                "subject": "Hello",
35                "body": "This is a test email.",
36            });
37
38            tokio::spawn(async move {
39                let task_id = postgres_queue::enqueue(
40                    &pool.get().await.unwrap(),
41                    "send_email",
42                    task_data.clone(),
43                    Utc::now(), // Run the task immediately
44                    None,       // No interval
45                )
46                .await
47                .expect("Failed to enqueue task");
48                println!("Enqueued task with ID: {}", task_id);
49            })
50        })
51        .collect();
52
53    // Wait for all tasks to be enqueued
54    for task in enqueue_tasks {
55        task.await.expect("Failed to enqueue task");
56    }
57
58    println!("All tasks enqueued.");
59}