mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
ref, feat(scheduler) global project refactor, add scheduler
This commit is contained in:
47 files changed
+796
-1037
No files matched your search
@@ -0,0 +1,30 @@
|
||||
use crate::blog::scheduler::task_func::TaskFunc;
|
||||
use crate::blog::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(func: fn(), cron_expr: &str) -> Result<Self, Box<dyn Error>> {
|
||||
Ok(BasicTask {
|
||||
schedule: Schedule::from_str(cron_expr)?,
|
||||
func: Box::new(func),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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