add README
This commit is contained in:
parent
75a0de365e
commit
917c65204b
3 changed files with 115 additions and 15 deletions
97
README.md
Normal file
97
README.md
Normal file
|
@ -0,0 +1,97 @@
|
|||
<p align="center"><img src="logo.png" alt="fang" height="300px"></p>
|
||||
|
||||
# Fang
|
||||
|
||||
Background job processing library for Rust.
|
||||
|
||||
Currently, it uses postgres to store state. But in the future, more backends will be supported.
|
||||
|
||||
## Installation
|
||||
|
||||
1. Add this to your Cargo.toml
|
||||
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
fang = "0.1"
|
||||
typetag = "0.1"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
```
|
||||
|
||||
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).
|
||||
|
||||
## Usage
|
||||
|
||||
### Defining a job
|
||||
|
||||
Every job should implement `fang::Runnable` trait which is used by `fang` to execut 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, trait implementation has `#[typetag::serde]` which is used to deserialize the job.
|
||||
|
||||
### Enqueuing a job
|
||||
|
||||
To enqueue a job use `Postgres::enqueue_task`
|
||||
|
||||
|
||||
```rust
|
||||
use fang::Postgres;
|
||||
|
||||
...
|
||||
|
||||
Postgres::enqueue_task(&Job { number: 10 }).unwrap();
|
||||
|
||||
```
|
||||
|
||||
|
||||
### Starting workers
|
||||
|
||||
Every worker is executed in a separate thread. In case of panic, they are always restarted.
|
||||
|
||||
Use `WorkerPool::new` to start workers. It accepts two parameters - the number of workers and the prefix for worker thread name.
|
||||
|
||||
|
||||
```rust
|
||||
use fang::WorkerPool;
|
||||
|
||||
WorkerPool::new(10, "sync".to_string()).start();
|
||||
```
|
||||
|
||||
## Potential/future features
|
||||
|
||||
* Extendable/new backends
|
||||
* Workers for specific types of tasks. Currently, each workers execute all types of tasks
|
||||
* Configurable db records retention. Currently, fang doesn't remove tasks from the db.
|
||||
|
||||
## 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)
|
BIN
logo.png
Normal file
BIN
logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 376 KiB |
|
@ -27,26 +27,14 @@ pub struct NewTask {
|
|||
}
|
||||
|
||||
pub struct Postgres {
|
||||
pub database_url: String,
|
||||
pub connection: PgConnection,
|
||||
}
|
||||
|
||||
impl Postgres {
|
||||
pub fn new(database_url: Option<String>) -> Self {
|
||||
dotenv().ok();
|
||||
let connection = Self::pg_connection(database_url);
|
||||
|
||||
let url = match database_url {
|
||||
Some(string_url) => string_url,
|
||||
None => env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
|
||||
};
|
||||
|
||||
let connection =
|
||||
PgConnection::establish(&url).unwrap_or_else(|_| panic!("Error connecting to {}", url));
|
||||
|
||||
Self {
|
||||
connection,
|
||||
database_url: url,
|
||||
}
|
||||
Self { connection }
|
||||
}
|
||||
|
||||
pub fn push_task(&self, job: &dyn Runnable) -> Result<Task, Error> {
|
||||
|
@ -57,6 +45,10 @@ impl Postgres {
|
|||
self.insert(&new_task)
|
||||
}
|
||||
|
||||
pub fn enqueue_task(job: &dyn Runnable) -> Result<Task, Error> {
|
||||
Self::new(None).push_task(job)
|
||||
}
|
||||
|
||||
pub fn insert(&self, params: &NewTask) -> Result<Task, Error> {
|
||||
diesel::insert_into(fang_tasks::table)
|
||||
.values(params)
|
||||
|
@ -130,9 +122,20 @@ impl Postgres {
|
|||
.get_result::<Task>(&self.connection)
|
||||
}
|
||||
|
||||
pub fn current_time() -> DateTime<Utc> {
|
||||
fn current_time() -> DateTime<Utc> {
|
||||
Utc::now()
|
||||
}
|
||||
|
||||
fn pg_connection(database_url: Option<String>) -> PgConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let url = match database_url {
|
||||
Some(string_url) => string_url,
|
||||
None => env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
|
||||
};
|
||||
|
||||
PgConnection::establish(&url).unwrap_or_else(|_| panic!("Error connecting to {}", url))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue