Background job processing library for Rust.
Currently, it uses Postgres to store state. But in the future, more backends will be supported.
toml
[dependencies]
fang = "0.2"
typetag = "0.1"
serde = { version = "1.0", features = ["derive"] }
fang_tasks
table in the Postgres database. The migration can be found in the migrations directory.Every job should implement fang::Runnable
trait which is used by fang
to execute it.
```rust use fang::Error; use fang::Runnable; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Job {
pub number: u16,
}
#[typetag::serde]
impl Runnable for Job {
fn run(&self) -> Result<(), Error> {
println!("the number is {}", self.number);
Ok(())
}
}
```
As you can see from the example above, the trait implementation has #[typetag::serde]
attribute which is used to deserialize the job.
To enqueue a job use Postgres::enqueue_task
```rust use fang::Postgres;
...
Postgres::enqueue_task(&Job { number: 10 }).unwrap();
```
Every worker runs in a separate thread. In case of panic, they are always restarted.
Use WorkerPool
to start workers. It accepts two parameters - the number of workers and the prefix for the worker thread name.
```rust use fang::WorkerPool;
WorkerPool::new(10, "sync".to_string()).start(); ```
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Ayrat Badykov (@ayrat555)