Function enqueue

Source
pub async fn enqueue(
    client: &Client,
    name: &str,
    task_data: TaskData,
    run_at: DateTime<Utc>,
    interval: Option<Duration>,
) -> Result<TaskId, TaskError>
Expand description

Enqueues a task with the specified parameters.

Examples found in repository?
examples/producer.rs (lines 39-45)
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}