mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(backend::scheduler) deadlock spotted
This commit is contained in:
15 files changed
+375
-428
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::{Arc, RwLock},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use crate::scheduler::{
|
||||
@@ -9,12 +9,13 @@ use crate::scheduler::{
|
||||
tasks::CronTask,
|
||||
};
|
||||
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub mod error;
|
||||
mod runner;
|
||||
mod sleeper;
|
||||
mod task_func;
|
||||
pub mod task_func;
|
||||
pub mod tasks;
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -26,43 +27,42 @@ pub struct Scheduler {
|
||||
|
||||
impl Scheduler {
|
||||
/// Start all tasks in the scheduler
|
||||
pub fn start(&self) -> Result<(), SchedulerError> {
|
||||
self.runner.run_all()
|
||||
pub async fn start(&self) -> Result<(), SchedulerError> {
|
||||
self.runner.run_all().await
|
||||
}
|
||||
|
||||
/// Terminate all tasks in the scheduler
|
||||
pub fn halt(&self) -> Result<(), SchedulerError> {
|
||||
for id in self.running.read()?.iter() {
|
||||
let func = &self.runner.terminate_remove(*id)?;
|
||||
self.running.write()?.remove(id);
|
||||
self.parking_pool.write()?.insert(*id, func.clone());
|
||||
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());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run a task with `id`
|
||||
pub fn run(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||
if self.running.read()?.contains(id) {
|
||||
pub async fn run(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||
if self.running.read().await.contains(id) {
|
||||
Ok(())
|
||||
} else if self.parking_pool.read()?.contains_key(id) {
|
||||
let func = self.parking_pool.write()?.remove(id).unwrap();
|
||||
self.running.write()?.insert(*id);
|
||||
self.runner
|
||||
.add_with_id(func, id)
|
||||
.and_then(|_| self.runner.run_id(id))
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop a task with `id`
|
||||
pub fn stop(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||
if self.running.read()?.contains(id) {
|
||||
let func = self.runner.terminate_remove(*id)?;
|
||||
self.running.write()?.remove(id);
|
||||
self.parking_pool.write()?.insert(*id, func.clone());
|
||||
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()?.get(id).is_some() {
|
||||
} else if self.parking_pool.read().await.get(id).is_some() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(IdNotFound)
|
||||
@@ -72,17 +72,21 @@ impl Scheduler {
|
||||
// pub fn now(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||
// }
|
||||
|
||||
pub fn insert(&self, task: Box<dyn CronTask>) -> Result<Uuid, SchedulerError> {
|
||||
let id = self.runner.add(task.into())?;
|
||||
self.running.write()?.insert(id);
|
||||
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 fn insert_list(&self, tasks: Vec<Box<dyn CronTask>>) -> Result<Vec<Uuid>, SchedulerError> {
|
||||
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())?;
|
||||
self.running.write()?.extend(&ids);
|
||||
.add_list(tasks.into_iter().map(|t| t.into()).collect())
|
||||
.await?;
|
||||
self.running.write().await.extend(&ids);
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
@@ -91,51 +95,51 @@ impl Scheduler {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{error::Error, thread, time::Duration};
|
||||
|
||||
use crate::scheduler::{
|
||||
tasks::{async_basic::AsyncBasic, basic::BasicTask},
|
||||
Scheduler,
|
||||
};
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
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)?;
|
||||
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(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
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))?;
|
||||
scheduler.start()?;
|
||||
execution(scheduler, id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
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))?;
|
||||
scheduler.start()?;
|
||||
execution(scheduler, id)
|
||||
}
|
||||
}
|
||||
// #[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