Skip to content

How to use Monitor & Backend? #58

Closed
Closed

Description

@Frankkkkk

Hi,

First of all thank you for this nice work!
I was trying to run two workers at the same time, but can't make the example work the way it's show on the readme.

What I have:

#[tokio::main]
async fn main() {
    let pool = get_pg_pool().await;
    let mut email_backend = PostgresStorage::new(&pool);
    let mut reminder_backend = PostgresStorage::new(&pool);

    let monitor = Monitor::new()
        .on_event(|ctx, event| println!("{}: {:?}", ctx.name(), event))
        .register(move |_| {
            let a = WorkerBuilder::new("email-worker")
                .backend(email_backend)
                .build(send_email_job);
            a
        })
        .register(move |_| {
            let b = WorkerBuilder::new("reminder-worker")
                .backend(reminder_backend)
                .build(send_reminder);
            b
        });

    monitor.run().await.unwrap();
}

But it says:

error[E0507]: cannot move out of `reminder_backend`, a captured variable in an `Fn` closure
   --> src/bin/worker.rs:22:26
    |
 10 |     let mut reminder_backend = PostgresStorage::new(&pool);
    |         --------------------   --------------------------- move occurs because `reminder_backend` has type `apalis_postgres::PostgresStorage<Remind>`, which does not implement the `Copy` trait
    |         |
    |         captured outer variable
...
 20 |         .register(move |_| {
    |                   -------- captured by this `Fn` closure
 21 |             let b = WorkerBuilder::new("reminder-worker")
 22 |                 .backend(reminder_backend)
    |                          ^^^^^^^^^^^^^^^^ `reminder_backend` is moved here

What works:

async fn main() {
    let pool = get_pg_pool().await;
    let pool_b = get_pg_pool().await;

    let monitor = Monitor::new()
        .on_event(|ctx, event| println!("{}: {:?}", ctx.name(), event))
        .register(move |_| {
            let mut email_backend = PostgresStorage::new(&pool);
            let a = WorkerBuilder::new("email-worker")
                .backend(email_backend)
                .build(send_email_job);
            a
        })
        .register(move |_| {
            let mut reminder_backend = PostgresStorage::new(&pool_b);
            let b = WorkerBuilder::new("reminder-worker")
                .backend(reminder_backend)
                .build(send_reminder);
            b
        });

    monitor.run().await.unwrap();
}

But it's rather ugly to duplicate the pool for each backend?

I'm sure I'm doing something wrong, but I don't see it right now

Thanks and cheers!
Frank

Activity

  1. geofmureithi commented on Feb 19, 2026

    @geofmureithi
    Member

    Have you looked at this example?

    You have a move problem where the register callback needs Clone since Monitor may need to restart a Worker.

  2. Frankkkkk commented on Feb 19, 2026

    @Frankkkkk
    Author

    It looks like the PostgresStorage doesn't implement clone:

    the trait bound `PgFetcher<_, Vec<u8>, apalis_codec::json::JsonCodec<Vec<u8>>>: Clone` is not satisfied
    required for `PgFetcher<SendEmail, Vec<u8>, apalis_codec::json::JsonCodec<Vec<u8>>>` to implement `Clone`
    1 redundant requirement hidden
    required for `apalis_postgres::PostgresStorage<SendEmail>` to implement `Clone`
    
  3. geofmureithi commented on Feb 19, 2026

    @geofmureithi
    Member

    That is a bug, It should be a straight forward fix. Will do a fix ASAP

  4. added theissue type on Feb 19, 2026
  5. geofmureithi commented on Feb 19, 2026

    @geofmureithi
    Member

    https://docs.rs/apalis-postgres/latest/apalis_postgres/struct.PgFetcher.html#impl-Clone-for-PgFetcher%3CArgs,+Compact,+Decode%3E

    The problem is that it expects Args to be clone and I guess SendEmail is not.

    Making your SendEmail clone might fix this for now

  6. Frankkkkk commented on Feb 19, 2026

    @Frankkkkk
    Author

    Hey, you're right, that fixes it. Sorry for the hassle, and thanks!

  7. geofmureithi commented on Feb 19, 2026

    @geofmureithi
    Member

    Reopening this to allow Clone without Args being Clone

  8. geofmureithi commented on Feb 21, 2026

    @geofmureithi
    Member

    Fixed in latest release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions