No description
Find a file
2023-03-12 18:38:54 +01:00
.github Fix CI for Postgres tests 2023-03-11 18:29:33 +01:00
examples/simple_worker Allow customization of the pulling interval per queue 2023-03-12 17:15:40 +01:00
migrations Make queues configurable 2023-03-11 16:38:32 +01:00
src Handle tasks that panic 2023-03-12 18:33:00 +01:00
.env Revamp project using newtype and rework tasks table 2023-03-07 16:41:20 +01:00
.gitattributes Update gitattributes 2023-03-11 22:27:30 +01:00
.gitignore Make possible to provide app state to tasks 2023-03-10 23:41:34 +01:00
Cargo.toml Release 0.2.0 2023-03-12 18:38:54 +01:00
diesel.toml Support only async 2023-03-04 19:07:17 +01:00
LICENCE Update readme 2023-03-11 22:22:25 +01:00
Makefile Supports graceful shutdown 2023-03-09 16:59:45 +01:00
README.md Allow customization of the pulling interval per queue 2023-03-12 17:15:40 +01:00

Backie 🚲

Async persistent background task processing for Rust applications with Tokio. Queue asynchronous tasks to be processed by workers. It's designed to be easy to use and horizontally scalable. It uses Postgres as a storage backend and can also be extended to support other types of storage.

High-level overview of how Backie works:

  • Client puts tasks on a queue
  • Server starts a multiple workers per queue
  • Worker pulls tasks off the queue and starts processing them
  • Tasks are processed concurrently by multiple workers

Backie started as a fork of fang crate, but quickly diverged significantly in its implementation.

Key features

Here are some of the Backie's key features:

  • Guaranteed execution: at least one execution of a task
  • Async workers: Workers are started as Tokio tasks
  • Application context: Tasks can access an shared user-provided application context
  • Single-purpose workers: Tasks are stored together but workers are configured to execute only tasks of a specific queue
  • Retries: Tasks are retried with a custom backoff mode
  • Graceful shutdown: provide a future to gracefully shutdown the workers, on-the-fly tasks are not interrupted
  • Recovery of unfinished tasks: Tasks that were not finished are retried on the next worker start
  • Unique tasks: Tasks are not duplicated in the queue if they provide a unique hash

Other planned 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

Installation

  1. Add this to your Cargo.toml
[dependencies]
backie = "0.1"

If you are not already using, you will also want to include the following dependencies for defining your tasks:

[dependencies]
async-trait = "0.1"
anyhow = "1"
serde = { version = "1.0", features = ["derive"] }
diesel = { version = "2.0", features = ["postgres", "serde_json", "chrono", "uuid"] }
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+

  1. Create the backie_tasks table in the Postgres database. The migration can be found in the migrations directory.

Usage

The [BackgroundTask] trait is used to define a task. You must implement this trait for all tasks you want to execute.

One important thing to note is the use of the attribute [BackgroundTask::TASK_NAME] which must be unique for the whole application. This attribute is critical for reconstructing the task back from the database.

The [BackgroundTask::AppData] can be used to argument the task with your application specific contextual information. This is useful for example to pass a database connection pool to the task or other application configuration.

The [BackgroundTask::run] method is where you define the behaviour of your background task execution. This method will be called by the task queue workers.

use async_trait::async_trait;
use backie::{BackgroundTask, CurrentTask};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct MyTask {
    info: String,
}

#[async_trait]
impl BackgroundTask for MyTask {
    const TASK_NAME: &'static str = "my_task_unique_name";
    type AppData = ();

    async fn run(&self, task: CurrentTask, context: Self::AppData) -> Result<(), anyhow::Error> {
        // Do something
        Ok(())
    }
}

Starting workers

First, we need to create a [TaskStore] trait instance. This is the object responsible for storing and retrieving tasks from a database. Backie currently only supports Postgres as a storage backend via the provided [PgTaskStore]. You can implement other storage backends by implementing the [TaskStore] trait.

Then, we can use the task_store to start a worker pool using the [WorkerPool]. The [WorkerPool] is responsible for starting the workers and managing their lifecycle.

A full example of starting a worker pool can be found in the examples directory.

Queueing tasks

After stating the workers, we get an instance of [Queue] which we can use to enqueue tasks. It is also possible to directly create a [Queue] instance from with a [TaskStore] instance.

This will enqueue the task and whenever a worker is available it will start processing. Workers don't need to be 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.

Contributing

  1. Fork it!
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

I would like to thank the authors of the Fang and background_job crates which were the main inspiration for this project.