Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introducing WorkerBuilderExt #428

Open
wants to merge 2 commits into
base: chore/v0.6.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ async fn main() -> {
let conn = apalis_redis::connect(redis_url).await.expect("Could not connect");
let storage = RedisStorage::new(conn);
Monitor::new()
.register_with_count(2, {
.register({
WorkerBuilder::new(format!("email-worker"))
.concurrency(2)
.data(0usize)
.backend(storage)
.build_fn(send_email)
Expand Down
6 changes: 3 additions & 3 deletions examples/actix-web/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use actix_web::rt::signal;
use actix_web::{web, App, HttpResponse, HttpServer};
use anyhow::Result;
use apalis::layers::tracing::TraceLayer;
use apalis::prelude::*;
use apalis::utils::TokioExecutor;
use apalis_redis::RedisStorage;
Expand Down Expand Up @@ -42,9 +41,10 @@ async fn main() -> Result<()> {
Ok(())
};
let worker = Monitor::<TokioExecutor>::new()
.register_with_count(2, {
.register({
WorkerBuilder::new("tasty-avocado")
.layer(TraceLayer::new())
.enable_tracing()
.concurrency(2)
.backend(storage)
.build_fn(send_email)
})
Expand Down
4 changes: 2 additions & 2 deletions examples/async-std-runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{future::Future, str::FromStr, time::Duration};

use anyhow::Result;
use apalis::{
layers::{retry::RetryLayer, retry::RetryPolicy, tracing::MakeSpan, tracing::TraceLayer},
layers::{retry::RetryPolicy, tracing::MakeSpan, tracing::TraceLayer},
prelude::*,
};
use apalis_cron::{CronStream, Schedule};
Expand Down Expand Up @@ -42,7 +42,7 @@ async fn main() -> Result<()> {

let schedule = Schedule::from_str("1/1 * * * * *").unwrap();
let worker = WorkerBuilder::new("daily-cron-worker")
.layer(RetryLayer::new(RetryPolicy::retries(5)))
.retry(RetryPolicy::retries(5))
.layer(TraceLayer::new().make_span_with(ReminderSpan::new()))
.backend(CronStream::new(schedule))
.build_fn(send_reminder);
Expand Down
4 changes: 2 additions & 2 deletions examples/axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! cd examples && cargo run -p axum-example
//! ```
use anyhow::Result;
use apalis::layers::tracing::TraceLayer;

use apalis::prelude::*;
use apalis_redis::RedisStorage;
use axum::{
Expand Down Expand Up @@ -76,7 +76,7 @@ async fn main() -> Result<()> {
Monitor::<TokioExecutor>::new()
.register({
WorkerBuilder::new("tasty-pear")
.layer(TraceLayer::new())
.enable_tracing()
.backend(storage.clone())
.build_fn(send_email)
})
Expand Down
10 changes: 5 additions & 5 deletions examples/basics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ mod service;

use std::{sync::Arc, time::Duration};

use apalis::{
layers::{catch_panic::CatchPanicLayer, tracing::TraceLayer},
prelude::*,
};
use apalis::{layers::catch_panic::CatchPanicLayer, prelude::*};
use apalis_sql::sqlite::{SqlitePool, SqliteStorage};

use email_service::Email;
Expand Down Expand Up @@ -106,6 +103,8 @@ async fn main() -> Result<(), std::io::Error> {
.register({
WorkerBuilder::new("tasty-banana")
// This handles any panics that may occur in any of the layers below
// .catch_panic()
// Or just to customize
.layer(CatchPanicLayer::with_panic_handler(|e| {
let panic_info = if let Some(s) = e.downcast_ref::<&str>() {
s.to_string()
Expand All @@ -114,9 +113,10 @@ async fn main() -> Result<(), std::io::Error> {
} else {
"Unknown panic".to_string()
};
// Abort tells the backend to kill job
Error::Abort(Arc::new(Box::new(PanicError::Panic(panic_info))))
}))
.layer(TraceLayer::new())
.enable_tracing()
.layer(LogLayer::new("some-log-example"))
// Add shared context to all jobs executed by this worker
.data(EmailService::new())
Expand Down
10 changes: 5 additions & 5 deletions examples/catch-panic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;
use apalis::layers::catch_panic::CatchPanicLayer;
use apalis::prelude::*;
use apalis::utils::TokioExecutor;
use apalis::{layers::tracing::TraceLayer, prelude::*};
use apalis_sql::sqlite::SqliteStorage;

use email_service::Email;
Expand Down Expand Up @@ -40,10 +39,11 @@ async fn main() -> Result<()> {
produce_emails(&mut email_storage).await?;

Monitor::<TokioExecutor>::new()
.register_with_count(2, {
.register({
WorkerBuilder::new("tasty-banana")
.layer(CatchPanicLayer::new())
.layer(TraceLayer::new())
.catch_panic()
.enable_tracing()
.concurrency(2)
.backend(email_storage)
.build_fn(send_email)
})
Expand Down
6 changes: 2 additions & 4 deletions examples/cron/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use apalis::layers::tracing::TraceLayer;
use apalis::prelude::*;
use apalis::utils::TokioExecutor;
use apalis_cron::CronStream;
use apalis_cron::Schedule;
use chrono::{DateTime, Utc};
use std::str::FromStr;
use std::time::Duration;
use tower::limit::RateLimitLayer;
use tower::load_shed::LoadShedLayer;

#[derive(Clone)]
Expand All @@ -32,9 +30,9 @@ async fn send_reminder(job: Reminder, svc: Data<FakeService>) {
async fn main() {
let schedule = Schedule::from_str("1/1 * * * * *").unwrap();
let worker = WorkerBuilder::new("morning-cereal")
.layer(TraceLayer::new())
.enable_tracing()
.layer(LoadShedLayer::new()) // Important when you have layers that block the service
.layer(RateLimitLayer::new(1, Duration::from_secs(2)))
.rate_limit(1, Duration::from_secs(2))
.data(FakeService)
.backend(CronStream::new(schedule))
.build_fn(send_reminder);
Expand Down
3 changes: 2 additions & 1 deletion examples/fn-args/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ async fn main() -> Result<(), std::io::Error> {
let mut sqlite: SqliteStorage<SimpleJob> = SqliteStorage::new(pool);
produce_jobs(&mut sqlite).await;
Monitor::<TokioExecutor>::new()
.register_with_count(2, {
.register({
WorkerBuilder::new("tasty-banana")
.data(Count::default())
.data(sqlite.clone())
.concurrency(2)
.backend(sqlite)
.build_fn(simple_job)
})
Expand Down
3 changes: 2 additions & 1 deletion examples/graceful-shutdown/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ async fn main() -> Result<(), std::io::Error> {
let mut sqlite: SqliteStorage<LongRunningJob> = SqliteStorage::new(pool);
produce_jobs(&mut sqlite).await;
Monitor::<TokioExecutor>::new()
.register_with_count(2, {
.register({
WorkerBuilder::new("tasty-banana")
.concurrency(2)
.backend(sqlite)
.build_fn(long_running_task)
})
Expand Down
4 changes: 2 additions & 2 deletions examples/mysql/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use apalis::layers::tracing::TraceLayer;

use apalis::prelude::*;
use apalis_sql::mysql::MySqlPool;
use apalis_sql::mysql::MysqlStorage;
Expand Down Expand Up @@ -36,7 +36,7 @@ async fn main() -> Result<()> {
Monitor::new_with_executor(TokioExecutor)
.register_with_count(1, {
WorkerBuilder::new("tasty-avocado")
.layer(TraceLayer::new())
.enable_tracing()
.backend(mysql)
.build_fn(send_email)
})
Expand Down
7 changes: 3 additions & 4 deletions examples/postgres/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Result;
use apalis::layers::retry::RetryPolicy;
use apalis::layers::tracing::TraceLayer;

use apalis::prelude::*;
use apalis_sql::postgres::{PgListen, PgPool, PostgresStorage};
use email_service::{send_email, Email};
use tower::retry::RetryLayer;
use tracing::{debug, info};

async fn produce_jobs(storage: &mut PostgresStorage<Email>) -> Result<()> {
Expand Down Expand Up @@ -47,8 +46,8 @@ async fn main() -> Result<()> {
Monitor::<TokioExecutor>::new()
.register_with_count(4, {
WorkerBuilder::new("tasty-orange")
.layer(TraceLayer::new())
.layer(RetryLayer::new(RetryPolicy::retries(5)))
.enable_tracing()
.retry(RetryPolicy::retries(5))
.backend(pg)
.build_fn(send_email)
})
Expand Down
4 changes: 2 additions & 2 deletions examples/redis-mq-example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt::Debug, marker::PhantomData, time::Duration};

use apalis::{layers::tracing::TraceLayer, prelude::*};
use apalis::prelude::*;

use apalis_redis::{self, Config};

Expand Down Expand Up @@ -173,7 +173,7 @@ async fn main() -> anyhow::Result<()> {
produce_jobs(&mut mq).await?;

let worker = WorkerBuilder::new("rango-tango")
.layer(TraceLayer::new())
.enable_tracing()
.backend(mq)
.build_fn(send_email);

Expand Down
12 changes: 5 additions & 7 deletions examples/redis/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::time::Duration;

use anyhow::Result;
use apalis::layers::limit::{ConcurrencyLimitLayer, RateLimitLayer};
use apalis::layers::tracing::TraceLayer;
use apalis::layers::ErrorHandlingLayer;
use apalis::{layers::TimeoutLayer, prelude::*};
use apalis::prelude::*;
use apalis_redis::RedisStorage;

use email_service::{send_email, Email};
Expand Down Expand Up @@ -36,10 +34,10 @@ async fn main() -> Result<()> {

let worker = WorkerBuilder::new("rango-tango")
.layer(ErrorHandlingLayer::new())
.layer(TraceLayer::new())
.layer(RateLimitLayer::new(5, Duration::from_secs(1)))
.layer(TimeoutLayer::new(Duration::from_millis(500)))
.layer(ConcurrencyLimitLayer::new(2))
.enable_tracing()
.rate_limit(5, Duration::from_secs(1))
.timeout(Duration::from_millis(500))
.concurrency(2)
.backend(storage)
.build_fn(send_email);

Expand Down
7 changes: 4 additions & 3 deletions examples/sentry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;
use tracing_subscriber::prelude::*;

use anyhow::Result;
use apalis::layers::tracing::TraceLayer;

use apalis::{layers::sentry::SentryLayer, prelude::*};
use apalis_redis::RedisStorage;
use email_service::Email;
Expand Down Expand Up @@ -133,11 +133,12 @@ async fn main() -> Result<()> {
produce_jobs(storage.clone()).await?;

Monitor::<TokioExecutor>::new()
.register_with_count(2, {
.register({
WorkerBuilder::new("tasty-avocado")
.layer(NewSentryLayer::new_from_top())
.layer(SentryLayer::new())
.layer(TraceLayer::new())
.enable_tracing()
.concurrency(2)
.backend(storage.clone())
.build_fn(email_service)
})
Expand Down
6 changes: 3 additions & 3 deletions examples/sqlite/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod job;

use anyhow::Result;
use apalis::prelude::*;
use apalis::utils::TokioExecutor;
use apalis::{layers::tracing::TraceLayer, prelude::*};
use apalis_sql::sqlite::SqliteStorage;
use chrono::Utc;
use email_service::{send_email, Email};
Expand Down Expand Up @@ -61,13 +61,13 @@ async fn main() -> Result<()> {
Monitor::<TokioExecutor>::new()
.register({
WorkerBuilder::new("tasty-banana")
.layer(TraceLayer::new())
.enable_tracing()
.backend(email_storage)
.build_fn(send_email)
})
.register({
WorkerBuilder::new("tasty-mango")
// .layer(TraceLayer::new())
// .enable_tracing()
.backend(notification_storage)
.build_fn(job::notify)
})
Expand Down
5 changes: 2 additions & 3 deletions examples/tracing/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::Result;

use apalis::layers::tracing::TraceLayer;
use apalis::layers::WorkerBuilderExt;
use apalis::{
prelude::{Monitor, Storage, WorkerBuilder, WorkerFactoryFn},
utils::TokioExecutor,
Expand Down Expand Up @@ -72,7 +71,7 @@ async fn main() -> Result<()> {
Monitor::<TokioExecutor>::new()
.register(
WorkerBuilder::new("tasty-avocado")
.chain(|srv| srv.layer(TraceLayer::new()))
.enable_tracing()
.backend(storage)
.build_fn(email_service),
)
Expand Down
10 changes: 5 additions & 5 deletions packages/apalis-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ impl<M, Serv> WorkerBuilder<(), (), (), M, Serv> {
}
}

impl<Req, M, Serv, Ctx> WorkerBuilder<Req, Ctx, (), M, Serv> {
impl<Req, M, Serv, Ctx, Src> WorkerBuilder<Req, Ctx, Src, M, Serv> {
/// Allows of decorating the service that consumes jobs.
/// Allows adding multiple [`tower`] middleware
pub fn chain<NewLayer>(
self,
f: impl Fn(ServiceBuilder<M>) -> ServiceBuilder<NewLayer>,
) -> WorkerBuilder<Req, Ctx, (), NewLayer, Serv> {
f: impl FnOnce(ServiceBuilder<M>) -> ServiceBuilder<NewLayer>,
) -> WorkerBuilder<Req, Ctx, Src, NewLayer, Serv> {
let middleware = f(self.layer);

WorkerBuilder {
Expand All @@ -109,7 +109,7 @@ impl<Req, M, Serv, Ctx> WorkerBuilder<Req, Ctx, (), M, Serv> {
}
}
/// Allows adding a single layer [tower] middleware
pub fn layer<U>(self, layer: U) -> WorkerBuilder<Req, Ctx, (), Stack<U, M>, Serv>
pub fn layer<U>(self, layer: U) -> WorkerBuilder<Req, Ctx, Src, Stack<U, M>, Serv>
where
M: Layer<U>,
{
Expand All @@ -124,7 +124,7 @@ impl<Req, M, Serv, Ctx> WorkerBuilder<Req, Ctx, (), M, Serv> {

/// Adds data to the context
/// This will be shared by all requests
pub fn data<D>(self, data: D) -> WorkerBuilder<Req, Ctx, (), Stack<Data<D>, M>, Serv>
pub fn data<D>(self, data: D) -> WorkerBuilder<Req, Ctx, Src, Stack<Data<D>, M>, Serv>
where
M: Layer<Data<D>>,
{
Expand Down
2 changes: 1 addition & 1 deletion packages/apalis-cron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn send_reminder(job: Reminder, svc: Data<FakeService>) {
async fn main() {
let schedule = Schedule::from_str("@daily").unwrap();
let worker = WorkerBuilder::new("morning-cereal")
.layer(RetryLayer::new(RetryPolicy::retries(5)))
.retry(RetryPolicy::retries(5))
.data(FakeService)
.stream(CronStream::new(schedule).into_stream())
.build_fn(send_reminder);
Expand Down
2 changes: 1 addition & 1 deletion packages/apalis-cron/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
//! async fn main() {
//! let schedule = Schedule::from_str("@daily").unwrap();
//! let worker = WorkerBuilder::new("morning-cereal")
//! .layer(RetryLayer::new(RetryPolicy::retries(5)))
//! .retry(RetryPolicy::retries(5))
//! .data(FakeService)
//! .backend(CronStream::new(schedule))
//! .build_fn(send_reminder);
Expand Down
Loading
Loading