Having trouble in modularization of rusty celery

Hi. I'm using rusty celery and I have problem on building the project.
This is the structure of the project:

src
 |____ <some modules of celery project>
 |____ task/
 |____ bin/
      |____ consumer.rs
 |____ celery_app.rs
 |____ lib.rs
 |____ main.rs

The bin/consumer.rs and celery_app.rs are created by me. Because I want to run consumer as binary like cargo run --bin consumer.

At first the celery_app.rs file was:

use anyhow::Result;
use async_trait::async_trait;
use celery:prelude::*;
use env_logger::Env;
use tokio::time::{self, Duration};

extern crate dotenv;
use dotenv::dotenv;
use std::env;

pub async fn create_app() -> Result<std::sync::Arc<celery::Celery<celery::broker::AMQPBroker>>> {
    dotenv().ok();

    let my_app = crate::app!(
        broker = AMQPBroker { env::var("AMQP_ADDR").unwrap() },
        tasks = [
            add
        ],
        task_routes = [
            "buggy_task" => "buggy-queue",
            "*" => "celery",
        ],
        prefetch_count = 2,
        heartbeat = Some(10),
    ).await?;

    Ok(my_app)
}

#[celery::task]
pub fn add(x: i32, y: i32) -> TaskResult<i32> {
    Ok(x + y)
}

Then, I created the bin/consumer.rs file:

use anyhow::Result;
use celery::prelude::*;
use env_logger::Env;

extern crate dotenv;
use dotenv::dotenv;
use std::env;

use celery::celery_app::create_app;


#[tokio::main]
async fn main() -> Result<()> {
    dotenv().ok();
    env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

    let my_app = create_app().await.unwrap();

    my_app.display_pretty().await;
    my_app.consume_from(&["celery", "buggy-queue"]).await?;

    Ok(())
}

After running cargo build I received this error:

error[E0432]: unresolved import `celery::celery_app`
 --> src/bin/consumer.rs:9:13
  |
9 | use celery::celery_app::create_app;
  |             ^^^^^^^^^^ could not find `celery_app` in `celery`

So I've added pub mod celery_app; at the end of lib.rs file.
But this time the error occurred in celery_app.rs file:

error[E0433]: failed to resolve: use of undeclared crate or module `celery`
 --> src/celery_app.rs:5:5
  |
5 | use celery::prelude::*;
  |     ^^^^^^ use of undeclared crate or module `celery`

error[E0433]: failed to resolve: use of undeclared crate or module `celery`
  --> src/celery_app.rs:17:18
   |
17 |     let my_app = celery::app!(
   |                  ^^^^^^ use of undeclared crate or module `celery`

error[E0433]: failed to resolve: use of undeclared crate or module `celery`
  --> src/celery_app.rs:52:3
   |
52 | #[celery::task]
   |   ^^^^^^ use of undeclared crate or module `celery`

error[E0433]: failed to resolve: use of undeclared crate or module `celery`
  --> src/celery_app.rs:14:52
   |
14 | pub async fn create_app() -> Result<std::sync::Arc<celery::Celery<celery::broker::AMQPBroker>>> {
   |                                                    ^^^^^^ use of undeclared crate or module `celery`

error[E0433]: failed to resolve: use of undeclared crate or module `celery`
  --> src/celery_app.rs:14:67
   |
14 | pub async fn create_app() -> Result<std::sync::Arc<celery::Celery<celery::broker::AMQPBroker>>> {
   |                                                                   ^^^^^^ use of undeclared crate or module `celery`

error[E0412]: cannot find type `TaskResult` in this scope
  --> src/celery_app.rs:53:31
   |
53 | pub fn add(x: i32, y: i32) -> TaskResult<i32> {
   |                               ^^^^^^^^^^ not found in this scope

Then I replaced celery with crate keyword and my celery_app.rs became:

use anyhow::Result;
use async_trait::async_trait;
use crate::prelude::*;
use env_logger::Env;
use tokio::time::{self, Duration};

extern crate dotenv;
use dotenv::dotenv;
use std::env;


pub async fn create_app() -> Result<std::sync::Arc<crate::Celery<crate::broker::AMQPBroker>>> {
    dotenv().ok();

    let my_app = crate::app!(
        broker = AMQPBroker { env::var("AMQP_ADDR").unwrap() },
        tasks = [
            add,
            // long_running_task,
            // bound_task,
        ],
        task_routes = [
            "buggy_task" => "buggy-queue",
            "*" => "celery",
        ],
        prefetch_count = 2,
        heartbeat = Some(10),
    ).await?;

    Ok(my_app)
}

#[crate::task]
pub fn add(x: i32, y: i32) -> TaskResult<i32> {
    Ok(x + y)
}

Now my problem is related to #[crate::task] such that without it the project is build successfully.
The output of the cargo build:

error[E0706]: functions in traits cannot be declared `async`
  --> src/celery_app.rs:53:1
   |
53 | #[crate::task]
   | ^^^^^^^^^^^^^^ `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: could not find `celery` in the list of imported crates
  --> src/celery_app.rs:53:1
   |
53 | #[crate::task]
   | ^^^^^^^^^^^^^^ could not find `celery` in the list of imported crates
   |
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot determine resolution for the attribute macro `async_trait`
  --> src/celery_app.rs:53:1
   |
53 | #[crate::task]
   | ^^^^^^^^^^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: could not find `celery` in the list of imported crates
  --> src/celery_app.rs:53:1
   |
53 | #[crate::task]
   | ^^^^^^^^^^^^^^ not found in `celery::task`
   |
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this struct
   |
3  | use crate::task::Signature;
   |

Even I added the use crate::task::Signature; but it didn't have effect.

The project exists in here(dev branch)

You need to declare your binaries in Cargo.toml.

https://doc.rust-lang.org/cargo/reference/cargo-targets.html#binaries

src/main.rs and src/bin/*.rs files are automatically compiled as binaries, and don't need to be explicitly listed in Cargo.toml.

You may need to replace celery with the magic variable $crate within your macro_rules definitions:

3 Likes

Thanks. This worked