geofmureithi / apalis

Simple, extensible multithreaded background job and message processing library for Rust
https://crates.io/crates/apalis
MIT License
457 stars 41 forks source link

apalis-cron example out of date #351

Closed thallada closed 2 months ago

thallada commented 3 months ago

I'm having some issues setting up a job that runs on a cron schedule and I cannot follow the apalis-cron example since it is using functions that don't exist in 0.5 (job_fn): https://github.com/geofmureithi/apalis/blob/master/packages/apalis-cron/README.md

I keep getting a type error (using 0.5.3 of apalis with cron feature):

use anyhow::Result;
use apalis::cron::{CronStream, Schedule};
use apalis::prelude::*;
use chrono::{DateTime, Utc};
use std::str::FromStr;

#[derive(Default, Debug, Clone)]
struct Crawl(DateTime<Utc>);

impl From<DateTime<Utc>> for Crawl {
    fn from(t: DateTime<Utc>) -> Self {
        Crawl(t)
    }
}

impl Job for Crawl {
    const NAME: &'static str = "apalis::Crawl";
}

pub async fn test_crawl_fn(job: Crawl) -> Result<()> {
    tracing::info!(job = ?job, "crawl");
    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    let schedule = Schedule::from_str("0 * * * * *").unwrap();

    let worker = WorkerBuilder::new("crawler")
        .stream(CronStream::new(schedule).into_stream())
        .build_fn(test_crawl_fn);

    Monitor::<TokioExecutor>::new()
        .register(worker)
        .run()
        .await
        .unwrap();

    Ok(())
}
error[E0277]: the trait bound `ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>> {test_crawl_fn}, _>: tower_service::Service<apalis::prelude::Request<_>>` is n
ot satisfied                                                                                                                                                                        
  --> src/bin/crawler.rs:31:10                                                                                                                                                      
   |                                                                                                                                                                                
31 |         .build_fn(test_crawl_fn);                                                                                                                                              
   |          ^^^^^^^^ the trait `tower_service::Service<apalis::prelude::Request<_>>` is not implemented for `ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>
> {test_crawl_fn}, _>`, which is required by `apalis::prelude::WorkerBuilder<_, Pin<Box<dyn futures_core::stream::Stream<Item = Result<Option<apalis::prelude::Request<_>>, apalis::
prelude::Error>> + Send>>, tower_layer::identity::Identity, _>: apalis::prelude::WorkerFactory<_, ServiceFn<_, _>>`                                                                 
   |                                                                                                                                                                                
   = help: the following other types implement trait `tower_service::Service<Request>`:                                                                                             
             ServiceFn<T, ()>                                                                                                                                                       
             ServiceFn<T, (A1, A2)>                                                                                                                                                 
             ServiceFn<T, (A1, A2, A3)>                                                                                                                                             
             ServiceFn<T, (A1, A2, A3, A4)>                                                                                                                                         
             ServiceFn<T, (A1, A2, A3, A4, A5)>                                                                                                                                     
             ServiceFn<T, (A1, A2, A3, A4, A5, A6)>                                                                                                                                 
             ServiceFn<T, (A1, A2, A3, A4, A5, A6, A7)>                                                                                                                             
             ServiceFn<T, (A1, A2, A3, A4, A5, A6, A7, A8)>                                                                                                                         
           and 9 others                                                                                                                                                             
   = note: required for `WorkerBuilder<_, Pin<Box<dyn Stream<Item = Result<Option<Request<_>>, Error>> + Send>>, Identity, ServiceFn<fn(Crawl) -> ... {test_crawl_fn}, ...>>` to imp
lement `apalis::prelude::WorkerFactory<_, ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>> {test_crawl_fn}, _>>`
   = note: the full name for the type has been written to '/Users/tyler/crawlnicle/target/debug/deps/crawler-03fd1b85afa5a103.long-type-13636588232781125929.txt'
   = note: consider using `--verbose` to print the full type name to the console

error[E0277]: the trait bound `ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>> {test_crawl_fn}, _>: tower_service::Service<apalis::prelude::Request<_>>` is n
ot satisfied
  --> src/bin/crawler.rs:31:10
   |
31 |         .build_fn(test_crawl_fn);
   |          ^^^^^^^^ the trait `tower_service::Service<apalis::prelude::Request<_>>` is not implemented for `ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>
> {test_crawl_fn}, _>`, which is required by `apalis::prelude::WorkerBuilder<_, Pin<Box<dyn futures_core::stream::Stream<Item = Result<Option<apalis::prelude::Request<_>>, apalis::
prelude::Error>> + Send>>, tower_layer::identity::Identity, ServiceFn<_, _>>: apalis::prelude::WorkerFactoryFn<_, _, _>`
   |
   = help: the following other types implement trait `tower_service::Service<Request>`:
             ServiceFn<T, ()>
             ServiceFn<T, (A1, A2)>
             ServiceFn<T, (A1, A2, A3)>
             ServiceFn<T, (A1, A2, A3, A4)>
             ServiceFn<T, (A1, A2, A3, A4, A5)>
             ServiceFn<T, (A1, A2, A3, A4, A5, A6)>
             ServiceFn<T, (A1, A2, A3, A4, A5, A6, A7)>
             ServiceFn<T, (A1, A2, A3, A4, A5, A6, A7, A8)>
           and 9 others
   = note: required for `WorkerBuilder<_, Pin<Box<dyn Stream<Item = Result<Option<Request<_>>, Error>> + Send>>, Identity, ServiceFn<fn(Crawl) -> ... {test_crawl_fn}, ...>>` to imp
lement `apalis::prelude::WorkerFactory<_, ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>> {test_crawl_fn}, _>>`
   = note: required for `WorkerBuilder<_, Pin<Box<dyn Stream<Item = Result<Option<Request<_>>, Error>> + Send>>, Identity, ServiceFn<fn(Crawl) -> ... {test_crawl_fn}, ...>>` to imp
lement `apalis::prelude::WorkerFactoryFn<_, fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>> {test_crawl_fn}, _>`
   = note: the full name for the type has been written to '/Users/tyler/crawlnicle/target/debug/deps/crawler-03fd1b85afa5a103.long-type-13636588232781125929.txt'
   = note: consider using `--verbose` to print the full type name to the console
   = note: the full name for the type has been written to '/Users/tyler/crawlnicle/target/debug/deps/crawler-03fd1b85afa5a103.long-type-13636588232781125929.txt'
   = note: consider using `--verbose` to print the full type name to the console

error[E0277]: the trait bound `ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>> {test_crawl_fn}, _>: tower_service::Service<apalis::prelude::Request<_>>` is not satisfied
  --> src/bin/crawler.rs:34:19
   |
34 |         .register(worker)
   |          -------- ^^^^^^ the trait `tower_service::Service<apalis::prelude::Request<_>>` is not implemented for `ServiceFn<fn(Crawl) -> impl Future<Output = Result<(), anyhow::Error>> {test_crawl_fn}, _>`
   |          |
   |          required by a bound introduced by this call
   |
   = help: the following other types implement trait `tower_service::Service<Request>`:
             ServiceFn<T, ()>
             ServiceFn<T, (A1, A2)>
             ServiceFn<T, (A1, A2, A3)>
             ServiceFn<T, (A1, A2, A3, A4)>
             ServiceFn<T, (A1, A2, A3, A4, A5)>
             ServiceFn<T, (A1, A2, A3, A4, A5, A6)>
             ServiceFn<T, (A1, A2, A3, A4, A5, A6, A7)>
             ServiceFn<T, (A1, A2, A3, A4, A5, A6, A7, A8)>
           and 9 others
note: required by a bound in `apalis::prelude::Monitor::<E>::register`
  --> /Users/tyler/.cargo/registry/src/index.crates.io-6f17d22bba15001f/apalis-core-0.5.3/src/monitor/mod.rs:83:12
   |
81 |     pub fn register<
   |            -------- required by a bound in this associated function
82 |         J: Send + Sync + 'static,
83 |         S: Service<Request<J>> + Send + 'static + Clone,
   |            ^^^^^^^^^^^^^^^^^^^ required by this bound in `Monitor::<E>::register`

I'm trying to follow what other repos have done to set up apalis-cron (e.g. https://github.com/IgnisDa/ryot/blob/182e4e7982d5fb24e08dd6e9593d2cece171185f/apps/backend/src/main.rs#L255-L270), I'm not sure why I am getting this type error while they are not.

geofmureithi commented 3 months ago

The issue is anyhow::Error which does not impl std::Error. See #258 The example should be updated.

geofmureithi commented 3 months ago

This example might help: https://github.com/geofmureithi/apalis/blob/master/examples/async-std-runtime/src/main.rs

geofmureithi commented 2 months ago

Resolved by https://github.com/geofmureithi/apalis/pull/359