Skip to content

Commit

Permalink
chore: Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
leo91000 committed Dec 31, 2023
1 parent 422a1b2 commit ca53165
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
55 changes: 52 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ cargo add archimedes
The definition of a task consist simply of an async function and a task identifier

```rust
#[derive(Deserialize)]
use serde::{Deserialize, Serialize};
use archimedes::{task, WorkerContext};

#[derive(Deserialize, Serialize)]
struct HelloPayload {
name: String,
}

async fn say_hello(_ctx: WorkerCtx, payload: HelloPayload) -> Result<(), ..> {
#[task]
async fn say_hello(payload: HelloPayload, _ctx: WorkerContext) -> Result<(), ()> {
println!("Hello {} !", payload.name);
Ok(())
}
Expand All @@ -40,7 +44,7 @@ async fn main() -> Result<(), ..> {
archimedes::WorkerOptions::default()
.concurrency(2)
.schema("example_simple_worker")
.define_job("say_hello", say_hello)
.define_job(say_hello)
.pg_pool(pg_pool)
.init()
.await?
Expand All @@ -59,6 +63,51 @@ Connect to your database and run the following SQL:
SELECT archimedes_worker.add_job('say_hello', json_build_object('name', 'Bobby Tables'));
```

### Schedule a job via RUST

```rust
// Reuse the task defined earlier :
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(())
}

// Run the task:
#[tokio::main]
async fn main() -> Result<(), ()> {
let worker = archimedes::WorkerOptions::default()
.concurrency(2)
.schema("example_simple_worker")
.define_job(say_hello)
.pg_pool(pg_pool)
.init()
.await?;

let worker_helpers = worker.create_helpers();

worker_helpers.add_job::<say_hello>(
// Using add_job forces the payload to be same struct defined in our type
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
worker_helpers.add_raw_job("say_hello", serde_json::json!({ "message": "world" })).await.unwrap();

Ok(())
}

```

### Success!

You should see the worker output `Hello Bobby Tables !`. Gosh, that was fast!
Expand Down
9 changes: 8 additions & 1 deletion src/sql/add_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::errors::ArchimedesError;
use chrono::Utc;
use getset::Getters;
use sqlx::{query, PgExecutor};
use tracing::info;

#[derive(Debug, Default)]
pub enum JobKeyMode {
Expand Down Expand Up @@ -59,7 +60,7 @@ pub async fn add_job(

query(&sql)
.bind(identifier)
.bind(payload)
.bind(&payload)
.bind(spec.queue_name)
.bind(spec.run_at)
.bind(spec.max_attempts)
Expand All @@ -70,5 +71,11 @@ pub async fn add_job(
.execute(executor)
.await?;

info!(
identifier,
payload = ?payload,
"Job added to queue"
);

Ok(())
}

0 comments on commit ca53165

Please sign in to comment.