Make TaskStore trait object safe

This commit is contained in:
Rafael Caricio 2023-03-13 13:08:54 +01:00
parent c07781a79b
commit c99486eaa6
Signed by: rafaelcaricio
GPG key ID: 3C86DBCE8E93C947
5 changed files with 32 additions and 10 deletions

View file

@ -31,6 +31,14 @@ Here are some of the Backie's key features:
- Task timeout: Tasks are retried if they are not completed in time
- Scheduling of tasks: Tasks can be scheduled to be executed at a specific time
## Safety
This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in 100% safe Rust.
## Minimum supported Rust version
Backie's MSRV is 1.68.
## Installation
1. Add this to your `Cargo.toml`
@ -54,8 +62,6 @@ diesel-async = { version = "0.2", features = ["postgres", "bb8"] }
Those dependencies are required to use the `#[async_trait]` and `#[derive(Serialize, Deserialize)]` attributes
in your task definitions and to connect to the Postgres database.
*Supports rustc 1.68+*
2. Create the `backie_tasks` table in the Postgres database. The migration can be found in [the migrations directory](https://github.com/rafaelcaricio/backie/blob/master/migrations/2023-03-06-151907_create_backie_tasks/up.sql).
## Usage
@ -114,6 +120,10 @@ This will enqueue the task and whenever a worker is available it will start proc
started before enqueuing tasks. Workers don't need to be in the same process as the queue as long as the workers have
access to the same underlying storage system. This enables horizontal scaling of the workers.
## License
This project is licensed under the [MIT license][license].
## Contributing
1. [Fork it!](https://github.com/rafaelcaricio/backie/fork)
@ -122,7 +132,7 @@ access to the same underlying storage system. This enables horizontal scaling of
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Thanks to related crates authors
## Acknowledgements
I would like to thank the authors of the [Fang](https://github.com/ayrat555/fang) and [background_job](https://git.asonix.dog/asonix/background-jobs.git) crates which were the main inspiration for this project.

View file

@ -8,14 +8,14 @@ use std::time::Duration;
#[derive(Clone)]
pub struct Queue<S>
where
S: TaskStore,
S: TaskStore + Clone,
{
task_store: Arc<S>,
}
impl<S> Queue<S>
where
S: TaskStore,
S: TaskStore + Clone,
{
pub fn new(task_store: Arc<S>) -> Self {
Queue { task_store }

View file

@ -197,7 +197,7 @@ pub mod test_store {
}
#[async_trait::async_trait]
pub trait TaskStore: Clone + Send + Sync + 'static {
pub trait TaskStore: Send + Sync + 'static {
async fn pull_next_task(
&self,
queue_name: &str,
@ -213,3 +213,15 @@ pub trait TaskStore: Clone + Send + Sync + 'static {
error: &str,
) -> Result<Task, AsyncQueueError>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::store::test_store::MemoryTaskStore;
#[test]
fn task_store_trait_is_object_safe() {
let store = MemoryTaskStore::default();
let _object = &store as &dyn TaskStore;
}
}

View file

@ -55,7 +55,7 @@ where
pub struct Worker<AppData, S>
where
AppData: Clone + Send + 'static,
S: TaskStore,
S: TaskStore + Clone,
{
store: Arc<S>,
@ -76,7 +76,7 @@ where
impl<AppData, S> Worker<AppData, S>
where
AppData: Clone + Send + 'static,
S: TaskStore,
S: TaskStore + Clone,
{
pub(crate) fn new(
store: Arc<S>,

View file

@ -16,7 +16,7 @@ use tokio::task::JoinHandle;
pub struct WorkerPool<AppData, S>
where
AppData: Clone + Send + 'static,
S: TaskStore,
S: TaskStore + Clone,
{
/// Storage of tasks.
task_store: Arc<S>,
@ -44,7 +44,7 @@ where
impl<AppData, S> WorkerPool<AppData, S>
where
AppData: Clone + Send + 'static,
S: TaskStore,
S: TaskStore + Clone,
{
/// Create a new worker pool.
pub fn new<A>(task_store: S, application_data_fn: A) -> Self