2024-12-01 16:17:51 +01:00
|
|
|
use core::fmt;
|
2024-10-27 11:19:26 +01:00
|
|
|
|
2024-12-01 21:26:38 +01:00
|
|
|
pub trait Task: Send {
|
2024-10-27 11:19:26 +01:00
|
|
|
fn tick(&mut self) {}
|
|
|
|
fn start(&mut self) {}
|
|
|
|
fn stop(&mut self) {}
|
2024-11-02 15:18:15 +01:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
core::any::type_name::<Self>()
|
|
|
|
}
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-12-01 21:07:09 +01:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
enum ScheduledState {
|
|
|
|
Stopped,
|
|
|
|
Start,
|
|
|
|
Running,
|
|
|
|
Stop
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ScheduledTask {
|
2024-12-01 21:07:09 +01:00
|
|
|
state: ScheduledState,
|
2024-10-27 15:14:28 +01:00
|
|
|
task: Box<dyn Task>,
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-12-01 21:07:27 +01:00
|
|
|
impl core::fmt::Debug for ScheduledTask {
|
2024-10-27 11:19:26 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("ScheduledTask")
|
|
|
|
.field("task", &self.task.name())
|
|
|
|
.field("state", &self.state)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ScheduledTask {
|
|
|
|
fn new(task: Box<dyn Task>) -> Self {
|
|
|
|
ScheduledTask {
|
2024-12-01 21:07:09 +01:00
|
|
|
state: ScheduledState::Start,
|
2024-10-27 15:14:28 +01:00
|
|
|
task: task,
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&mut self) {
|
2024-12-01 21:07:09 +01:00
|
|
|
self.state = match self.state {
|
|
|
|
ScheduledState::Stopped => ScheduledState::Start,
|
|
|
|
ScheduledState::Stop => ScheduledState::Running,
|
|
|
|
_ => self.state
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) {
|
2024-12-01 21:07:09 +01:00
|
|
|
self.state = match self.state {
|
|
|
|
ScheduledState::Running => ScheduledState::Stop,
|
|
|
|
ScheduledState::Start => ScheduledState::Stopped,
|
|
|
|
_ => self.state
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tick(&mut self) {
|
2024-12-01 21:07:09 +01:00
|
|
|
self.state = match self.state {
|
|
|
|
ScheduledState::Start => {
|
|
|
|
log::info!("Starting task {}", self.task.name());
|
|
|
|
self.task.start();
|
|
|
|
ScheduledState::Running
|
|
|
|
},
|
|
|
|
ScheduledState::Running => {
|
|
|
|
self.task.tick();
|
|
|
|
ScheduledState::Running
|
|
|
|
},
|
|
|
|
ScheduledState::Stop => {
|
|
|
|
log::info!("Stopping task {}", self.task.name());
|
|
|
|
self.task.stop();
|
|
|
|
ScheduledState::Stopped
|
|
|
|
},
|
|
|
|
ScheduledState::Stopped => ScheduledState::Stopped
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 15:21:30 +01:00
|
|
|
#[derive(Debug)]
|
2024-10-27 11:19:26 +01:00
|
|
|
pub struct Scheduler {
|
2024-10-27 15:14:28 +01:00
|
|
|
tasks: Vec<ScheduledTask>,
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scheduler {
|
|
|
|
pub fn new(tasks: Vec<Box<dyn Task>>) -> Self {
|
|
|
|
let mut scheduled = Vec::new();
|
|
|
|
for task in tasks {
|
2024-11-02 15:21:30 +01:00
|
|
|
log::info!("Scheduling task {} {:?}", task.name(), task);
|
2024-10-27 11:19:26 +01:00
|
|
|
scheduled.push(ScheduledTask::new(task));
|
|
|
|
}
|
|
|
|
Scheduler {
|
|
|
|
tasks: scheduled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) {
|
|
|
|
for task in &mut self.tasks {
|
|
|
|
task.tick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|