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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- **In-memory result storage works.** A job configured with `ResultStorage::Memory` had
its result silently discarded: workers only stored `ResultStorage::Database` results.
Workers now store them in an in-memory store owned by the `JobQueue` and shared by its
clones, before the job is marked Completed, and `get_job_result`, `delete_job_result`,
`cleanup_expired_results` and `delete_job` use it. Results expire at their TTL (checked
on read and pruned as results are stored), and the store holds at most 10,000 results
by default (`queue::DEFAULT_MEMORY_RESULT_CAPACITY`), evicting the one that expires
soonest, or the oldest; set the limit with the new
`JobQueue::with_memory_result_capacity`. In-memory results are per process: other
processes, including `cargo hammerwork` and the web dashboard, don't see them, and
they are lost on restart. See [Job Results](docs/job-types.md#job-results).
`TestQueue::delete_job` now also deletes the job's result.

## [2.0.0] - 2026-10-09

A hardening release ([#7](https://github.com/CodingAnarchy/hammerwork/issues/7)). It fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ A high-performance, database-driven job queue for Rust with comprehensive featur
- **Multi-database support**: PostgreSQL and MySQL backends with optimized dependency queries
- **Advanced retry strategies**: Exponential backoff, linear, Fibonacci, and custom retry patterns with jitter
- **Job prioritization**: Five priority levels with weighted and strict scheduling algorithms
- **Result storage**: Database and in-memory result storage with TTL and automatic cleanup
- **Result storage**: Database and in-memory (per-process, bounded) result storage with TTL and automatic cleanup ([Job Results](docs/job-types.md#job-results))
- **Worker autoscaling**: Dynamic worker pool scaling based on queue depth and configurable thresholds
- **Batch operations**: High-performance bulk job enqueuing with optimized worker processing
- **Cron scheduling**: Full cron expression support with timezone awareness
Expand Down
70 changes: 70 additions & 0 deletions docs/job-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,76 @@ let handler: JobHandler = Arc::new(|job: Job| {
});
```

## Job Results

A handler created with `Worker::new_with_result_handler` can return data with
`JobResult::with_data`. The job's result configuration decides what happens to it:

| `ResultStorage` | Where the result goes | Who can read it |
|---|---|---|
| `None` (default) | Discarded | Nobody |
| `Database` | The job's row in `hammerwork_jobs` | Any process with database access |
| `Memory` | The `JobQueue`'s in-memory store | Only this process, through the same `JobQueue` (or its clones) |

The worker stores the result before it marks the job Completed, so whoever sees the job as
Completed can read its result with `get_job_result`. `Job::with_result_ttl` sets how long
the result is kept; without a TTL it is kept until it is deleted (`delete_job_result`,
or deleting the job). `cleanup_expired_results` removes expired results from the
database and from the in-memory store.

```rust,no_run
# #[allow(unused_imports)] use hammerwork::{*, queue::*, worker::*};
# #[allow(unused_imports)] use serde_json::json;
# #[allow(unused_imports)] use std::sync::Arc;
# #[allow(unused_variables)]
# async fn doc(pool: sqlx::PgPool) -> std::result::Result<(), Box<dyn std::error::Error>> {
use hammerwork::{Job, JobQueue, ResultStorage, Worker, WorkerPool};
use hammerwork::worker::{JobHandlerWithResult, JobResult};
use std::time::Duration;

// Keep up to 50,000 in-memory results (the default is 10,000).
let queue = Arc::new(JobQueue::new(pool).with_memory_result_capacity(50_000));

let handler: JobHandlerWithResult = Arc::new(|job: Job| {
Box::pin(async move { Ok(JobResult::with_data(json!({"total": job.payload["n"]}))) })
});
let mut pool = WorkerPool::new();
pool.add_worker(Worker::new_with_result_handler(queue.clone(), "reports".to_string(), handler));

let job = Job::new("reports".to_string(), json!({"n": 42}))
.with_result_storage(ResultStorage::Memory)
.with_result_ttl(Duration::from_secs(600));
let job_id = queue.enqueue(job).await?;

// ...once the job has completed, in the same process:
if let Some(result) = queue.get_job_result(job_id).await? {
println!("total = {}", result["total"]);
}
# Ok(())
# }
```

In-memory results are faster to store than database results, but:

- **They are per process.** A worker in one process stores them in its own `JobQueue`.
Another process (another worker host, `cargo hammerwork`, the web dashboard) reads
only the database, so for it the job has no stored result. A separately created
`JobQueue` in the same process has its own store too: share one `Arc<JobQueue>`.
- **They are lost when the process exits.**
- **They are bounded.** Each `JobQueue` keeps at most `DEFAULT_MEMORY_RESULT_CAPACITY`
(10,000) results, or the number set with `JobQueue::with_memory_result_capacity`. When
it is full, storing a result evicts the one that expires soonest, or the oldest if
none expire. Expired results are removed when read and pruned as new results are
stored.
- They don't appear in `Job::result_data` (from `get_job`), which reflects the database
columns; read them with `get_job_result`.

`ResultConfig::with_max_size` is recorded with the job but not enforced, for database
and in-memory results alike.

The `TestQueue` (feature `test`) keeps all results in memory regardless of
`ResultStorage`, expiring them by its mock clock.

## Error Handling

Jobs can fail and be retried automatically:
Expand Down
Loading
Loading