Skip to content

Commit c0d1fc9

Browse files
authored
Rename Watchdog to Taskchain (#90)
* refactor: rename Watchdog -> Taskchain * remove: Taskchain subscribers/event
1 parent 94eae37 commit c0d1fc9

File tree

4 files changed

+44
-71
lines changed

4 files changed

+44
-71
lines changed

src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pub mod discord;
22
pub mod service; // Will be fixed when lum gets seperated into multiple workspaces
33
pub mod service_manager;
4+
pub mod taskchain;
45
pub mod types;
5-
pub mod watchdog;
66

77
pub use service::{Service, ServiceInfo};
88
pub use service_manager::{ServiceManager, ServiceManagerBuilder};
9+
pub use taskchain::Taskchain;
910
pub use types::{
1011
BoxedError, LifetimedPinnedBoxedFuture, LifetimedPinnedBoxedFutureResult, OverallStatus,
1112
PinnedBoxedFuture, PinnedBoxedFutureResult, Priority, ShutdownError, StartupError, Status,
1213
};
13-
pub use watchdog::Watchdog;

src/service/service_manager.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use super::{
22
service::Service,
33
types::{OverallStatus, Priority, ShutdownError, StartupError, Status},
44
};
5-
use crate::{event::EventRepeater, service::Watchdog};
5+
use crate::{event::EventRepeater, service::Taskchain};
66
use log::{error, info, warn};
77
use std::{
88
collections::HashMap,
9+
error::Error,
910
fmt::{self, Display},
1011
mem,
1112
sync::{Arc, OnceLock, Weak},
@@ -81,7 +82,7 @@ impl ServiceManagerBuilder {
8182

8283
pub struct ServiceManager {
8384
weak: OnceLock<Weak<Self>>,
84-
background_tasks: Mutex<HashMap<String, JoinHandle<()>>>,
85+
background_tasks: Mutex<HashMap<String, JoinHandle<Result<(), Box<dyn Error + Send + Sync>>>>>,
8586

8687
pub services: Vec<Arc<Mutex<dyn Service>>>,
8788
pub on_status_change: Arc<EventRepeater<Status>>,
@@ -450,13 +451,9 @@ impl ServiceManager {
450451

451452
let task = service_lock.task();
452453
if let Some(task) = task {
453-
let mut watchdog = Watchdog::new(task);
454+
let mut taskchain = Taskchain::new(task);
454455

455-
watchdog.append(|result| async move {
456-
/*
457-
We technically only need a read lock here, but we want to immediately stop
458-
other services from accessing the service, so we acquire a write lock instead.
459-
*/
456+
taskchain.append(|result| async move {
460457
let service = service.lock().await;
461458

462459
match result {
@@ -492,7 +489,7 @@ impl ServiceManager {
492489
Ok(())
493490
});
494491

495-
let join_handle = spawn(watchdog.run());
492+
let join_handle = spawn(taskchain.run());
496493

497494
self.background_tasks
498495
.lock()

src/service/taskchain.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use core::mem;
2+
use std::future::Future;
3+
4+
use super::LifetimedPinnedBoxedFuture;
5+
6+
pub struct Taskchain<'a, T: Send + Sync + 'static> {
7+
task: LifetimedPinnedBoxedFuture<'a, T>,
8+
}
9+
10+
impl<'a, T: Send + Sync + 'static> Taskchain<'a, T> {
11+
pub fn new(task: LifetimedPinnedBoxedFuture<'a, T>) -> Self {
12+
Self { task }
13+
}
14+
15+
pub fn append<FN, FUT>(&mut self, task: FN)
16+
where
17+
FN: FnOnce(T) -> FUT + Send + Sync + 'a,
18+
FUT: Future<Output = T> + Send + Sync + 'a,
19+
{
20+
let previous_task = mem::replace(
21+
&mut self.task,
22+
Box::pin(async { unreachable!("Undefined Taskchain task") }),
23+
);
24+
25+
let task = async move {
26+
let result = previous_task.await;
27+
task(result).await
28+
};
29+
30+
self.task = Box::pin(task);
31+
}
32+
33+
pub async fn run(self) -> T {
34+
self.task.await
35+
}
36+
}

src/service/watchdog.rs

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)