mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
feat(scheduler) remove terminate() etc. impl
This commit is contained in:
10 files changed
+91
-409
No files matched your search
@@ -1,145 +1,56 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::Arc,
|
||||
};
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use crate::scheduler::{
|
||||
error::{SchedulerError, SchedulerError::IdNotFound},
|
||||
runner::Runner,
|
||||
tasks::CronTask,
|
||||
};
|
||||
use crate::scheduler::{error::SchedulerError, tasks::CronTask, utils::execute};
|
||||
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
use futures::future::join_all;
|
||||
use tracing::warn;
|
||||
|
||||
pub mod error;
|
||||
mod runner;
|
||||
mod sleeper;
|
||||
pub mod task_func;
|
||||
pub mod tasks;
|
||||
mod utils;
|
||||
|
||||
pub type TaskPool = HashMap<String, Arc<Box<dyn CronTask>>>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Scheduler {
|
||||
running: RwLock<HashSet<Uuid>>,
|
||||
parking_pool: RwLock<HashMap<Uuid, Arc<Box<dyn CronTask>>>>,
|
||||
runner: Runner,
|
||||
task_pool: TaskPool,
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
/// Start all tasks in the scheduler
|
||||
pub async fn start(&self) -> Result<(), SchedulerError> {
|
||||
self.runner.run_all().await
|
||||
pub async fn start(&self) {
|
||||
dbg!("entering");
|
||||
|
||||
let fut = self
|
||||
.task_pool
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(name, task)| execute(name, task))
|
||||
.collect::<Vec<_>>();
|
||||
// self.runner.run_all().await
|
||||
join_all(fut).await;
|
||||
}
|
||||
|
||||
/// Terminate all tasks in the scheduler
|
||||
pub async fn halt(&self) -> Result<(), SchedulerError> {
|
||||
for id in self.running.read().await.iter() {
|
||||
let func = &self.runner.terminate_remove(*id).await?;
|
||||
self.running.write().await.remove(id);
|
||||
self.parking_pool.write().await.insert(*id, func.clone());
|
||||
pub fn insert(&mut self, task: (&str, Box<dyn CronTask>)) -> Result<&Self, SchedulerError> {
|
||||
let (k, v) = task;
|
||||
if self.task_pool.insert(k.into(), v.into()).is_some() {
|
||||
warn!("task list duplicated key detected: {}", k);
|
||||
}
|
||||
Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Run a task with `id`
|
||||
pub async fn run(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||
if self.running.read().await.contains(id) {
|
||||
Ok(())
|
||||
} else if self.parking_pool.read().await.contains_key(id) {
|
||||
let func = self.parking_pool.write().await.remove(id).unwrap();
|
||||
self.running.write().await.insert(*id);
|
||||
self.runner.add_with_id(func, id).await?;
|
||||
self.runner.run_id(id).await
|
||||
} else {
|
||||
Err(IdNotFound)
|
||||
pub fn insert_list(
|
||||
&mut self,
|
||||
tasks: Vec<(&str, Box<dyn CronTask>)>,
|
||||
) -> Result<&Self, SchedulerError> {
|
||||
for task in tasks {
|
||||
self.insert(task)?;
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop a task with `id`
|
||||
pub async fn stop(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||
if self.running.read().await.contains(id) {
|
||||
let func = self.runner.terminate_remove(*id).await?;
|
||||
self.running.write().await.remove(id);
|
||||
self.parking_pool.write().await.insert(*id, func.clone());
|
||||
Ok(())
|
||||
} else if self.parking_pool.read().await.get(id).is_some() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(IdNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
// pub fn now(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||
// }
|
||||
|
||||
pub async fn insert(&self, task: Box<dyn CronTask>) -> Result<Uuid, SchedulerError> {
|
||||
let id = self.runner.add(task.into()).await?;
|
||||
self.running.write().await.insert(id);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn insert_list(
|
||||
&self,
|
||||
tasks: Vec<Box<dyn CronTask>>,
|
||||
) -> Result<Vec<Uuid>, SchedulerError> {
|
||||
let ids = self
|
||||
.runner
|
||||
.add_list(tasks.into_iter().map(|t| t.into()).collect())
|
||||
.await?;
|
||||
self.running.write().await.extend(&ids);
|
||||
Ok(ids)
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use std::{error::Error, thread, time::Duration};
|
||||
//
|
||||
// use crate::scheduler::{
|
||||
// tasks::{async_basic::AsyncBasic, basic::BasicTask},
|
||||
// Scheduler,
|
||||
// };
|
||||
//
|
||||
// use uuid::Uuid;
|
||||
//
|
||||
// async fn execution(scheduler: Scheduler, id: Uuid) -> Result<(), Box<dyn Error>> {
|
||||
// for i in 1..=2 {
|
||||
// println!("Loop {} start", i);
|
||||
// println!("Run {}", i);
|
||||
// scheduler.run(&id).await?;
|
||||
// println!("Sleep {}", i);
|
||||
// thread::sleep(Duration::from_secs(4));
|
||||
// println!("Sleep {} finished", i);
|
||||
// scheduler.stop(&id)?;
|
||||
// println!("Stop {}", i);
|
||||
// thread::sleep(Duration::from_secs(2));
|
||||
// println!("Loop {} finished", i);
|
||||
// }
|
||||
// Ok(())
|
||||
// }
|
||||
//
|
||||
// #[tokio::test]
|
||||
// async fn test_run() -> Result<(), Box<dyn Error>> {
|
||||
// let scheduler = Scheduler::default();
|
||||
// let task = BasicTask::new(|| println!("test print"), "* * * * * *")?;
|
||||
// let id = scheduler.insert(Box::new(task)).await?;
|
||||
// scheduler.start().await?;
|
||||
// execution(scheduler, id)
|
||||
// }
|
||||
//
|
||||
// #[tokio::test]
|
||||
// async fn test_run_async() -> Result<(), Box<dyn Error>> {
|
||||
// let scheduler = Scheduler::default();
|
||||
// let task = AsyncBasic::new(
|
||||
// || Box::pin(async { println!("test async print") }),
|
||||
// "* * * * * *",
|
||||
// )?;
|
||||
// let id = scheduler.insert(Box::new(task)).await?;
|
||||
// scheduler.start().await?;
|
||||
// execution(scheduler, id)
|
||||
// }
|
||||
// }
|
||||
Reference in new issue
Block a user