refactor(config)!: Move scheduler out of device_manager
Due to changes made in mlua the new scheduler is much simpler. It also had no real business being part of the device manager, so it has now been moved to be part of the returned config.
This commit is contained in:
28
src/schedule.rs
Normal file
28
src/schedule.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use std::collections::HashMap;
|
||||
use std::pin::Pin;
|
||||
|
||||
use tokio_cron_scheduler::{Job, JobScheduler, JobSchedulerError};
|
||||
|
||||
pub async fn start_scheduler(
|
||||
schedule: HashMap<String, mlua::Function>,
|
||||
) -> Result<(), JobSchedulerError> {
|
||||
let scheduler = JobScheduler::new().await?;
|
||||
|
||||
for (s, f) in schedule {
|
||||
let job = {
|
||||
move |_uuid, _lock| -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
||||
let f = f.clone();
|
||||
|
||||
Box::pin(async move {
|
||||
f.call_async::<()>(()).await.unwrap();
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
let job = Job::new_async(s, job)?;
|
||||
|
||||
scheduler.add(job).await?;
|
||||
}
|
||||
|
||||
scheduler.start().await
|
||||
}
|
||||
Reference in New Issue
Block a user