Skip to content

Commit

Permalink
chore: improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
geofmureithi committed Dec 19, 2024
1 parent 5365a67 commit 9fb013e
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 85 deletions.
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ apalis-redis = { version = "0.6" }

```rust
use apalis::prelude::*;
use apalis_redis::{RedisStorage, Config};
use apalis_redis::RedisStorage;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
struct Email {
to: String,
}

/// A function that will be converted into a service.
/// A function called for every job
async fn send_email(job: Email, data: Data<usize>) -> Result<(), Error> {
/// execute job
Ok(())
Expand All @@ -94,33 +94,25 @@ async fn main() -> {
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let storage = RedisStorage::new(conn);
Monitor::new()
.register({
WorkerBuilder::new(format!("email-worker"))
.concurrency(2)
.data(0usize)
.backend(storage)
.build_fn(send_email)
})
.run()
.await
WorkerBuilder::new("email-worker")
.concurrency(2)
.data(0usize)
.backend(storage)
.build_fn(send_email)
.run()
.await;
}

```

Then

```rust
//This can be in another part of the program or another application eg a http server
async fn produce_route_jobs(storage: &RedisStorage<Email>) -> Result<()> {
let mut storage = storage.clone();
async fn produce_route_jobs(storage: &mut RedisStorage<Email>) -> Result<()> {
storage
.push(Email {
to: "test@example.com".to_string(),
})
.await?;
}

```

## Feature flags
Expand Down
3 changes: 2 additions & 1 deletion examples/actix-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async fn main() -> Result<()> {
std::env::set_var("RUST_LOG", "debug");
env_logger::init();

let conn = apalis_redis::connect("redis://127.0.0.1/").await?;
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let storage = RedisStorage::new(conn);
let data = web::Data::new(storage.clone());
let http = async {
Expand Down
3 changes: 2 additions & 1 deletion examples/axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ async fn main() -> Result<()> {
))
.with(tracing_subscriber::fmt::layer())
.init();
let conn = apalis_redis::connect("redis://127.0.0.1/").await?;
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let storage = RedisStorage::new(conn);
// build our application with some routes
let app = Router::new()
Expand Down
3 changes: 2 additions & 1 deletion examples/prometheus/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async fn main() -> Result<()> {
))
.with(tracing_subscriber::fmt::layer())
.init();
let conn = apalis_redis::connect("redis://127.0.0.1/").await?;
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let storage = RedisStorage::new(conn);
// build our application with some routes
let recorder_handle = setup_metrics_recorder();
Expand Down
3 changes: 2 additions & 1 deletion examples/redis-with-msg-pack/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ async fn main() -> Result<()> {

tracing_subscriber::fmt::init();

let conn = apalis_redis::connect("redis://127.0.0.1/").await?;
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let config = apalis_redis::Config::default()
.set_namespace("apalis_redis-with-msg-pack")
.set_max_retries(5);
Expand Down
3 changes: 2 additions & 1 deletion examples/redis/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ async fn main() -> Result<()> {

tracing_subscriber::fmt::init();

let conn = apalis_redis::connect("redis://127.0.0.1/").await?;
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let storage = RedisStorage::new(conn);
// This can be in another part of the program
produce_jobs(storage.clone()).await?;
Expand Down
3 changes: 2 additions & 1 deletion examples/rest-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ async fn main() -> Result<()> {
std::env::set_var("RUST_LOG", "debug");
env_logger::init();

let conn = apalis_redis::connect("redis://127.0.0.1/").await?;
let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let storage = RedisStorage::new(conn);
let data = web::Data::new(storage.clone());
let http = async {
Expand Down
32 changes: 10 additions & 22 deletions packages/apalis-cron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,32 @@ Since apalis-cron is build on top of apalis which supports tower middleware, you
## Example

```rust
use apalis::layers::retry::RetryLayer;
use apalis::layers::retry::RetryPolicy;
use tower::ServiceBuilder;
use apalis_cron::Schedule;
use apalis::{prelude::*, layers::retry::RetryPolicy};
use std::str::FromStr;
use apalis::prelude::*;
use apalis_cron::CronStream;
use apalis_cron::{CronStream, Schedule};
use chrono::{DateTime, Utc};

#[derive(Clone)]
struct FakeService;
impl FakeService {
fn execute(&self, item: Reminder){}
}

#[derive(Default, Debug, Clone)]
struct Reminder(DateTime<Utc>);
impl From<DateTime<Utc>> for Reminder {
fn from(t: DateTime<Utc>) -> Self {
Reminder(t)
}
}
async fn send_reminder(job: Reminder, svc: Data<FakeService>) {
svc.execute(job);
async fn handle_tick(job: Reminder, data: Data<usize>) {
// Do something with the current tick
}

#[tokio::main]
async fn main() {
let schedule = Schedule::from_str("@daily").unwrap();

let worker = WorkerBuilder::new("morning-cereal")
.retry(RetryPolicy::retries(5))
.data(FakeService)
.stream(CronStream::new(schedule).into_stream())
.build_fn(send_reminder);
Monitor::new()
.register(worker)
.run()
.await
.unwrap();
.data(42usize)
.backend(CronStream::new(schedule))
.build_fn(handle_tick);

worker.run().await;
}
```
47 changes: 19 additions & 28 deletions packages/apalis-cron/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,38 @@
//! ## Example
//!
//! ```rust,no_run
//! # use apalis::layers::retry::RetryLayer;
//! # use apalis::layers::retry::RetryPolicy;
//! use tower::ServiceBuilder;
//! use apalis_cron::Schedule;
//! use apalis::{prelude::*, layers::retry::RetryPolicy};
//! use std::str::FromStr;
//! # use apalis::prelude::*;
//! use apalis_cron::CronStream;
//! use apalis_cron::{CronStream, Schedule};
//! use chrono::{DateTime, Utc};
//!
//! #[derive(Clone)]
//! struct FakeService;
//! impl FakeService {
//! fn execute(&self, item: Reminder){}
//! }
//!
//!
//! #[derive(Default, Debug, Clone)]
//! struct Reminder(DateTime<Utc>);
//! impl From<DateTime<Utc>> for Reminder {
//! fn from(t: DateTime<Utc>) -> Self {
//! Reminder(t)
//! }
//!
//! # impl From<DateTime<Utc>> for Reminder {
//! # fn from(t: DateTime<Utc>) -> Self {
//! # Reminder(t)
//! # }
//! # }
//!
//! async fn handle_tick(job: Reminder, data: Data<usize>) {
//! // Do something with the current tick
//! }
//! async fn send_reminder(job: Reminder, svc: Data<FakeService>) {
//! svc.execute(job);
//! }
//!
//!
//! #[tokio::main]
//! async fn main() {
//! let schedule = Schedule::from_str("@daily").unwrap();
//!
//! let worker = WorkerBuilder::new("morning-cereal")
//! .retry(RetryPolicy::retries(5))
//! .data(FakeService)
//! .data(42usize)
//! .backend(CronStream::new(schedule))
//! .build_fn(send_reminder);
//! Monitor::new()
//! .register(worker)
//! .run()
//! .await
//! .unwrap();
//! .build_fn(handle_tick);
//!
//! worker.run().await;
//! }
//! ```
//! ```
use apalis_core::backend::Backend;
use apalis_core::error::BoxDynError;
Expand Down
28 changes: 17 additions & 11 deletions packages/apalis-redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
//! ```rust,no_run
//! use apalis::prelude::*;
//! use apalis_redis::{RedisStorage, Config};
//! use email_service::send_email;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Deserialize, Serialize)]
//! struct Email {
//! to: String,
//! }
//!
//! async fn send_email(job: Email) -> Result<(), Error> {
//! Ok(())
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! let conn = apalis_redis::connect("redis://127.0.0.1/").await.unwrap();
//! let redis_url = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
//! let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
//! let storage = RedisStorage::new(conn);
//! Monitor::new()
//! .register(
//! WorkerBuilder::new("tasty-pear")
//! .backend(storage.clone())
//! .build_fn(send_email),
//! )
//! .run()
//! .await
//! .unwrap();
//! let worker = WorkerBuilder::new("tasty-pear")
//! .backend(storage.clone())
//! .build_fn(send_email);
//!
//! worker.run().await;
//! }
//! ```
Expand Down

0 comments on commit 9fb013e

Please sign in to comment.