繁体   English   中英

火箭整流罩、tokio-scheduler 和 crons 的生命周期问题

[英]Lifetime issues with rocket fairings, tokio-scheduler and crons

我目前正在 Rocket 上构建一个 API 引擎,正如现代应用程序所做的那样,我想包含一个自动调度程序来在 Rocket API 运行时运行异步任务(又名 crons)。

我决定使用 Rocket fairings 在“升空”事件中启用围绕 tokio-schedule 构建的上述调度程序。

事实上,我设置了所有必需的部分(登录数据库、结构和特征),但在编译我的整流罩寿命时出现了一个奇怪的错误。

这是我的代码的演练:

-> 这是我的“命令”模块,包含用于使用我的应用程序构建和移动命令(又名 crons)的所有结构部分。

/// Synthetize a command execution result.
pub enum CommandResult {
    SUCCESS,
    ERROR(String),
    SKIPPED(String),
}

/// Trait to define structs as runnable async crons with tokio_scheduler
#[async_trait]
pub trait Command: Send + Sync {
    /// returns the current command name
    fn get_command_name(&self) -> String;

    /// returns the current command argument payload
    fn get_command_args(&self) -> Option<HashMap<String, String>>;

    /// returns the "cron_middleware"
    fn get_cron_middleware(&self) -> CronLogMiddleware<CronLogRepository>;

    /// real body for the command execution, must be overriden in impls.
    async fn do_run(&self) -> Result<CommandResult>;

    /// starts the command process by validating command lock, and registering an open cron log into database.
    async fn begin(&self) -> Result<CronLog> {
        // ...
    }

    /// ends the command process by releasing command lock, and registering the result of the command to an opened cron log into database.
    async fn end(&self, cron_log: &CronLog, result: CommandResult) -> Result<()> {
        // ...
    }

    /// hidden runner of commands, uses begin, end and do_run, and will be used by runner.
    async fn run(&self) -> Result<()> {
        // ...
    }

    /// generates a unique key for this command name + args, for locks purposes
    fn generate_unicity_key(&self) -> String {
        // ...
    }

    /// converts command args as a string payload
    #[allow(clippy::or_fun_call)]
    fn get_command_args_as_string(&self) -> String {
        // ...
    }
}

/// struct to move a command + its cron schedule into scheduler.
pub struct CommandHandle<T: Command + ?Sized + Send + Sync> {
    pub command: Box<T>,
    pub schedule: String,
}

然后,出于测试目的,我创建了一个测试命令结构,如下所示:

/// a testing purpose command
pub struct TestCommand {
    pub name: String,
    pub args: Option<HashMap<String, String>>,
    pub cron_log_middleware: CronLogMiddleware<CronLogRepository>,
}

#[async_trait]
impl Command for TestCommand {
    // accessors (get_... functions)

    async fn do_run(&self) -> Result<CommandResult> {
        debug!("executed !");

        Ok(CommandResult::SUCCESS)
    }
}

火箭制造者看起来像这样:

    let mut sched = CronScheduler::default();

    sched.add_cron(CommandHandle {
        command: Box::new(TestCommand {
            name: "app:test".to_string(),
            args: None,
            cron_log_middleware: cron_log_middleware.clone(),
        }),
        schedule: "*/1 * * * *".to_string(),
    });

    // then I add sched to rocket with .manage()

整流罩看起来像这样:


/// a rocket fairing enabling async tasks (eg crons) while rocket is launching
#[derive(Default)]
pub struct CronScheduler {
    crons: Vec<CommandHandle<dyn Command>>,
}

impl CronScheduler {
    /// adds a cron (eg CommandHandle with a given command) to run with the scheduler.
    pub fn add_cron(&mut self, cron: CommandHandle<dyn Command>) {
        self.crons.push(cron);
    }
}

#[rocket::async_trait]
impl Fairing for CronScheduler {
    //...
                        v -- error is here
    async fn on_liftoff(&self, _rocket: &Rocket<Orbit>) {
        let sched = SchedulerBuilder::build().await;

        for handle in self.crons.iter() {
            let job = Job::new_cron_job_async(handle.schedule.as_str(), |_uid, _lock| {
                Box::pin(async move {
                    handle.command.run().await;
                })
            })
            .unwrap();

            sched.add(job).await.unwrap();
        }

        sched.start().await.unwrap();
    }
}

Aaaand 我得到这个错误:

error[E0759]: `self` has lifetime `'life0` but it needs to satisfy a `'static` lifetime requirement
  --> src/core/fairings/cron_scheduler.rs:34:26
   |
34 |       async fn on_liftoff(&self, rocket: &Rocket<Orbit>) {
   |                            ^^^^ this data with lifetime `'life0`...
...
39 | /                 Box::pin(async move {
40 | |                     handle.command.run().await;
41 | |                 })
   | |__________________- ...is used and required to live as long as `'static` here
   |
note: `'static` lifetime requirement introduced by the return type
  --> src/core/fairings/cron_scheduler.rs:34:5
   |
34 |       async fn on_liftoff(&self, rocket: &Rocket<Orbit>) {
   |       ^^^^^ requirement introduced by this return type
...
39 | /                 Box::pin(async move {
40 | |                     handle.command.run().await;
41 | |                 })
   | |__________________- because of this returned expression

我的货物 toml 缩短了:

[dependencies]
rocket = {version = "0.5.0-rc.2", features = ["json"]}
// ...
tokio-cron-scheduler = {version = "0.8.1", features = ["signal"]}
// ...

我尝试了不同的解决方案,并指出这是导致问题的命令,就好像我用类似 println 的东西替换了“Box::pin(...)”的内容。 没有任何问题。

我不知道这是否是 async-trait 和 rocket async traits 之间的冲突,或者其他,但我无法弄清楚。

编辑 1:缩短了很多代码,因为票太长了。

编辑 2:由于经过验证的答案找到了解决方案; 这是修补的最终代码,如果它可以帮助任何人。

FTR,我确实必须自己实施克隆(不使用宏)并使用固定答案的代码作为参考。

// command 

pub struct CommandHandle<T: Command + ?Sized + Send + Sync> {
    pub command: Arc<T>,
    pub schedule: String,
}

impl<T> Clone for CommandHandle<T>
where
    T: Command + ?Sized + Send + Sync,
{
    fn clone(&self) -> Self {
        Self {
            command: self.command.clone(),
            schedule: self.schedule.clone(),
        }
    }
}

// fairing 

    async fn on_liftoff(&self, _rocket: &Rocket<Orbit>) {
        let sched = SchedulerBuilder::build().await;

        for handle in self.crons.iter() {
            let schedule = handle.schedule.clone();

            let handle = handle.clone();

            let job = Job::new_cron_job_async(schedule.as_str(), move |_uid, _lock| {
                let handle = handle.clone();

                Box::pin(async move {
                    handle.command.run().await.unwrap();
                })
            })
            .unwrap();

            sched.add(job).await.unwrap();
        }

        sched.start().await.unwrap();
    }

Job::new_cron_job_async要求你的闭包是'static ,但它不是,因为handle是对self.crons的引用。

快速查看您的结构,如果您在CommandHandle中使用Arc而不是Box ,那么它很容易被克隆,因此可以为 cron 作业提供一个'static句柄”:

pub struct CommandHandle<T: Command + ?Sized + Send + Sync> {
    pub command: Arc<T>, // <------------
    pub schedule: String,
}

impl Clone for CommandHandle ...
for handle in self.crons.iter() {
    let handle = handle.clone(); // <------------               vvvv
    let job = Job::new_cron_job_async(handle.schedule.as_str(), move |_uid, _lock| {
        let handle = handle.clone(); // <------------
        Box::pin(async move {
            handle.command.run().await;
        })
    })
    .unwrap();

    sched.add(job).await.unwrap();
}

由于您发布的代码不完整,因此很难验证,但我相信您需要上面的两个克隆,因为 function 需要所有权为'static ,但也必须是FnMut才能被多次调用,因此无法移动句柄直接进入async块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM