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:
Ayrat Badykov 2021-07-11 10:26:20 +03:00 committed by GitHub
parent 36e705a7ef
commit 4f50385c96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 22 deletions

View file

@ -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)
- Execute different types of tasks in separate workers - [#1](https://github.com/ayrat555/fang/pull/1)

View file

@ -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.
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
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
[dependencies]
fang = "0.3"
typetag = "0.1"
serde = { version = "1.0", features = ["derive"] }
fang = "0.4"
```
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
use fang::Error;
use fang::Runnable;
use serde::{Deserialize, Serialize};
use fang::Error;
use fang::Runnable;
use fang::{Deserialize, Serialize};
use fang::typetag;
#[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(())
}
#[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.
@ -117,7 +118,7 @@ impl Runnable for Job {
Set `task_type` to the `WorkerParamas`:
```
```rust
let mut worker_params = WorkerParams::new();
worker_params.set_task_type("number".to_string());

View file

@ -186,8 +186,9 @@ mod executor_tests {
use crate::postgres::NewTask;
use crate::postgres::Postgres;
use crate::schema::FangTaskState;
use crate::typetag;
use crate::{Deserialize, Serialize};
use diesel::connection::Connection;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct ExecutorJobTest {

View file

@ -13,3 +13,8 @@ pub mod worker_pool;
pub use executor::*;
pub use postgres::*;
pub use worker_pool::*;
#[doc(hidden)]
pub use serde::{Deserialize, Serialize};
#[doc(hidden)]
pub use typetag;

View file

@ -177,11 +177,12 @@ mod postgres_tests {
use crate::executor::Runnable;
use crate::schema::fang_tasks;
use crate::schema::FangTaskState;
use crate::typetag;
use crate::{Deserialize, Serialize};
use chrono::{DateTime, Duration, Utc};
use diesel::connection::Connection;
use diesel::prelude::*;
use diesel::result::Error;
use serde::{Deserialize, Serialize};
#[test]
fn insert_inserts_task() {

View file

@ -136,9 +136,10 @@ mod job_pool_tests {
use crate::postgres::Postgres;
use crate::postgres::Task;
use crate::schema::fang_tasks;
use crate::typetag;
use crate::{Deserialize, Serialize};
use diesel::pg::PgConnection;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use std::thread;
use std::time::Duration;