[英]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.