Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ See [Upgrading from 2.x to 3.0](docs/upgrading-to-3.0.md) for what to change.
- **No features are enabled by default** (#92). `default` used to be `["metrics", "alerting", "webhooks"]`, which pulled `warp`, `reqwest`, `lettre` and `prometheus` into every build. It is now empty. Enable what you use, or the new `full` feature (`metrics`, `alerting`, `webhooks`, `streaming`, `tracing`, `encryption`; no database backend, KMS key source, event stream sink or `test-queue`). Code that uses `hammerwork::metrics`, `hammerwork::alerting` or `hammerwork::webhooks` must now enable those features. `cargo-hammerwork` and `hammerwork-web` enable the features they need themselves.
- **`azure-kv` is renamed `azure-kms`**, to match `aws-kms`, `gcp-kms` and `vault-kms` (#92). There is no alias. The same rename applies to the `cargo-hammerwork` feature.
- **`test` is renamed `test-queue`**, after the `TestQueue` it enables (#92). There is no alias.
- **`DatabaseQueue` is sealed** (#87). It can no longer be implemented outside Hammerwork; its implementations are `JobQueue<Postgres>`, `JobQueue<MySql>` and `TestQueue`, and it stays usable as a bound (`impl DatabaseQueue`, `Q: DatabaseQueue`). Use `TestQueue` (feature `test-queue`) to test without a database. The default method bodies that existed only for implementations outside the crate are gone (the one-at-a-time fallback for batch claims, and the fallbacks of `release_job_run`, `finish_job_run`, `complete_job_run_with_children`, `heartbeat_job`, `start_claimed_run`, `requeue_stale_jobs`, `purge_expired_encrypted_jobs`, `purge_archived_jobs_in_queue` and `list_queue_names`); every backend implements these methods, so behaviour does not change. `queue::RELEASED_JOB_MESSAGE`, the error message of the removed `release_job_run` fallback, is removed.
- **One claim method** (#89). `DatabaseQueue::dequeue(queue, &DequeueOptions)` replaces `dequeue`, `dequeue_with_priority_weights`, `dequeue_leased`, `dequeue_batch` and `dequeue_batch_leased`. It returns `Vec<Job>`. The new `#[non_exhaustive]` `queue::DequeueOptions` (also `hammerwork::DequeueOptions`) has `priority_weights: Option<PriorityWeights>`, `lease_duration: Option<Duration>` (`None` is `DEFAULT_LEASE_DURATION`) and `max_jobs` (default 1), built with `DequeueOptions::new()`/`default()`, `DequeueOptions::batch(n)`, `with_priority_weights`, `with_lease_duration` and `with_max_jobs`. `DatabaseQueue::dequeue_one(queue)` keeps the common case short: it is the 2.x `dequeue(queue)` (one job, strict priority, default lease, `Option<Job>`). Claims behave as before: a `max_jobs` of 1 claims like the old single-job methods, larger values like the old batch claims.
- **`Worker::with_batch_processing_enabled` is removed** (#89). `with_batch_size(n)` enables batch mode, as it already did; `with_batch_size(1)` replaces `with_batch_processing_enabled(true)` (batch statistics, one job per poll). New optional `[worker]` settings `batch_size` and `batch_concurrency` (`WorkerConfig::batch_size`, `WorkerConfig::batch_concurrency`) set them from configuration through `Worker::with_config`; code that builds `WorkerConfig` with a struct literal must add them (or use `..Default::default()`).
- **`Worker::with_max_retries` is renamed `with_attempt_cap`** (#89), since it can only lower a job's own `max_attempts`. There is no alias.

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,13 @@ async fn test_delayed_job_processing() {
let job_id = queue.enqueue(job).await.unwrap();

// Job shouldn't be available immediately
assert!(queue.dequeue("test_queue").await.unwrap().is_none());
assert!(queue.dequeue_one("test_queue").await.unwrap().is_none());

// Advance time past scheduled time
clock.advance(Duration::hours(2));

// Now job should be available for processing
let dequeued = queue.dequeue("test_queue").await.unwrap().unwrap();
let dequeued = queue.dequeue_one("test_queue").await.unwrap().unwrap();
assert_eq!(dequeued.id, job_id);

// Complete the job
Expand Down
2 changes: 1 addition & 1 deletion cargo-hammerwork/src/integration_tests/job_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ macro_rules! job_state_scenarios {
"retry --queue must not touch other queues"
);
// The retried job is actually runnable by the library's dequeue.
let dequeued = queue.dequeue(&qname).await.unwrap();
let dequeued = queue.dequeue_one(&qname).await.unwrap();
assert!(dequeued.is_some(), "retried job should be dequeueable");

// --- retry of a single job in a state that cannot be retried is rejected ---
Expand Down
17 changes: 8 additions & 9 deletions docs/batch-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ for (job_id, error) in &result.job_errors {

A worker in batch mode claims up to `batch_size` jobs per poll in one round trip instead
of one job per round trip, then runs them `batch_concurrency` at a time (one at a time by
default). `with_batch_size(n)` enables batch mode; `with_batch_processing_enabled(true)`
alone keeps the default batch size of 1, so it claims one job per poll as before and only
adds batch statistics:
default). `with_batch_size(n)` enables batch mode; `with_batch_size(1)` claims one job
per poll as without batch mode and only adds batch statistics. The `[worker]` settings
`batch_size` and `batch_concurrency` (`WorkerConfig`) set the same from configuration:

```rust,no_run
# #[allow(unused_imports)] use hammerwork::queue::DatabaseQueue;
Expand Down Expand Up @@ -348,13 +348,12 @@ The queue API claims batches too, for custom consumers:
# #[allow(unused_imports)] use hammerwork::queue::DatabaseQueue;
# #[allow(unused_variables, unused_mut, dead_code, unreachable_code)]
# async fn doc(queue: std::sync::Arc<hammerwork::JobQueue<sqlx::Postgres>>, handler: hammerwork::worker::JobHandler, jobs: Vec<hammerwork::Job>, worker: hammerwork::Worker<sqlx::Postgres>, batch_id: hammerwork::BatchId) -> std::result::Result<(), Box<dyn std::error::Error>> {
use hammerwork::{JobOutcome, queue::DatabaseQueue};
use hammerwork::{JobOutcome, queue::{DatabaseQueue, DequeueOptions}};
use std::time::Duration;

// Up to 20 jobs, strict priority order, each leased for 2 minutes.
let jobs = queue
.dequeue_batch_leased("emails", None, Duration::from_secs(120), 20)
.await?;
let options = DequeueOptions::batch(20).with_lease_duration(Duration::from_secs(120));
let jobs = queue.dequeue("emails", &options).await?;
for job in jobs {
let run = job.clone();
let job = queue.decrypt_job(job).await?; // a no-op for unencrypted jobs
Expand Down Expand Up @@ -608,7 +607,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let batch_id = queue.enqueue_batch(batch).await?;
println!("Enqueued batch: {}", batch_id);

// Worker with batch processing enabled
// Worker in batch mode: claims up to 20 jobs per poll
let handler: JobHandler = Arc::new(|_job: Job| {
Box::pin(async move {
tokio::time::sleep(Duration::from_millis(100)).await;
Expand All @@ -617,7 +616,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
});

let worker = Worker::new(queue.clone(), "data_processing".to_string(), handler)
.with_batch_processing_enabled(true);
.with_batch_size(20);

let mut worker_pool = WorkerPool::new();
worker_pool.add_worker(worker);
Expand Down
2 changes: 1 addition & 1 deletion docs/cron-scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ async fn setup_backup_system(

let backup_worker = Worker::new(queue, "backup".to_string(), backup_handler)
.with_default_timeout(Duration::from_secs(4 * 60 * 60))
.with_max_retries(2)
.with_attempt_cap(2)
.with_retry_delay(Duration::from_secs(30 * 60));

let mut pool = WorkerPool::new();
Expand Down
4 changes: 2 additions & 2 deletions docs/job-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ let job = Job::new("api_call".to_string(), json!({"url": "https://api.example.co
.with_max_attempts(5); // At most 5 attempts in total (the first run plus 4 retries)

// Worker-level retry configuration. The job's max_attempts is the limit;
// with_max_retries only caps it for jobs processed by this worker.
// with_attempt_cap only caps it for jobs processed by this worker.
let worker = Worker::new(queue, "default".to_string(), handler)
.with_max_retries(3)
.with_attempt_cap(3)
.with_retry_delay(std::time::Duration::from_secs(30)); // Wait 30s between retries
# Ok(())
# }
Expand Down
10 changes: 5 additions & 5 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
// Create workers with comprehensive configuration
let email_worker = Worker::new(queue.clone(), "email".to_string(), handler.clone())
.with_poll_interval(Duration::from_secs(1))
.with_max_retries(3)
.with_attempt_cap(3)
.with_default_timeout(Duration::from_secs(30))
.with_rate_limit(RateLimit::per_second(2))
.with_stats_collector(Arc::clone(&stats_collector));

let data_worker = Worker::new(queue.clone(), "data_processing".to_string(), handler.clone())
.with_poll_interval(Duration::from_secs(2))
.with_max_retries(2)
.with_attempt_cap(2)
.with_default_timeout(Duration::from_secs(60))
.with_stats_collector(Arc::clone(&stats_collector));

Expand Down Expand Up @@ -263,13 +263,13 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
// Create workers for different job types
let image_worker = Worker::new(queue.clone(), "image_processing".to_string(), handler.clone())
.with_poll_interval(Duration::from_secs(1))
.with_max_retries(2)
.with_attempt_cap(2)
.with_default_timeout(Duration::from_secs(120)) // Longer timeout for images
.with_stats_collector(Arc::clone(&stats_collector));

let email_worker = Worker::new(queue.clone(), "email_queue".to_string(), handler.clone())
.with_poll_interval(Duration::from_millis(500))
.with_max_retries(3)
.with_attempt_cap(3)
.with_default_timeout(Duration::from_secs(30))
.with_stats_collector(Arc::clone(&stats_collector));

Expand Down Expand Up @@ -329,7 +329,7 @@ let job = Job::with_delay("queue_name".to_string(), json!({"data": "value"}), ch
# fn example(queue: Arc<JobQueue<sqlx::Postgres>>, handler: JobHandler, stats_collector: Arc<dyn StatisticsCollector>) {
let worker = Worker::new(queue.clone(), "queue_name".to_string(), handler)
.with_poll_interval(Duration::from_secs(1)) // How often to check for jobs
.with_max_retries(3) // Default retry attempts
.with_attempt_cap(3) // At most 3 attempts per job
.with_default_timeout(Duration::from_secs(30)) // Default job timeout
.with_rate_limit(RateLimit::per_second(2)) // Rate limiting
.with_stats_collector(stats_collector); // Statistics collection
Expand Down
Loading
Loading