hammerwork 1.15.5

A high-performance, database-driven job queue for Rust with PostgreSQL and MySQL support, featuring job prioritization, cron scheduling, event streaming (Kafka/Kinesis/PubSub), webhooks, rate limiting, Prometheus metrics, and comprehensive monitoring
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use hammerwork::{
    Job, JobQueue, Worker, WorkerPool,
    priority::{JobPriority, PriorityWeights},
    queue::DatabaseQueue,
    stats::{InMemoryStatsCollector, StatisticsCollector},
};
use serde_json::json;
use sqlx::postgres::PgPoolOptions;
use std::{sync::Arc, time::Duration};
use tokio::time::sleep;
use tracing::{error, info};

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing
    tracing_subscriber::fmt::init();

    info!("🔧 Hammerwork Priority Example");
    info!("This example demonstrates job prioritization and weighted scheduling");

    // Create database connection
    let database_url = std::env::var("DATABASE_URL")
        .unwrap_or_else(|_| "postgresql://postgres:password@localhost/hammerwork".to_string());

    let pool = PgPoolOptions::new()
        .max_connections(10)
        .connect(&database_url)
        .await?;

    let queue = Arc::new(JobQueue::new(pool));

    // NOTE: Run `cargo hammerwork migrate` to create the necessary database tables
    // before running this example
    info!("📋 Make sure to run 'cargo hammerwork migrate' first to set up database tables");

    // Setup statistics collection
    let stats_collector = Arc::new(InMemoryStatsCollector::new_default());

    // Demonstrate different priority levels
    demonstrate_priority_levels(&queue).await?;

    // Demonstrate weighted priority scheduling
    demonstrate_weighted_scheduling(&queue, stats_collector.clone()).await?;

    // Demonstrate strict priority scheduling
    demonstrate_strict_priority(&queue, stats_collector.clone()).await?;

    // Show priority statistics
    show_priority_statistics(stats_collector).await?;

    info!("🎉 Priority example completed successfully!");
    Ok(())
}

/// Demonstrate creating jobs with different priority levels
async fn demonstrate_priority_levels(
    queue: &Arc<JobQueue<sqlx::Postgres>>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    info!("\n📋 === PRIORITY LEVELS DEMONSTRATION ===");

    // Create jobs with different priorities
    let critical_job = Job::new(
        "notifications".to_string(),
        json!({
            "type": "system_alert",
            "message": "Critical system failure detected!"
        }),
    )
    .as_critical();

    let high_job = Job::new(
        "notifications".to_string(),
        json!({
            "type": "user_alert",
            "message": "Payment failed for premium account"
        }),
    )
    .as_high_priority();

    let normal_job = Job::new(
        "processing".to_string(),
        json!({
            "type": "data_processing",
            "batch_id": 12345
        }),
    ); // Default normal priority

    let low_job = Job::new(
        "cleanup".to_string(),
        json!({
            "type": "log_cleanup",
            "older_than_days": 30
        }),
    )
    .as_low_priority();

    let background_job = Job::new(
        "analytics".to_string(),
        json!({
            "type": "analytics_aggregation",
            "time_range": "last_month"
        }),
    )
    .as_background();

    // Enqueue all jobs
    let critical_id = queue.enqueue(critical_job).await?;
    let high_id = queue.enqueue(high_job).await?;
    let normal_id = queue.enqueue(normal_job).await?;
    let low_id = queue.enqueue(low_job).await?;
    let background_id = queue.enqueue(background_job).await?;

    info!("📤 Enqueued jobs with different priorities:");
    info!("  🔴 Critical:    {} (system alert)", critical_id);
    info!("  🟡 High:        {} (payment alert)", high_id);
    info!("  🟢 Normal:      {} (data processing)", normal_id);
    info!("  🔵 Low:         {} (log cleanup)", low_id);
    info!("  ⚫ Background:  {} (analytics)", background_id);

    Ok(())
}

/// Demonstrate weighted priority scheduling (default behavior)
async fn demonstrate_weighted_scheduling(
    queue: &Arc<JobQueue<sqlx::Postgres>>,
    stats_collector: Arc<InMemoryStatsCollector>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    info!("\n⚖️  === WEIGHTED PRIORITY SCHEDULING ===");

    // Create custom priority weights
    let weights = PriorityWeights::new()
        .with_weight(JobPriority::Critical, 100) // Very high weight
        .with_weight(JobPriority::High, 20) // High weight
        .with_weight(JobPriority::Normal, 5) // Normal weight
        .with_weight(JobPriority::Low, 2) // Low weight
        .with_weight(JobPriority::Background, 1) // Minimal weight
        .with_fairness_factor(0.1); // 10% fairness to prevent starvation

    info!("🎛️  Priority weights configured:");
    info!(
        "   Critical: {}x weight",
        weights.get_weight(JobPriority::Critical)
    );
    info!("   High: {}x weight", weights.get_weight(JobPriority::High));
    info!(
        "   Normal: {}x weight",
        weights.get_weight(JobPriority::Normal)
    );
    info!("   Low: {}x weight", weights.get_weight(JobPriority::Low));
    info!(
        "   Background: {}x weight",
        weights.get_weight(JobPriority::Background)
    );
    info!(
        "   Fairness factor: {:.1}%",
        weights.fairness_factor() * 100.0
    );

    // Create worker with weighted priority
    let worker = Worker::new(
        queue.clone(),
        "notifications".to_string(),
        Arc::new(|job| {
            Box::pin(async move {
                info!(
                    "🔄 Processing {} job: {} (Priority: {:?})",
                    job.queue_name, job.id, job.priority
                );

                // Simulate different processing times based on priority
                let delay = match job.priority {
                    JobPriority::Critical => Duration::from_millis(100), // Fast
                    JobPriority::High => Duration::from_millis(300),     // Medium-fast
                    JobPriority::Normal => Duration::from_millis(500),   // Medium
                    JobPriority::Low => Duration::from_millis(800),      // Slow
                    JobPriority::Background => Duration::from_millis(1000), // Slowest
                };

                sleep(delay).await;
                info!("✅ Completed job: {} (took {:?})", job.id, delay);
                Ok(())
            })
        }),
    )
    .with_priority_weights(weights)
    .with_stats_collector(stats_collector.clone())
    .with_poll_interval(Duration::from_millis(100));

    info!("👷 Starting weighted priority worker...");

    // Start worker in background
    let (shutdown_tx, shutdown_rx) = tokio::sync::mpsc::channel(1);
    let worker_handle = {
        let worker = worker;
        tokio::spawn(async move {
            if let Err(e) = worker.run(shutdown_rx).await {
                error!("Worker error: {}", e);
            }
        })
    };

    // Let worker process for a bit
    sleep(Duration::from_secs(3)).await;

    // Shutdown worker
    let _ = shutdown_tx.send(()).await;
    let _ = worker_handle.await;

    info!("⏹️  Weighted priority worker stopped");
    Ok(())
}

/// Demonstrate strict priority scheduling
async fn demonstrate_strict_priority(
    queue: &Arc<JobQueue<sqlx::Postgres>>,
    stats_collector: Arc<InMemoryStatsCollector>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    info!("\n🎯 === STRICT PRIORITY SCHEDULING ===");
    info!("In strict mode, higher priority jobs are ALWAYS processed first");

    // Add more jobs to demonstrate strict ordering
    let jobs = vec![
        ("Background task", JobPriority::Background),
        ("Regular task", JobPriority::Normal),
        ("Urgent task", JobPriority::High),
        ("Emergency task", JobPriority::Critical),
        ("Another normal task", JobPriority::Normal),
        ("Low priority task", JobPriority::Low),
    ];

    for (name, priority) in &jobs {
        let job = Job::new(
            "processing".to_string(),
            json!({
                "task_name": name,
                "timestamp": chrono::Utc::now().to_rfc3339()
            }),
        )
        .with_priority(*priority);

        let job_id = queue.enqueue(job).await?;
        info!(
            "📤 Enqueued: {} (Priority: {:?}) - ID: {}",
            name, priority, job_id
        );
    }

    // Create worker with strict priority
    let worker = Worker::new(
        queue.clone(),
        "processing".to_string(),
        Arc::new(|job| {
            Box::pin(async move {
                let task_name = job.payload["task_name"].as_str().unwrap_or("unknown");
                info!(
                    "🎯 STRICT: Processing '{}' (Priority: {:?})",
                    task_name, job.priority
                );

                // Quick processing for demo
                sleep(Duration::from_millis(200)).await;
                info!("✅ STRICT: Completed '{}'", task_name);
                Ok(())
            })
        }),
    )
    .with_strict_priority() // This enables strict priority mode
    .with_stats_collector(stats_collector.clone())
    .with_poll_interval(Duration::from_millis(100));

    info!("👷 Starting strict priority worker...");
    info!("📋 Expected order: Critical → High → Normal → Normal → Low → Background");

    // Start worker in background
    let (shutdown_tx, shutdown_rx) = tokio::sync::mpsc::channel(1);
    let worker_handle = {
        let worker = worker;
        tokio::spawn(async move {
            if let Err(e) = worker.run(shutdown_rx).await {
                error!("Worker error: {}", e);
            }
        })
    };

    // Let worker process all jobs
    sleep(Duration::from_secs(3)).await;

    // Shutdown worker
    let _ = shutdown_tx.send(()).await;
    let _ = worker_handle.await;

    info!("⏹️  Strict priority worker stopped");
    Ok(())
}

/// Show comprehensive priority statistics
async fn show_priority_statistics(
    stats_collector: Arc<InMemoryStatsCollector>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    info!("\n📊 === PRIORITY STATISTICS ===");

    // Get statistics for different queues
    let queues = vec!["notifications", "processing", "cleanup", "analytics"];

    for queue_name in queues {
        match stats_collector
            .get_queue_statistics(queue_name, Duration::from_secs(300))
            .await
        {
            Ok(stats) => {
                if stats.total_processed > 0 {
                    info!("\n📈 Queue: {}", queue_name);
                    info!("   Total processed: {}", stats.total_processed);
                    info!("   Completed: {}", stats.completed);
                    info!(
                        "   Average processing time: {:.1}ms",
                        stats.avg_processing_time_ms
                    );
                    info!("   Throughput: {:.1} jobs/min", stats.throughput_per_minute);
                    info!("   Error rate: {:.1}%", stats.error_rate * 100.0);

                    // Show priority breakdown if available
                    if let Some(priority_stats) = &stats.priority_stats {
                        info!("   Priority breakdown:");
                        for priority in JobPriority::all_priorities() {
                            if let Some(count) = priority_stats.job_counts.get(&priority) {
                                if *count > 0 {
                                    let percentage = priority_stats
                                        .priority_distribution
                                        .get(&priority)
                                        .copied()
                                        .unwrap_or(0.0);
                                    let avg_time = priority_stats
                                        .avg_processing_times
                                        .get(&priority)
                                        .copied()
                                        .unwrap_or(0.0);

                                    info!(
                                        "     {:?}: {} jobs ({:.1}%, avg: {:.1}ms)",
                                        priority, count, percentage, avg_time
                                    );
                                }
                            }
                        }

                        // Check for priority starvation
                        let starved = priority_stats.check_starvation(5.0); // 5% threshold
                        if !starved.is_empty() {
                            info!("   ⚠️  Potential starvation detected for: {:?}", starved);
                            info!("      Consider adjusting priority weights or fairness factor");
                        }
                    }
                }
            }
            Err(e) => {
                error!("Failed to get stats for queue {}: {}", queue_name, e);
            }
        }
    }

    // Show overall system statistics
    match stats_collector
        .get_all_statistics(Duration::from_secs(300))
        .await
    {
        Ok(all_stats) => {
            if !all_stats.is_empty() {
                info!("\n🌐 Overall system statistics:");
                let total_processed: u64 =
                    all_stats.iter().map(|s| s.statistics.total_processed).sum();
                let total_completed: u64 = all_stats.iter().map(|s| s.statistics.completed).sum();
                let avg_throughput: f64 = all_stats
                    .iter()
                    .map(|s| s.statistics.throughput_per_minute)
                    .sum::<f64>()
                    / all_stats.len() as f64;

                info!("   Total jobs processed: {}", total_processed);
                info!("   Total completed: {}", total_completed);
                info!(
                    "   Average system throughput: {:.1} jobs/min",
                    avg_throughput
                );
                info!("   Active queues: {}", all_stats.len());
            }
        }
        Err(e) => {
            error!("Failed to get overall stats: {}", e);
        }
    }

    Ok(())
}

/// Demonstrate WorkerPool with mixed priority configurations
#[allow(dead_code)]
async fn demonstrate_worker_pool_priorities(
    queue: Arc<JobQueue<sqlx::Postgres>>,
    stats_collector: Arc<InMemoryStatsCollector>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    info!("\n👥 === WORKER POOL WITH PRIORITY CONFIGURATIONS ===");

    let mut pool = WorkerPool::new().with_stats_collector(stats_collector.clone());

    // Add workers with different priority configurations
    type JobHandler = Arc<
        dyn Fn(
                Job,
            ) -> std::pin::Pin<
                Box<dyn std::future::Future<Output = hammerwork::Result<()>> + Send>,
            > + Send
            + Sync,
    >;

    let handler: JobHandler = Arc::new(|job: Job| {
        Box::pin(async move {
            info!(
                "🔄 Pool worker processing: {} (Priority: {:?})",
                job.id, job.priority
            );
            sleep(Duration::from_millis(300)).await;
            Ok(())
        })
    });

    // Worker 1: Strict priority for critical notifications
    let critical_worker = Worker::new(queue.clone(), "notifications".to_string(), handler.clone())
        .with_strict_priority()
        .with_poll_interval(Duration::from_millis(50));

    // Worker 2: Weighted priority for general processing
    let weighted_worker = Worker::new(queue.clone(), "processing".to_string(), handler.clone())
        .with_weighted_priority()
        .with_poll_interval(Duration::from_millis(100));

    // Worker 3: Custom weights for specialized tasks
    let custom_weights = PriorityWeights::new()
        .with_weight(JobPriority::Critical, 50)
        .with_weight(JobPriority::High, 15)
        .with_fairness_factor(0.2);

    let custom_worker = Worker::new(queue.clone(), "cleanup".to_string(), handler)
        .with_priority_weights(custom_weights)
        .with_poll_interval(Duration::from_millis(200));

    pool.add_worker(critical_worker);
    pool.add_worker(weighted_worker);
    pool.add_worker(custom_worker);

    info!("🚀 Starting worker pool with 3 workers (different priority configs)...");

    // Note: In a real application, you would call:
    // pool.start().await?;
    // For this example, we just demonstrate the setup

    info!("✅ Worker pool configured successfully");
    Ok(())
}