Make TaskStore trait object safe
This commit is contained in:
parent
c07781a79b
commit
c99486eaa6
5 changed files with 32 additions and 10 deletions
16
README.md
16
README.md
|
@ -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
|
- 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
|
- 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
|
## Installation
|
||||||
|
|
||||||
1. Add this to your `Cargo.toml`
|
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
|
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.
|
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).
|
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
|
## 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
|
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.
|
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
|
## Contributing
|
||||||
|
|
||||||
1. [Fork it!](https://github.com/rafaelcaricio/backie/fork)
|
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`)
|
4. Push to the branch (`git push origin my-new-feature`)
|
||||||
5. Create a new Pull Request
|
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.
|
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.
|
||||||
|
|
||||||
|
|
|
@ -8,14 +8,14 @@ use std::time::Duration;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Queue<S>
|
pub struct Queue<S>
|
||||||
where
|
where
|
||||||
S: TaskStore,
|
S: TaskStore + Clone,
|
||||||
{
|
{
|
||||||
task_store: Arc<S>,
|
task_store: Arc<S>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> Queue<S>
|
impl<S> Queue<S>
|
||||||
where
|
where
|
||||||
S: TaskStore,
|
S: TaskStore + Clone,
|
||||||
{
|
{
|
||||||
pub fn new(task_store: Arc<S>) -> Self {
|
pub fn new(task_store: Arc<S>) -> Self {
|
||||||
Queue { task_store }
|
Queue { task_store }
|
||||||
|
|
14
src/store.rs
14
src/store.rs
|
@ -197,7 +197,7 @@ pub mod test_store {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait TaskStore: Clone + Send + Sync + 'static {
|
pub trait TaskStore: Send + Sync + 'static {
|
||||||
async fn pull_next_task(
|
async fn pull_next_task(
|
||||||
&self,
|
&self,
|
||||||
queue_name: &str,
|
queue_name: &str,
|
||||||
|
@ -213,3 +213,15 @@ pub trait TaskStore: Clone + Send + Sync + 'static {
|
||||||
error: &str,
|
error: &str,
|
||||||
) -> Result<Task, AsyncQueueError>;
|
) -> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ where
|
||||||
pub struct Worker<AppData, S>
|
pub struct Worker<AppData, S>
|
||||||
where
|
where
|
||||||
AppData: Clone + Send + 'static,
|
AppData: Clone + Send + 'static,
|
||||||
S: TaskStore,
|
S: TaskStore + Clone,
|
||||||
{
|
{
|
||||||
store: Arc<S>,
|
store: Arc<S>,
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ where
|
||||||
impl<AppData, S> Worker<AppData, S>
|
impl<AppData, S> Worker<AppData, S>
|
||||||
where
|
where
|
||||||
AppData: Clone + Send + 'static,
|
AppData: Clone + Send + 'static,
|
||||||
S: TaskStore,
|
S: TaskStore + Clone,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
store: Arc<S>,
|
store: Arc<S>,
|
||||||
|
|
|
@ -16,7 +16,7 @@ use tokio::task::JoinHandle;
|
||||||
pub struct WorkerPool<AppData, S>
|
pub struct WorkerPool<AppData, S>
|
||||||
where
|
where
|
||||||
AppData: Clone + Send + 'static,
|
AppData: Clone + Send + 'static,
|
||||||
S: TaskStore,
|
S: TaskStore + Clone,
|
||||||
{
|
{
|
||||||
/// Storage of tasks.
|
/// Storage of tasks.
|
||||||
task_store: Arc<S>,
|
task_store: Arc<S>,
|
||||||
|
@ -44,7 +44,7 @@ where
|
||||||
impl<AppData, S> WorkerPool<AppData, S>
|
impl<AppData, S> WorkerPool<AppData, S>
|
||||||
where
|
where
|
||||||
AppData: Clone + Send + 'static,
|
AppData: Clone + Send + 'static,
|
||||||
S: TaskStore,
|
S: TaskStore + Clone,
|
||||||
{
|
{
|
||||||
/// Create a new worker pool.
|
/// Create a new worker pool.
|
||||||
pub fn new<A>(task_store: S, application_data_fn: A) -> Self
|
pub fn new<A>(task_store: S, application_data_fn: A) -> Self
|
||||||
|
|
Loading…
Reference in a new issue