mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(backend) fix deadlock issue, code cleanup
This commit is contained in:
2 files changed
+62
-33
No files matched your search
@@ -19,8 +19,8 @@ pub mod tasks;
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Scheduler {
|
pub struct Scheduler {
|
||||||
running: Arc<RwLock<HashSet<Uuid>>>,
|
running: RwLock<HashSet<Uuid>>,
|
||||||
parking_pool: Arc<RwLock<HashMap<Uuid, Arc<Box<dyn CronTask>>>>>,
|
parking_pool: RwLock<HashMap<Uuid, Arc<Box<dyn CronTask>>>>,
|
||||||
runner: Runner,
|
runner: Runner,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +47,9 @@ impl Scheduler {
|
|||||||
} else if self.parking_pool.read()?.contains_key(id) {
|
} else if self.parking_pool.read()?.contains_key(id) {
|
||||||
let func = self.parking_pool.write()?.remove(id).unwrap();
|
let func = self.parking_pool.write()?.remove(id).unwrap();
|
||||||
self.running.write()?.insert(*id);
|
self.running.write()?.insert(*id);
|
||||||
self.runner.add_with_id(func, id)
|
self.runner
|
||||||
|
.add_with_id(func, id)
|
||||||
|
.and_then(|_| self.runner.run_id(id))
|
||||||
} else {
|
} else {
|
||||||
Err(IdNotFound)
|
Err(IdNotFound)
|
||||||
}
|
}
|
||||||
@@ -55,21 +57,14 @@ impl Scheduler {
|
|||||||
|
|
||||||
/// Stop a task with `id`
|
/// Stop a task with `id`
|
||||||
pub fn stop(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
pub fn stop(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||||
dbg!("stop() called");
|
|
||||||
if self.running.read()?.contains(id) {
|
if self.running.read()?.contains(id) {
|
||||||
dbg!("b1");
|
|
||||||
let func = self.runner.terminate_remove(*id)?;
|
let func = self.runner.terminate_remove(*id)?;
|
||||||
dbg!("b1a");
|
|
||||||
self.running.write()?.remove(id);
|
self.running.write()?.remove(id);
|
||||||
dbg!("b1b");
|
|
||||||
self.parking_pool.write()?.insert(*id, func.clone());
|
self.parking_pool.write()?.insert(*id, func.clone());
|
||||||
dbg!("b1c");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else if self.parking_pool.read()?.get(id).is_some() {
|
} else if self.parking_pool.read()?.get(id).is_some() {
|
||||||
dbg!("b2");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
dbg!("b3");
|
|
||||||
Err(IdNotFound)
|
Err(IdNotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,18 +93,16 @@ impl Scheduler {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::scheduler::tasks::basic::BasicTask;
|
use std::{error::Error, thread, time::Duration};
|
||||||
use crate::scheduler::Scheduler;
|
|
||||||
use std::error::Error;
|
|
||||||
use std::thread;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[test]
|
use crate::scheduler::{
|
||||||
fn test_run() -> Result<(), Box<dyn Error>> {
|
tasks::{async_basic::AsyncBasic, basic::BasicTask},
|
||||||
let scheduler = Scheduler::default();
|
Scheduler,
|
||||||
let task = BasicTask::new(|| println!("test print"), "* * * * * *")?;
|
};
|
||||||
let id = scheduler.insert(Box::new(task))?;
|
|
||||||
scheduler.start()?;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
fn execution(scheduler: Scheduler, id: Uuid) -> Result<(), Box<dyn Error>> {
|
||||||
for i in 1..=2 {
|
for i in 1..=2 {
|
||||||
println!("Loop {} start", i);
|
println!("Loop {} start", i);
|
||||||
println!("Run {}", i);
|
println!("Run {}", i);
|
||||||
@@ -124,4 +117,25 @@ mod tests {
|
|||||||
}
|
}
|
||||||
Ok(())
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -41,9 +41,10 @@ impl TaskEntity {
|
|||||||
|
|
||||||
pub async fn run_blocking(&self) {
|
pub async fn run_blocking(&self) {
|
||||||
loop {
|
loop {
|
||||||
|
self.status.sleeper.read().unwrap().start();
|
||||||
if self.status.expected_running.load(Ordering::SeqCst) {
|
if self.status.expected_running.load(Ordering::SeqCst) {
|
||||||
self.status.sleeper.read().unwrap().start();
|
|
||||||
self.func.call().await;
|
self.func.call().await;
|
||||||
|
|
||||||
if let Some(next_execution) = self.func.get_next_execution() {
|
if let Some(next_execution) = self.func.get_next_execution() {
|
||||||
*(self.status.sleeper.write().unwrap()) = next_execution.into();
|
*(self.status.sleeper.write().unwrap()) = next_execution.into();
|
||||||
} else {
|
} else {
|
||||||
@@ -80,20 +81,33 @@ impl Runner {
|
|||||||
let list = &self.task_list.read()?;
|
let list = &self.task_list.read()?;
|
||||||
self.rt.block_on(async {
|
self.rt.block_on(async {
|
||||||
for (_, entity) in list.iter() {
|
for (_, entity) in list.iter() {
|
||||||
if !entity.status.is_running.load(Ordering::SeqCst)
|
self.run_entity(entity.clone()).await
|
||||||
&& entity.status.expected_running.load(Ordering::SeqCst)
|
|
||||||
{
|
|
||||||
entity.status.is_running.store(true, Ordering::SeqCst);
|
|
||||||
tokio::spawn({
|
|
||||||
let entity_clone = entity.clone();
|
|
||||||
async move { entity_clone.run_blocking().await }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn run_id(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||||
|
if let Some(entity) = self.task_list.read()?.get(id).cloned() {
|
||||||
|
self.rt.block_on(async { self.run_entity(entity).await });
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(IdNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run_entity(&self, entity: Arc<TaskEntity>) {
|
||||||
|
if !entity.status.is_running.load(Ordering::SeqCst)
|
||||||
|
&& entity.status.expected_running.load(Ordering::SeqCst)
|
||||||
|
{
|
||||||
|
entity.status.is_running.store(true, Ordering::SeqCst);
|
||||||
|
tokio::spawn({
|
||||||
|
let entity_clone = entity.clone();
|
||||||
|
async move { entity_clone.run_blocking().await }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_with_id(
|
pub fn add_with_id(
|
||||||
&self,
|
&self,
|
||||||
task: Arc<Box<dyn CronTask>>,
|
task: Arc<Box<dyn CronTask>>,
|
||||||
@@ -134,11 +148,12 @@ impl Runner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn terminate_remove(&self, id: Uuid) -> Result<Arc<Box<dyn CronTask>>, SchedulerError> {
|
pub fn terminate_remove(&self, id: Uuid) -> Result<Arc<Box<dyn CronTask>>, SchedulerError> {
|
||||||
match self.task_list.read()?.get(&id) {
|
let mut list = self.task_list.write()?;
|
||||||
|
match list.get(&id) {
|
||||||
None => Err(IdNotFound),
|
None => Err(IdNotFound),
|
||||||
Some(task) => {
|
Some(task) => {
|
||||||
task.terminate();
|
task.terminate();
|
||||||
let func = &self.task_list.write()?.remove(&id).unwrap().func;
|
let func = list.remove(&id).unwrap().func.clone();
|
||||||
Ok(func.clone())
|
Ok(func.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user