From 1aabb91206feb14b46f3c458b929c1890ed9c8a3 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Sat, 31 Jul 2021 10:40:11 +0300 Subject: [PATCH] bump version to 0.4.0 (#10) * update readme * remove note * add CHANGELOG * fix pr number --- CHANGELOG.md | 9 +++++++++ Cargo.toml | 2 +- README.md | 42 +++++++++++++++++++++++++----------------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4af31a1..c308016 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 0.4.0 (2021-07-31) + +### [#8](https://github.com/ayrat555/fang/pull/8): + +- Maintain ConnectionPool for Worker Threads +- Rename Postgres into Queue +- Pass PgConnection into run function +- Add function to remove all tasks of the specified type + ## 0.3.1 (2021-07-24) - Add periodic tasks - [#5](https://github.com/ayrat555/fang/pull/5), [#7](https://github.com/ayrat555/fang/pull/7) diff --git a/Cargo.toml b/Cargo.toml index 5bbf820..e0a89d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fang" -version = "0.3.2" +version = "0.4.0" authors = ["Ayrat Badykov "] description = "Background job processing library for Rust" repository = "https://github.com/ayrat555/fang" diff --git a/README.md b/README.md index 2727da0..9bff02b 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ Background job processing library for Rust. It uses Postgres DB as a task queue. -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 @@ -16,7 +14,8 @@ Note that the README follows the master branch, to see instructions for the late ```toml [dependencies] -fang = "0.3.2" +fang = "0.4.0" +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). @@ -27,13 +26,12 @@ fang = "0.3.2" Every job should implement `fang::Runnable` trait which is used by `fang` to execute it. - ```rust use fang::Error; use fang::Runnable; -use fang::{Deserialize, Serialize}; use fang::typetag; - +use fang::PgConnection; +use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] struct Job { @@ -42,7 +40,7 @@ struct Job { #[typetag::serde] impl Runnable for Job { - fn run(&self) -> Result<(), Error> { + fn run(&self, _connection: &PgConnection) -> Result<(), Error> { println!("the number is {}", self.number); Ok(()) @@ -52,31 +50,39 @@ impl Runnable for Job { As you can see from the example above, the trait implementation has `#[typetag::serde]` attribute which is used to deserialize the job. +The second parameter of the `run` function is diesel's PgConnection, You can re-use it to manipulate the job queue, for example, to add a new job during the current job's execution. Or you can just re-use it in your own queries if you're using diesel. If you don't need it, just ignore it. + ### Enqueuing a job -To enqueue a job use `Postgres::enqueue_task` +To enqueue a job use `Queue::enqueue_task` ```rust -use fang::Postgres; +use fang::Queue; ... -Postgres::enqueue_task(&Job { number: 10 }).unwrap(); +Queue::enqueue_task(&Job { number: 10 }).unwrap(); ``` The example above creates a new postgres connection on every call. If you want to reuse the same postgres connection to enqueue several jobs use Postgres struct instance: ```rust -let postgres = Postgres::new(); +let queue = Queue::new(); for id in &unsynced_feed_ids { - postgres.push_task(&SyncFeedJob { feed_id: *id }).unwrap(); + queue.push_task(&SyncFeedJob { feed_id: *id }).unwrap(); } ``` +Or you can use `PgConnection` struct: + +```rust +Queue::push_task_query(pg_connection, &new_job).unwrap(); +``` + ### Starting workers Every worker runs in a separate thread. In case of panic, they are always restarted. @@ -90,6 +96,9 @@ use fang::WorkerPool; WorkerPool::new(10).start(); ``` + +Checkout out a [simple example](https://github.com/ayrat555/fang/tree/master/fang_examples/simple_worker). + ### Configuration To configure workers, instead of `WorkerPool::new` which uses default values, use `WorkerPool.new_with_params`. It accepts two parameters - the number of workers and `WorkerParams` struct. @@ -190,15 +199,15 @@ Usage example: ```rust use fang::Scheduler; -use fang::Postgres; +use fang::Queue; -let postgres = Postgres::new(); +let queue = Queue::new(); -postgres +queue .push_periodic_task(&SyncJob::default(), 120) .unwrap(); -postgres +queue .push_periodic_task(&DeliverJob::default(), 60) .unwrap(); @@ -211,7 +220,6 @@ In the example above, `push_periodic_task` is used to save the specified task to - Db check period in seconds - Acceptable error limit in seconds - |current_time - scheduled_time| < error - ## Contributing 1. [Fork it!](https://github.com/ayrat555/fang/fork)