simplify usage by re-exporting serde traits (#3)
* simplify usage by re-exporting serde traits * improve readme formatting * use typetag * try different approach * reexport through macro * fix re-exporting * add CHANGELOG entry * use typetag::serde in README
This commit is contained in:
parent
36e705a7ef
commit
4f50385c96
6 changed files with 37 additions and 22 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
# Changelog
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
- Simplify usage by re-exporting serde traits - [#3](https://github.com/ayrat555/fang/pull/2)
|
||||||
|
|
||||||
## 0.3.0 (2021-07-04)
|
## 0.3.0 (2021-07-04)
|
||||||
|
|
||||||
- Execute different types of tasks in separate workers - [#1](https://github.com/ayrat555/fang/pull/1)
|
- Execute different types of tasks in separate workers - [#1](https://github.com/ayrat555/fang/pull/1)
|
||||||
|
|
39
README.md
39
README.md
|
@ -6,6 +6,8 @@ Background job processing library for Rust.
|
||||||
|
|
||||||
Currently, it uses Postgres to store state. But in the future, more backends will be supported.
|
Currently, it uses Postgres to store state. But in the future, more backends will be supported.
|
||||||
|
|
||||||
|
Note that the README follows the master branch, to see instructions for the latest published version, check [crates.io](https://crates.io/crates/fang).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
1. Add this to your Cargo.toml
|
1. Add this to your Cargo.toml
|
||||||
|
@ -13,9 +15,7 @@ Currently, it uses Postgres to store state. But in the future, more backends wil
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fang = "0.3"
|
fang = "0.4"
|
||||||
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).
|
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).
|
||||||
|
@ -28,24 +28,25 @@ Every job should implement `fang::Runnable` trait which is used by `fang` to exe
|
||||||
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use fang::Error;
|
use fang::Error;
|
||||||
use fang::Runnable;
|
use fang::Runnable;
|
||||||
use serde::{Deserialize, Serialize};
|
use fang::{Deserialize, Serialize};
|
||||||
|
use fang::typetag;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Job {
|
struct Job {
|
||||||
pub number: u16,
|
pub number: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[typetag::serde]
|
#[typetag::serde]
|
||||||
impl Runnable for Job {
|
impl Runnable for Job {
|
||||||
fn run(&self) -> Result<(), Error> {
|
fn run(&self) -> Result<(), Error> {
|
||||||
println!("the number is {}", self.number);
|
println!("the number is {}", self.number);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
As you can see from the example above, the trait implementation has `#[typetag::serde]` attribute which is used to deserialize the job.
|
As you can see from the example above, the trait implementation has `#[typetag::serde]` attribute which is used to deserialize the job.
|
||||||
|
@ -117,7 +118,7 @@ impl Runnable for Job {
|
||||||
|
|
||||||
Set `task_type` to the `WorkerParamas`:
|
Set `task_type` to the `WorkerParamas`:
|
||||||
|
|
||||||
```
|
```rust
|
||||||
let mut worker_params = WorkerParams::new();
|
let mut worker_params = WorkerParams::new();
|
||||||
worker_params.set_task_type("number".to_string());
|
worker_params.set_task_type("number".to_string());
|
||||||
|
|
||||||
|
|
|
@ -186,8 +186,9 @@ mod executor_tests {
|
||||||
use crate::postgres::NewTask;
|
use crate::postgres::NewTask;
|
||||||
use crate::postgres::Postgres;
|
use crate::postgres::Postgres;
|
||||||
use crate::schema::FangTaskState;
|
use crate::schema::FangTaskState;
|
||||||
|
use crate::typetag;
|
||||||
|
use crate::{Deserialize, Serialize};
|
||||||
use diesel::connection::Connection;
|
use diesel::connection::Connection;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct ExecutorJobTest {
|
struct ExecutorJobTest {
|
||||||
|
|
|
@ -13,3 +13,8 @@ pub mod worker_pool;
|
||||||
pub use executor::*;
|
pub use executor::*;
|
||||||
pub use postgres::*;
|
pub use postgres::*;
|
||||||
pub use worker_pool::*;
|
pub use worker_pool::*;
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use serde::{Deserialize, Serialize};
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use typetag;
|
||||||
|
|
|
@ -177,11 +177,12 @@ mod postgres_tests {
|
||||||
use crate::executor::Runnable;
|
use crate::executor::Runnable;
|
||||||
use crate::schema::fang_tasks;
|
use crate::schema::fang_tasks;
|
||||||
use crate::schema::FangTaskState;
|
use crate::schema::FangTaskState;
|
||||||
|
use crate::typetag;
|
||||||
|
use crate::{Deserialize, Serialize};
|
||||||
use chrono::{DateTime, Duration, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use diesel::connection::Connection;
|
use diesel::connection::Connection;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel::result::Error;
|
use diesel::result::Error;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_inserts_task() {
|
fn insert_inserts_task() {
|
||||||
|
|
|
@ -136,9 +136,10 @@ mod job_pool_tests {
|
||||||
use crate::postgres::Postgres;
|
use crate::postgres::Postgres;
|
||||||
use crate::postgres::Task;
|
use crate::postgres::Task;
|
||||||
use crate::schema::fang_tasks;
|
use crate::schema::fang_tasks;
|
||||||
|
use crate::typetag;
|
||||||
|
use crate::{Deserialize, Serialize};
|
||||||
use diesel::pg::PgConnection;
|
use diesel::pg::PgConnection;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue