diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
deleted file mode 100644
index 3475f51..0000000
--- a/.github/FUNDING.yml
+++ /dev/null
@@ -1,4 +0,0 @@
-# These are supported funding model platforms
-
-github: [ayrat555, pxp9]
-custom: ["https://paypal.me/AyratBadykov", "https://paypal.me/PMR9"]
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml
deleted file mode 100644
index 80cb4a8..0000000
--- a/.github/workflows/docs.yml
+++ /dev/null
@@ -1,23 +0,0 @@
-name: docs
-
-on:
- workflow_dispatch:
-
-jobs:
- build:
- name: Deploy to gh-pages
- runs-on: ubuntu-latest
- steps:
- - uses: actions/checkout@v3
- with:
- ref: 'master'
- submodules: 'recursive'
- - run: |
- git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
- - run: |
- git config --global user.email "ayratin555@gmail.com"
- git config --global user.name "Ayrat Badykov"
-
- - name: Deploy docs
- run: |
- source ./docs/deploy.sh && build && deploy
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 4d200f4..f5d3031 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -19,7 +19,7 @@ jobs:
image: postgres
# Provide the password for postgres
env:
- POSTGRES_PASSWORD: postgres
+ POSTGRES_PASSWORD: password
POSTGRES_USER: postgres
# Set health checks to wait until postgres has started
@@ -60,9 +60,11 @@ jobs:
- name: Run tests
uses: actions-rs/cargo@v1
+ env:
+ DATABASE_URL: postgres://postgres:password@localhost:5432
with:
command: test
- args: --verbose --all-features
+ args: --verbose -- --test-threads 1
- name: Run dirty tests
uses: actions-rs/cargo@v1
diff --git a/Cargo.toml b/Cargo.toml
index 8630409..e3d707f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,11 +24,12 @@ serde_derive = "1.0"
serde_json = "1.0"
sha2 = "0.10"
thiserror = "1.0"
-typed-builder = "0.12"
+typed-builder = "0.13"
typetag = "0.2"
uuid = { version = "1.1", features = ["v4", "serde"] }
async-trait = "0.1"
async-recursion = "1"
+futures = "0.3"
diesel = { version = "2.0", features = ["postgres", "serde_json", "chrono", "uuid"] }
diesel-derive-newtype = "2.0.0-rc.0"
diesel-async = { version = "0.2", features = ["postgres", "bb8"] }
diff --git a/Makefile b/Makefile
index f955856..df74c47 100644
--- a/Makefile
+++ b/Makefile
@@ -1,20 +1,25 @@
-db:
- docker run --rm -d --name postgres -p 5432:5432 \
- -e POSTGRES_DB=fang \
- -e POSTGRES_USER=postgres \
- -e POSTGRES_PASSWORD=postgres \
- postgres:latest
-clippy:
- cargo clippy --all-features
-diesel:
- DATABASE_URL=postgres://postgres:postgres@localhost/fang diesel migration run
-stop:
- docker kill postgres
-tests:
- DATABASE_URL=postgres://postgres:postgres@localhost/fang cargo test --all-features -- --color always --nocapture
+PHONY: db, clippy, diesel, stop, tests, ignored, doc
-ignored:
- DATABASE_URL=postgres://postgres:postgres@localhost/fang cargo test --all-features -- --color always --nocapture --ignored
+DATABASE_URL := postgres://postgres:password@localhost/backie
+
+db:
+ docker run --rm -d --name backie-db -p 5432:5432 \
+ -e POSTGRES_DB=backie \
+ -e POSTGRES_USER=postgres \
+ -e POSTGRES_PASSWORD=password \
+ postgres:latest
+
+clippy:
+ cargo clippy --tests -- -D clippy::all
+
+diesel:
+ DATABASE_URL=$(DATABASE_URL) diesel migration run
+
+stop:
+ docker kill backie-db
+
+tests:
+ DATABASE_URL=$(DATABASE_URL) cargo test --all-features -- --color always --nocapture --test-threads 1
doc:
cargo doc --open
diff --git a/docs/config.toml b/docs/config.toml
deleted file mode 100644
index 2075b46..0000000
--- a/docs/config.toml
+++ /dev/null
@@ -1,112 +0,0 @@
-# The URL the site will be built for
-base_url = "https://fang.badykov.com"
-title = "Fang"
-description = "Fang is a background task processing for Rust. It uses Postgres DB as a task queue."
-
-# The site theme to use.
-theme = "adidoks"
-
-# The default language; used in feeds and search index
-# Note: the search index doesn't support Chinese/Japanese/Korean Languages
-default_language = "en"
-
-# Whether to automatically compile all Sass files in the sass directory
-compile_sass = true
-
-# Whether to generate a feed file for the site
-generate_feed = true
-
-# When set to "true", the generated HTML files are minified.
-minify_html = false
-
-# The taxonomies to be rendered for the site and their configuration.
-taxonomies = [
- {name = "authors"}, # Basic definition: no feed or pagination
-]
-
-# Whether to build a search index to be used later on by a JavaScript library
-# When set to "true", a search index is built from the pages and section
-# content for `default_language`.
-build_search_index = false
-
-[search]
-# Whether to include the title of the page/section in the index
-include_title = true
-# Whether to include the description of the page/section in the index
-include_description = false
-# Whether to include the rendered content of the page/section in the index
-include_content = true
-
-[markdown]
-# Whether to do syntax highlighting.
-# Theme can be customised by setting the `highlight_theme`
-# variable to a theme supported by Zola
-highlight_code = true
-
-[extra]
-# Put all your custom variables here
-author = "Ayrat Badykov, Pepe Márquez"
-github = "https://github.com/ayrat555/fang"
-
-# If running on netlify.app site, set to true
-is_netlify = true
-
-# Set HTML file language
-language_code = "en-US"
-
-# Set theme-color meta tag for Chrome browser
-theme_color = "#fff"
-
-# More about site's title
-title_separator = "|" # set as |, -, _, etc
-title_addition = "Background processing"
-
-
-# Set date format in blog publish metadata
-timeformat = "%B %e, %Y" # e.g. June 14, 2021
-timezone = "America/New_York"
-
-# Edit page on reposity or not
-edit_page = false
-docs_repo = "https://github.com/ayrat555/fang"
-repo_branch = "master"
-
-## Math settings
-# options: true, false. Enable math support globally,
-# default: false. You can always enable math on a per page.
-math = false
-library = "katex" # options: "katex", "mathjax". default is "katex".
-
-## Open Graph + Twitter Cards
-[extra.open]
-enable = true
-# this image will be used as fallback if a page has no image of its own
-image = "logo.png"
-twitter_site = ""
-twitter_creator = ""
-facebook_author = ""
-facebook_publisher = ""
-og_locale = "en_US"
-
-## JSON-LD
-[extra.schema]
-type = "FANG"
-logo = "logo.png"
-twitter = ""
-linked_in = ""
-github = "https://github.com/ayrat555/fang"
-section = "blog" # see config.extra.main~url
-## Sitelinks Search Box
-site_links_search_box = false
-
-
-[[extra.menu.social]]
-name = "GitHub"
-pre = ''
-url = "https://github.com/ayrat555/fang"
-post = "v0.1.0"
-weight = 20
-
-# Footer contents
-[extra.footer]
-info = 'Built with ❤️ by Ayrat Badykov and Pepe Márquez'
diff --git a/docs/content/_index.md b/docs/content/_index.md
deleted file mode 100644
index 9300752..0000000
--- a/docs/content/_index.md
+++ /dev/null
@@ -1,58 +0,0 @@
-+++
-title = "FANG"
-
-
-# The homepage contents
-[extra]
-lead = 'Fang is a background task processing for Rust. It uses Postgres DB as a task queue.'
-
-url = "/docs/readme"
-
-url_button = "Get started"
-repo_version = "GitHub v0.10.2"
-repo_license = "Open-source MIT License."
-repo_url = "https://github.com/ayrat555/fang"
-
-# Menu items
-[[extra.menu.main]]
-name = "README"
-section = "docs"
-url = "/docs/readme"
-weight = 10
-
-[[extra.menu.main]]
-name = "CHANGELOG"
-section = "docs"
-url = "/docs/changelog"
-
-[[extra.menu.main]]
-name = "Blog"
-section = "blog"
-url = "/blog/"
-weight = 20
-
-[[extra.list]]
-title = "Async and threaded workers"
-content = 'Workers can be started in threads (threaded workers) or tokio tasks (async workers)'
-
-[[extra.list]]
-title = "Scheduled tasks"
-content = 'Tasks can be scheduled at any time in the future'
-
-[[extra.list]]
-title = "Periodic (CRON) tasks"
-content = 'Tasks can be scheduled using cron expressions'
-
-[[extra.list]]
-title = "Unique tasks"
-content = 'Tasks are not duplicated in the queue if they are unique'
-
-[[extra.list]]
-title = "Single-purpose workers"
-content = 'Tasks are stored in a single table but workers can execute only tasks of the specific type'
-+++
-
-[[extra.list]]
-title = "Retries"
-content = 'Tasks can be retried with a custom backoff mode'
-+++
diff --git a/docs/content/authors/_index.md b/docs/content/authors/_index.md
deleted file mode 100644
index fa3abd1..0000000
--- a/docs/content/authors/_index.md
+++ /dev/null
@@ -1,15 +0,0 @@
-+++
-title = "Authors"
-description = "The authors of the blog articles."
-date = 2022-09-06T08:00:00.00Z
-updated = 2022-09-06T08:00:00.00Z
-draft = false
-
-# Authors
-
-[extra.author_pages]
-"ayrat-badykov" = "authors/ayrat-badykov.md"
-"pepe-marquez" = "authors/pepe-marquez.md"
-+++
-
-The authors of the blog articles.
diff --git a/docs/content/authors/ayrat-badykov.md b/docs/content/authors/ayrat-badykov.md
deleted file mode 100644
index 213948c..0000000
--- a/docs/content/authors/ayrat-badykov.md
+++ /dev/null
@@ -1,11 +0,0 @@
-+++
-title = "Ayrat Badykov"
-description = "Creator of Fang."
-date = 2021-04-01T08:50:45.00Z
-updated = 2021-04-01T08:50:45.00Z
-draft = false
-+++
-
-Co-creator of **Fang**
-
-[@ayrat555](https://github.com/ayrat555)
diff --git a/docs/content/authors/pepe-marquez.md b/docs/content/authors/pepe-marquez.md
deleted file mode 100644
index 8120b32..0000000
--- a/docs/content/authors/pepe-marquez.md
+++ /dev/null
@@ -1,11 +0,0 @@
-+++
-title = "Pepe Márquez Romero"
-description = "Co-Creator of Fang."
-date = 2021-04-01T08:50:45.00Z
-updated = 2021-04-01T08:50:45.00Z
-draft = false
-+++
-
-Co-creator of **Fang**.
-
-[@pxp9](https://github.com/pxp9)
diff --git a/docs/content/blog/2022-08-06-async-processing.md b/docs/content/blog/2022-08-06-async-processing.md
deleted file mode 100644
index 97e5572..0000000
--- a/docs/content/blog/2022-08-06-async-processing.md
+++ /dev/null
@@ -1,189 +0,0 @@
-+++
-title = "Fang, async background processing for Rust"
-description = "Async background processing for rust with tokio and postgres"
-date = 2022-08-06T08:00:00+00:00
-updated = 2022-08-06T08:00:00+00:00
-template = "blog/page.html"
-sort_by = "weight"
-weight = 1
-draft = false
-
-[taxonomies]
-authors = ["Ayrat Badykov", "Pepe Márquez"]
-
-[extra]
-lead = 'Async background processing for rust with tokio and postgres'
-images = []
-+++
-
-Even though the first stable version of Rust was released in 2015, there are still some holes in its ecosystem for solving common tasks. One of which is background processing.
-
-In software engineering background processing is a common approach for solving several problems:
-
-- Carry out periodic tasks. For example, deliver notifications, update cached values.
-- Defer expensive work so your application stays responsive while performing calculations in the background
-
-Most programming languages have go-to background processing frameworks/libraries. For example:
-
-- Ruby - [sidekiq](https://github.com/mperham/sidekiq). It uses Redis as a job queue.
-- Python - [dramatiq](https://github.com/Bogdanp/dramatiq). It uses RabbitMQ as a job queue.
-- Elixir - [oban](https://github.com/sorentwo/oban). It uses a Postgres DB as a job queue.
-
-The async programming (async/await) can be used for background processing but it has several major disadvantages if used directly:
-
-- It doesn't give control of the number of tasks that are being executed at any given time. So a lot of spawned tasks can overload a thread/threads that they're started on.
-- It doesn't provide any monitoring which can be useful to investigate your system and find bottlenecks
-- Tasks are not persistent. So all enqueued tasks are lost on every application restart
-
-To solve these shortcomings of the async programming we implemented the async processing in [the fang library](https://github.com/ayrat555/fang).
-
-## Threaded Fang
-
-Fang is a background processing library for rust. The first version of Fang was released exactly one year ago. Its key features were:
-
-- Each worker is started in a separate thread
-- A Postgres table is used as the task queue
-
-This implementation was written for a specific use case - [el monitorro bot](https://github.com/ayrat555/el_monitorro). This specific implementation of background processing was proved by time. Each day it processes more and more feeds every minute (the current number is more than 3000). Some users host the bot on their infrastructure.
-
-You can find out more about the threaded processing in fang in [this blog post](https://www.badykov.com/rust/fang/).
-
-## Async Fang
-
-
-
-Async provides significantly reduced CPU and memory overhead, especially for workloads with a large amount of IO-bound tasks, such as servers and databases. All else equal, you can have orders of magnitude more tasks than OS threads, because an async runtime uses a small amount of (expensive) threads to handle a large amount of (cheap) tasks
-
-
-
-
-For some lightweight background tasks, it's cheaper to run them on the same thread using async instead of starting one thread per worker. That's why we implemented this kind of processing in fang. Its key features:
-
-- Each worker is started as a tokio task
-- If any worker fails during task execution, it's restarted
-- Tasks are saved to a Postgres database. Instead of diesel, [tokio-postgres](https://github.com/sfackler/rust-postgres) is used to interact with a db. The threaded processing uses the [diesel](https://github.com/diesel-rs/diesel) ORM which blocks the thread.
-- The implementation is based on traits so it's easy to implement additional backends (redis, in-memory) to store tasks.
-
-## Usage
-
-The usage is straightforward:
-
-1. Define a serializable task by adding `serde` derives to a task struct.
-2. Implement `AsyncRunnable` runnable trait for fang to be able to run it.
-3. Start workers.
-4. Enqueue tasks.
-
-Let's go over each step.
-
-### Define a job
-
-```rust
-use fang::serde::{Deserialize, Serialize};
-
-#[derive(Serialize, Deserialize)]
-#[serde(crate = "fang::serde")]
-pub struct MyTask {
- pub number: u16,
-}
-
-impl MyTask {
- pub fn new(number: u16) -> Self {
- Self { number }
- }
-}
-```
-
-Fang re-exports `serde` so it's not required to add it to the `Cargo.toml` file
-
-
-### Implement the AsyncRunnable trait
-
-```rust
-use fang::async_trait;
-use fang::typetag;
-use fang::AsyncRunnable;
-use std::time::Duration;
-
-#[async_trait]
-#[typetag::serde]
-impl AsyncRunnable for MyTask {
- async fn run(&self, queue: &mut dyn AsyncQueueable) -> Result<(), Error> {
- let new_task = MyTask::new(self.number + 1);
- queue
- .insert_task(&new_task as &dyn AsyncRunnable)
- .await
- .unwrap();
-
- log::info!("the current number is {}", self.number);
- tokio::time::sleep(Duration::from_secs(3)).await;
-
- Ok(())
- }
-}
-
-```
-
-- Fang uses the [typetag library](https://github.com/dtolnay/typetag) to serialize trait objects and save them to the queue.
-- The [async-trait](https://github.com/dtolnay/async-trait) is used for implementing async traits
-
-### Init queue
-
-```rust
-use fang::asynk::async_queue::AsyncQueue;
-
-let max_pool_size: u32 = 2;
-let mut queue = AsyncQueue::builder()
- .uri("postgres://postgres:postgres@localhost/fang")
- .max_pool_size(max_pool_size)
- .duplicated_tasks(true)
- .build();
-```
-
-
-### Start workers
-
-```rust
-use fang::asynk::async_worker_pool::AsyncWorkerPool;
-use fang::NoTls;
-
-let mut pool: AsyncWorkerPool> = AsyncWorkerPool::builder()
- .number_of_workers(10_u32)
- .queue(queue.clone())
- .build();
-
-pool.start().await;
-```
-
-### Insert tasks
-
-```rust
-let task = MyTask::new(0);
-
-queue
- .insert_task(&task1 as &dyn AsyncRunnable)
- .await
- .unwrap();
-```
-
-## Pitfalls
-
-The async processing is suitable for lightweight tasks. But for heavier tasks it's advised to use one of the following approaches:
-
-- start a separate tokio runtime to run fang workers
-- use the threaded processing feature implemented in fang instead of the async processing
-
-## Future directions
-
-There are a couple of features planned for fang:
-
-- Retries with different backoff modes
-- Additional backends (in-memory, redis)
-- Graceful shutdown for async workers (for the threaded processing this feature is implemented)
-- Cron jobs
-
-## Conclusion
-
-The project is available on [GitHub](https://github.com/ayrat555/fang)
-
-The async feature and this post is written in collaboration between [Ayrat Badykov](https://www.badykov.com/) ([github](https://github.com/ayrat555)) and [Pepe Márquez Romero](https://pxp9.github.io/) ([github](https://github.com/pxp9))
-
diff --git a/docs/content/blog/2022-09-08-fang-09-release.md b/docs/content/blog/2022-09-08-fang-09-release.md
deleted file mode 100644
index 023e199..0000000
--- a/docs/content/blog/2022-09-08-fang-09-release.md
+++ /dev/null
@@ -1,163 +0,0 @@
-+++
-title = "Fang 0.9"
-description = "What's new with the release of fang 0.9"
-date = 2022-09-09T16:45:22+00:00
-updated = 2022-09-09T16:45:22+00:00
-template = "blog/page.html"
-sort_by = "weight"
-weight = 1
-draft = false
-
-[taxonomies]
-authors = ["Pepe Márquez", "Ayrat Badykov"]
-
-[extra]
-lead = "What's new with the release of fang 0.9"
-images = []
-+++
-
-## Major changes
-
-- Simplify the database schema
-- Improve the way tasks are scheduled
-- Add CRON tasks support to both modules (asynk and blocking)
-- Update the diesel crate to 2.0 (used only by blocking module)
-- Major refactoring of the blocking module
-
-### Simplify the DB schema
-
-We got rid of the periodic tasks table. Now periodic, scheduled and one-time tasks are stored in the same table (`fang_tasks`).
-
-We added two new fields to the `fang_tasks` table
-
-- `scheduled_at` - based on this table tasks are scheduled. Workers fetch tasks with `scheduled_at` <= `current_time`
-- `uniq_hash` - hash calculated from the JSON metadata of the task. Based on this field tasks are deduplicated.
-
-So changed schema is looking like this:
-
-```sql
-CREATE TABLE fang_tasks (
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
- metadata jsonb NOT NULL,
- error_message TEXT,
- state fang_task_state DEFAULT 'new' NOT NULL,
- task_type VARCHAR DEFAULT 'common' NOT NULL,
- uniq_hash CHAR(64),
- scheduled_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
- created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
- updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
-);
-```
-
-### Schedule tasks
-
-Let's examine how periodic tasks can be created with fang 0.9.
-
-The only method that should be defined is the `cron` method in the `Runnable`(blocking)/`AsyncRunnable`(asynk) trait implementation.
-
-Let's take a look at an example:
-
-```rust
-impl AsyncRunnable for MyCronTask {
- async fn run(&self, _queue: &mut dyn AsyncQueueable) -> Result<(), Error> {
- log::info!("CRON!!!!!!!!!!!!!!!",);
-
- Ok(())
- }
-
- // you must use fang::Scheduled enum.
- fn cron(&self) -> Option {
- // cron expression to execute a task every 20 seconds.
- let expression = "0/20 * * * * * *";
- Some(Scheduled::CronPattern(expression.to_string()))
- }
-
- fn uniq(&self) -> bool {
- true
- }
-}
-```
-
-Also, it is possible to schedule a task only once.
-
-```rust
-impl AsyncRunnable for MyCronTask {
- async fn run(&self, _queue: &mut dyn AsyncQueueable) -> Result<(), Error> {
- log::info!("CRON!!!!!!!!!!!!!!!",);
-
- Ok(())
- }
-
- // you must use fang::Scheduled enum.
- fn cron(&self) -> Option {
- // You must use DateTime to specify
- // when in the future you would like schedule the task.
-
- // This will schedule the task for within 7 seconds.
- Some(Scheduled::ScheduleOnce(Utc::now() + Duration::seconds(7i64)))
- }
-
- fn uniq(&self) -> bool {
- true
- }
-}
-```
-
-More examples are available at [fang examples](https://github.com/ayrat555/fang/tree/master/fang_examples)
-
-It is no longer needed to start the scheduler process, the scheduled tasks will be executed by `WorkerPool` or `AsyncWorkerPool`. If a task is periodic, it will be re-scheduled before its next execution.
-
-### Blocking refactor
-
-- We deleted the graceful shutdown feature of the blocking module. But we're planning to re-implement it in the future.
-
-- We completely changed most of the blocking module's API.
-
-The reason for this change is to unify the APIs of the blocking and the asynk modules. So users can easily switch between blocking and async workers.
-
-Another reason is we wanted to do a trait for the task queue in the blocking module. It opens a possibility to implement new backends for the blocking module.
-
-A new API of the blocking queues looks like this:
-
-```rust
-pub trait Queueable {
- fn fetch_and_touch_task(&self, task_type: String) -> Result