backie/README.md

100 lines
2.2 KiB
Markdown
Raw Normal View History

2021-06-24 09:58:02 +00:00
<p align="center"><img src="logo.png" alt="fang" height="300px"></p>
# Fang
Background job processing library for Rust.
2021-06-24 10:15:10 +00:00
Currently, it uses Postgres to store state. But in the future, more backends will be supported.
2021-06-24 09:58:02 +00:00
## Installation
1. Add this to your Cargo.toml
```toml
[dependencies]
2021-06-24 10:15:10 +00:00
fang = "0.2"
2021-06-24 09:58:02 +00:00
typetag = "0.1"
serde = { version = "1.0", features = ["derive"] }
```
2021-06-24 10:15:10 +00:00
2. Create `fang_tasks` table in the Postgres database. The migration can be found in [the migrations directory](https://github.com/ayrat555/fang/blob/master/migrations/2021-06-05-112912_create_fang_tasks/up.sql).
2021-06-24 09:58:02 +00:00
## Usage
### Defining a job
2021-06-24 10:15:10 +00:00
Every job should implement `fang::Runnable` trait which is used by `fang` to execute it.
2021-06-24 09:58:02 +00:00
```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(())
}
}
```
2021-06-24 10:15:10 +00:00
As you can see from the example above, the trait implementation has `#[typetag::serde]` attribute which is used to deserialize the job.
2021-06-24 09:58:02 +00:00
### Enqueuing a job
To enqueue a job use `Postgres::enqueue_task`
```rust
use fang::Postgres;
...
Postgres::enqueue_task(&Job { number: 10 }).unwrap();
```
### Starting workers
2021-06-24 10:15:10 +00:00
Every worker runs in a separate thread. In case of panic, they are always restarted.
2021-06-24 09:58:02 +00:00
2021-06-24 10:15:10 +00:00
Use `WorkerPool` to start workers. It accepts two parameters - the number of workers and the prefix for the worker thread name.
2021-06-24 09:58:02 +00:00
```rust
use fang::WorkerPool;
WorkerPool::new(10, "sync".to_string()).start();
```
## Potential/future features
* Extendable/new backends
2021-06-24 10:15:10 +00:00
* Workers for specific types of tasks. Currently, each worker execute all types of tasks
2021-06-24 09:58:02 +00:00
* Configurable db records retention. Currently, fang doesn't remove tasks from the db.
2021-06-24 10:15:10 +00:00
* Retries
* Scheduled tasks
2021-06-24 09:58:02 +00:00
## Contributing
1. [Fork it!](https://github.com/ayrat555/fang/fork)
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 new Pull Request
## Author
Ayrat Badykov (@ayrat555)