mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
feat(backend) move module scheduler
This commit is contained in:
8 files changed
+381
-1
No files matched your search
@@ -0,0 +1,37 @@
|
||||
use crate::scheduler::task_func::TaskFunc;
|
||||
use crate::scheduler::tasks::Task;
|
||||
use chrono::{DateTime, Utc};
|
||||
use cron::Schedule;
|
||||
use std::error::Error;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct BasicTask {
|
||||
schedule: Schedule,
|
||||
func: Box<dyn TaskFunc>,
|
||||
}
|
||||
|
||||
impl BasicTask {
|
||||
pub fn new<F>(func: F, cron_expr: &str) -> Result<Self, Box<dyn Error>>
|
||||
where
|
||||
F: TaskFunc,
|
||||
{
|
||||
Ok(BasicTask {
|
||||
schedule: Schedule::from_str(cron_expr)?,
|
||||
func: Box::new(func),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_task(self) -> Box<dyn Task> {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Task for BasicTask {
|
||||
fn get_next_execution(&self) -> Option<DateTime<Utc>> {
|
||||
self.schedule.upcoming(Utc).next()
|
||||
}
|
||||
|
||||
fn call(&self) {
|
||||
(self.func)()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user