Skip to content

Conversation

@maxcountryman
Copy link
Owner

This refactor improves ergonomics and internal consistency by wrapping all Queue instances in Arc. It simplifies the public API, avoids unnecessary clones, and enables more flexible, re-entrant usage patterns.

Key changes:

  • Queue ownership:

    • Job, Worker, Scheduler, and EnqueuedJob now store Arc<Queue<T>>.
    • Worker::new and Scheduler::new now take Arc<Queue<T>> instead of owning the Queue directly.
  • Job-scoped helpers:

    • Job::queue() -> Arc<Queue<Self>>
    • Job::worker() -> Worker<Self>
    • Job::scheduler() -> Scheduler<Self>
    • These eliminate repetitive clone calls and centralize access to the underlying queue.
  • Borrow-based execution:

    • Job::run(&self) and Job::start(&self) now take &self instead of self.
    • Enables spawning multiple schedulers/workers for the same Job without moving ownership.
  • Removed wrappers:

    • Job::run_worker and Job::run_scheduler removed.
    • Use job.worker().run().await and job.scheduler().run().await instead.

Rationale:

  • Simpler public API: Consumers think in terms of jobs, not queues. The new helpers reduce boilerplate and expose a safer, more intuitive entry point.

  • Consistent shared state: Advisory locks and database handles are now consistently shared across job runners via Arc, reducing the risk of misuse.

  • Avoid unnecessary clones: Fewer queue.clone() calls, more efficient task spawning.

  • Supports concurrent orchestration: Borrow-based run/start methods allow tokio::spawn(job.worker().run()) without moving the job.

Migration:

  • Before:
let queue = Queue::builder().build().await?;
let job = Job::builder().queue(queue.clone()).build();
Worker::new(queue.clone(), job.clone()).run().await;
  • After:
let job = Job::builder().pool(pool).build().await?;
job.worker().run().await;

Update any code accessing queue directly to instead call job.queue().

This refactor improves ergonomics and internal consistency by wrapping
all Queue<T> instances in Arc. It simplifies the public API, avoids
unnecessary clones, and enables more flexible, re-entrant usage patterns.

Key changes:

- Queue ownership:
  - `Job`, `Worker`, `Scheduler`, and `EnqueuedJob` now store `Arc<Queue<T>>`.
  - `Worker::new` and `Scheduler::new` now take `Arc<Queue<T>>` instead of
    owning the `Queue` directly.

- Job-scoped helpers:
  - `Job::queue()` -> `Arc<Queue<Self>>`
  - `Job::worker()` -> `Worker<Self>`
  - `Job::scheduler()` -> `Scheduler<Self>`
  - These eliminate repetitive clone calls and centralize access to the
    underlying queue.

- Borrow-based execution:
  - `Job::run(&self)` and `Job::start(&self)` now take `&self` instead of `self`.
  - Enables spawning multiple schedulers/workers for the same `Job`
    without moving ownership.

- Removed wrappers:
  - `Job::run_worker` and `Job::run_scheduler` removed.
  - Use `job.worker().run().await` and `job.scheduler().run().await` instead.

Rationale:

- Simpler public API: Consumers think in terms of jobs, not queues. The
  new helpers reduce boilerplate and expose a safer, more intuitive
  entry point.

- Consistent shared state: Advisory locks and database handles are now
  consistently shared across job runners via `Arc`, reducing the risk of
  misuse.

- Avoid unnecessary clones: Fewer `queue.clone()` calls, more efficient
  task spawning.

- Supports concurrent orchestration: Borrow-based run/start methods
  allow `tokio::spawn(job.worker().run())` without moving the job.

Migration:

- Before:

```rust
let queue = Queue::builder().build().await?;
let job = Job::builder().queue(queue.clone()).build();
Worker::new(queue.clone(), job.clone()).run().await;
```

- After:

```rust
let job = Job::builder().pool(pool).build().await?;
job.worker().run().await;
```

Update any code accessing `queue` directly to instead call
`job.queue()`.
@maxcountryman maxcountryman merged commit ae74ce9 into main Jul 16, 2025
4 checks passed
@maxcountryman maxcountryman deleted the feat/arc-queue branch July 16, 2025 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants