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
use hammerwork::{
    HammerworkError, Job, JobPriority, JobQueue,
    batch::{JobBatch, PartialFailureMode},
    queue::DatabaseQueue,
    stats::{InMemoryStatsCollector, StatisticsCollector},
    worker::{JobHandler, Worker, WorkerPool},
};
use serde_json::json;
use sqlx::{Pool, Postgres};
use std::sync::Arc;
use std::time::Duration;
use tracing::{error, info};

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

    // Connect to PostgreSQL
    let database_url = std::env::var("DATABASE_URL")
        .unwrap_or_else(|_| "postgresql://localhost/hammerwork_test".to_string());

    let pool = Pool::<Postgres>::connect(&database_url).await?;
    let queue = Arc::new(JobQueue::new(pool));

    // Initialize database tables
    // Run `cargo hammerwork migrate` to set up the database schema before running this example

    // Create statistics collector
    let stats_collector = Arc::new(InMemoryStatsCollector::new_default());

    // Demo: Worker with batch processing enabled
    info!("=== Worker Batch Processing Demo ===");
    batch_worker_demo(&queue, &stats_collector).await?;

    info!("Worker batch processing demo completed successfully!");
    Ok(())
}

async fn batch_worker_demo(
    queue: &Arc<JobQueue<Postgres>>,
    stats_collector: &Arc<InMemoryStatsCollector>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    info!("Setting up worker with batch processing enabled...");

    // Create a job handler that demonstrates batch processing features
    let handler: JobHandler = Arc::new(|job: Job| {
        Box::pin(async move {
            let job_type = job
                .payload
                .get("type")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown");
            let batch_info = if job.batch_id.is_some() {
                format!(" (batch: {})", job.batch_id.unwrap())
            } else {
                String::new()
            };

            match job_type {
                "data_processing" => {
                    let file_name = job
                        .payload
                        .get("file")
                        .and_then(|v| v.as_str())
                        .unwrap_or("unknown");
                    info!("🔄 Processing data file: {}{}", file_name, batch_info);

                    // Simulate processing time based on file size
                    let size = job
                        .payload
                        .get("size_mb")
                        .and_then(|v| v.as_u64())
                        .unwrap_or(1);
                    let processing_time = Duration::from_millis(size * 10); // 10ms per MB
                    tokio::time::sleep(processing_time).await;

                    // Simulate occasional processing failures
                    if file_name.contains("corrupted") {
                        return Err(HammerworkError::Processing(format!(
                            "File {} is corrupted and cannot be processed",
                            file_name
                        )));
                    }

                    info!("✅ Completed processing: {}", file_name);
                }
                "image_resize" => {
                    let image_id = job
                        .payload
                        .get("image_id")
                        .and_then(|v| v.as_str())
                        .unwrap_or("unknown");
                    let dimensions = job
                        .payload
                        .get("target_size")
                        .and_then(|v| v.as_str())
                        .unwrap_or("1024x768");

                    info!(
                        "🖼️ Resizing image {} to {}{}",
                        image_id, dimensions, batch_info
                    );

                    // Simulate image processing
                    tokio::time::sleep(Duration::from_millis(100)).await;

                    // Simulate memory issues for large images
                    if dimensions.contains("4K") {
                        return Err(HammerworkError::Processing(format!(
                            "Insufficient memory to resize image {} to {}",
                            image_id, dimensions
                        )));
                    }

                    info!("✅ Image resized: {}", image_id);
                }
                "email_send" => {
                    let recipient = job
                        .payload
                        .get("to")
                        .and_then(|v| v.as_str())
                        .unwrap_or("unknown");
                    let subject = job
                        .payload
                        .get("subject")
                        .and_then(|v| v.as_str())
                        .unwrap_or("No subject");

                    info!(
                        "📧 Sending email to {} with subject '{}'{}",
                        recipient, subject, batch_info
                    );

                    // Simulate email sending
                    tokio::time::sleep(Duration::from_millis(50)).await;

                    // Simulate email delivery failures
                    if recipient.contains("bounced") {
                        return Err(HammerworkError::Processing(format!(
                            "Email delivery failed to {}: Recipient address bounced",
                            recipient
                        )));
                    }

                    info!("✅ Email sent to: {}", recipient);
                }
                _ => {
                    error!("❌ Unknown job type: {}", job_type);
                    return Err(HammerworkError::Processing(format!(
                        "Unknown job type: {}",
                        job_type
                    )));
                }
            }

            Ok(())
        })
    });

    // Create worker with batch processing enabled
    let worker = Worker::new(queue.clone(), "batch_processing_queue".to_string(), handler)
        .with_batch_processing_enabled(true) // Enable batch processing features
        .with_poll_interval(Duration::from_millis(100))
        .with_max_retries(3)
        .with_stats_collector(stats_collector.clone())
        .with_priority_weights(
            hammerwork::PriorityWeights::new()
                .with_weight(JobPriority::Critical, 50)
                .with_weight(JobPriority::High, 20)
                .with_weight(JobPriority::Normal, 10),
        );

    // Get initial batch statistics
    let initial_stats = worker.get_batch_stats();
    info!(
        "Initial batch stats - Jobs processed: {}, Batches completed: {}",
        initial_stats.jobs_processed, initial_stats.batches_completed
    );

    // Create multiple batches to demonstrate batch processing
    let batches = create_demo_batches();
    let mut batch_ids = Vec::new();

    for batch in batches {
        info!(
            "Enqueueing batch: {} with {} jobs",
            batch.name,
            batch.job_count()
        );
        let batch_id = queue.enqueue_batch(batch).await?;
        batch_ids.push(batch_id);
    }

    // Start the worker pool
    let mut worker_pool = WorkerPool::new();
    worker_pool.add_worker(worker);

    info!(
        "Starting worker pool to process {} batches...",
        batch_ids.len()
    );

    // Run the worker pool for a limited time
    let worker_handle = tokio::spawn(async move {
        if let Err(e) = worker_pool.start().await {
            error!("Worker pool error: {}", e);
        }
    });

    // Monitor batch progress
    let mut completed_batches = 0;
    for _ in 0..60 {
        // Monitor for up to 60 seconds
        tokio::time::sleep(Duration::from_secs(1)).await;

        let mut all_complete = true;
        for &batch_id in &batch_ids {
            let batch_result = queue.get_batch_status(batch_id).await?;

            if batch_result.pending_jobs > 0 {
                all_complete = false;
            } else if batch_result.pending_jobs == 0 && batch_result.total_jobs > 0 {
                // This batch just completed
                if completed_batches < batch_ids.len() {
                    completed_batches += 1;
                    let success_rate = batch_result.success_rate();
                    info!(
                        "Batch {} completed! Success rate: {:.1}% ({}/{} jobs successful)",
                        batch_id,
                        success_rate * 100.0,
                        batch_result.completed_jobs,
                        batch_result.total_jobs
                    );
                }
            }
        }

        if all_complete {
            info!("🎉 All batches completed!");
            break;
        }
    }

    // Stop the worker
    worker_handle.abort();

    // Display final batch processing statistics
    info!("\n=== Final Batch Processing Statistics ===");

    for &batch_id in &batch_ids {
        let batch_result = queue.get_batch_status(batch_id).await?;
        info!(
            "Batch {}: {} total, {} completed, {} failed, {:.1}% success rate",
            batch_id,
            batch_result.total_jobs,
            batch_result.completed_jobs,
            batch_result.failed_jobs,
            batch_result.success_rate() * 100.0
        );
    }

    // Display overall statistics
    let overall_stats = stats_collector
        .get_system_statistics(Duration::from_secs(300))
        .await?;
    info!("\nOverall Statistics:");
    info!("  Total processed: {}", overall_stats.total_processed);
    info!("  Completed: {}", overall_stats.completed);
    info!("  Failed: {}", overall_stats.failed);
    info!(
        "  Success rate: {:.1}%",
        (1.0 - overall_stats.error_rate) * 100.0
    );

    if overall_stats.avg_processing_time_ms > 0.0 {
        info!(
            "  Average processing time: {:.1}ms",
            overall_stats.avg_processing_time_ms
        );
    }

    // Clean up batches
    for batch_id in batch_ids {
        queue.delete_batch(batch_id).await?;
    }

    info!("Batch processing demo completed and cleaned up successfully!\n");

    Ok(())
}

/// Create sample batches for demonstration
fn create_demo_batches() -> Vec<JobBatch> {
    vec![
        // Batch 1: Data processing jobs
        JobBatch::new("data_processing_batch")
            .with_jobs(vec![
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "data_processing",
                        "file": "dataset_001.csv",
                        "size_mb": 50
                    }),
                )
                .as_high_priority(),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "data_processing",
                        "file": "dataset_002.csv",
                        "size_mb": 30
                    }),
                ),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "data_processing",
                        "file": "corrupted_data.csv",  // This will fail
                        "size_mb": 25
                    }),
                ),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "data_processing",
                        "file": "dataset_003.csv",
                        "size_mb": 40
                    }),
                ),
            ])
            .with_partial_failure_handling(PartialFailureMode::ContinueOnError)
            .with_metadata("department", "data_science")
            .with_metadata("priority", "high"),
        // Batch 2: Image processing jobs
        JobBatch::new("image_processing_batch")
            .with_jobs(vec![
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "image_resize",
                        "image_id": "IMG_001.jpg",
                        "target_size": "1920x1080"
                    }),
                )
                .as_critical(),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "image_resize",
                        "image_id": "IMG_002.jpg",
                        "target_size": "1024x768"
                    }),
                ),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "image_resize",
                        "image_id": "IMG_003.jpg",
                        "target_size": "4K"  // This will fail due to memory
                    }),
                ),
            ])
            .with_partial_failure_handling(PartialFailureMode::CollectErrors)
            .with_metadata("campaign", "product_images")
            .with_metadata("quality", "high"),
        // Batch 3: Email notifications
        JobBatch::new("email_notification_batch")
            .with_jobs(vec![
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "email_send",
                        "to": "user1@example.com",
                        "subject": "Welcome to our service!"
                    }),
                )
                .as_high_priority(),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "email_send",
                        "to": "user2@example.com",
                        "subject": "Your order has shipped"
                    }),
                ),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "email_send",
                        "to": "bounced@example.com",  // This will fail
                        "subject": "Monthly newsletter"
                    }),
                ),
                Job::new(
                    "batch_processing_queue".to_string(),
                    json!({
                        "type": "email_send",
                        "to": "user3@example.com",
                        "subject": "Password reset confirmation"
                    }),
                )
                .as_critical(),
            ])
            .with_partial_failure_handling(PartialFailureMode::ContinueOnError)
            .with_metadata("campaign_id", "Q4_2024")
            .with_metadata("email_type", "transactional"),
    ]
}