mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
feat(backend) update (unfinished)
This commit is contained in:
26 files changed
+1607
-14
No files matched your search
@@ -12,7 +12,7 @@ use crate::scheduler::{
|
||||
SchedulerError, SchedulerError::IdAlreadyRunning, SchedulerError::IdNotFound,
|
||||
},
|
||||
sleeper::SkippableSleeper,
|
||||
tasks::Task,
|
||||
tasks::CronTask,
|
||||
};
|
||||
|
||||
use tracing::log::error;
|
||||
@@ -30,7 +30,7 @@ struct TaskStatus {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Scheduler {
|
||||
tasks: Arc<RwLock<HashMap<Uuid, Arc<Box<dyn Task>>>>>,
|
||||
tasks: Arc<RwLock<HashMap<Uuid, Arc<Box<dyn CronTask>>>>>,
|
||||
running: Arc<RwLock<HashMap<Uuid, Arc<TaskStatus>>>>,
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ impl Scheduler {
|
||||
fn run_task(
|
||||
running: Arc<RwLock<HashMap<Uuid, Arc<TaskStatus>>>>,
|
||||
id: Uuid,
|
||||
task: Arc<Box<dyn Task>>,
|
||||
task: Arc<Box<dyn CronTask>>,
|
||||
) -> Result<(), SchedulerError> {
|
||||
if running.read()?.get(&id).is_some() {
|
||||
return Err(IdAlreadyRunning);
|
||||
@@ -159,13 +159,13 @@ impl Scheduler {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&self, task: Box<dyn Task>) -> Result<Uuid, SchedulerError> {
|
||||
pub fn insert(&self, task: Box<dyn CronTask>) -> Result<Uuid, SchedulerError> {
|
||||
let id = Uuid::new_v4();
|
||||
self.tasks.write()?.insert(id, Arc::new(task));
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn insert_list(&self, tasks: Vec<Box<dyn Task>>) -> Result<Vec<Uuid>, SchedulerError> {
|
||||
pub fn insert_list(&self, tasks: Vec<Box<dyn CronTask>>) -> Result<Vec<Uuid>, SchedulerError> {
|
||||
tasks.into_iter().map(|t| self.insert(t)).collect()
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ use std::{
|
||||
|
||||
use crate::scheduler::scheduler_error::SchedulerError::MutexPoison;
|
||||
|
||||
// todo: refactor with thiserror
|
||||
pub enum SchedulerError {
|
||||
IdNotFound,
|
||||
IdAlreadyRunning,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::error::Error;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::scheduler::{task_func::TaskFunc, tasks::Task};
|
||||
use crate::scheduler::{task_func::TaskFunc, tasks::CronTask};
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use cron::Schedule;
|
||||
@@ -22,12 +22,12 @@ impl BasicTask {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_task(self) -> Box<dyn Task> {
|
||||
pub fn to_task(self) -> Box<dyn CronTask> {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Task for BasicTask {
|
||||
impl CronTask for BasicTask {
|
||||
fn get_next_execution(&self) -> Option<DateTime<Utc>> {
|
||||
self.schedule.upcoming(Utc).next()
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use chrono::{DateTime, Utc};
|
||||
|
||||
pub mod basic;
|
||||
|
||||
pub trait Task: Send + Sync {
|
||||
pub trait CronTask: Send + Sync {
|
||||
fn get_next_execution(&self) -> Option<DateTime<Utc>>;
|
||||
fn call(&self);
|
||||
}
|
||||
Reference in new issue
Block a user