Crates.io | archimedes |
lib.rs | archimedes |
version | 0.4.0 |
source | src |
created_at | 2022-12-19 20:59:38.286625 |
updated_at | 2024-01-01 16:09:15.322516 |
description | High performance Rust/PostgreSQL job queue (also suitable for getting jobs generated by PostgreSQL triggers/functions out into a different work queue) |
homepage | https://docs.rs/archimedes |
repository | https://github.com/leo91000/archimedes |
max_upload_size | |
id | 741600 |
size | 140,829 |
NOT PRODUCTION READY
Rewrite of Graphile Worker in Rust. If you like this library go sponsor Benjie project, all research has been done by him, this library is only a rewrite in Rust 🦀.
The port should mostly be compatible with graphile-worker
(meaning you can run it side by side with Node.JS).
The following differs from Graphile Worker
:
Graphile Worker
, each process has it's worker_id. In rust there is only one worker_id, then jobs are processed in your async runtime thread.Job queue for PostgreSQL running on Rust - allows you to run jobs (e.g. sending emails, performing calculations, generating PDFs, etc) "in the background" so that your HTTP response/application code is not held up. Can be used with any PostgreSQL-backed application.
cargo add archimedes
The definition of a task consist simply of an async function and a task identifier
use serde::{Deserialize, Serialize};
use archimedes::{task, WorkerContext};
#[derive(Deserialize, Serialize)]
struct HelloPayload {
name: String,
}
#[task]
async fn say_hello(payload: HelloPayload, _ctx: WorkerContext) -> Result<(), ()> {
println!("Hello {} !", payload.name);
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), ()> {
archimedes::WorkerOptions::default()
.concurrency(2)
.schema("example_simple_worker")
.define_job(say_hello)
.pg_pool(pg_pool)
.init()
.await?
.run()
.await?;
Ok(())
}
Connect to your database and run the following SQL:
SELECT archimedes_worker.add_job('say_hello', json_build_object('name', 'Bobby Tables'));
#[tokio::main]
async fn main() -> Result<(), ()> {
// ...
let helpers = worker.create_helpers();
// Using add_job forces the payload to be same struct defined in our type
helpers.add_job::<say_hello>(
HelloPayload { name: "world".to_string() },
Default::default(),
).await.unwrap();
// You can also use `add_raw_job` if you don't have access to the task, or don't care about end 2 end safety
helpers.add_raw_job("say_hello", serde_json::json!({ "message": "world" }), Default::default()).await.unwrap();
Ok(())
}
You should see the worker output Hello Bobby Tables !
. Gosh, that was fast!
LISTEN
/NOTIFY
to be informed of jobs as they're inserted)SKIP LOCKED
to find jobs to execute, resulting in
faster fetches)job_key
NOT production ready (use it at your own risk).
PostgreSQL 12+ Might work with older versions, but has not been tested.
Note: Postgres 12 is required for the generated always as (expression)
feature
cargo add archimedes
archimedes
manages its own database schema (archimedes_worker
). Just
point at your database and we handle our own migrations.