From be708caea5d6a69d524dc0ae9d24b6c0925567b6 Mon Sep 17 00:00:00 2001 From: "nieznany.sprawiciel" Date: Fri, 24 Nov 2023 17:46:58 +0100 Subject: [PATCH] Remove ya-utils-scheduler --- Cargo.lock | 7 -- Cargo.toml | 1 - utils/scheduler/Cargo.toml | 8 -- utils/scheduler/examples/scheduler.rs | 20 ---- utils/scheduler/src/lib.rs | 124 ------------------------ utils/scheduler/src/task.rs | 29 ------ utils/scheduler/src/trigger.rs | 131 -------------------------- 7 files changed, 320 deletions(-) delete mode 100644 utils/scheduler/Cargo.toml delete mode 100644 utils/scheduler/examples/scheduler.rs delete mode 100644 utils/scheduler/src/lib.rs delete mode 100644 utils/scheduler/src/task.rs delete mode 100644 utils/scheduler/src/trigger.rs diff --git a/Cargo.lock b/Cargo.lock index f04354bd6e..5c7a5ceb28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8953,13 +8953,6 @@ dependencies = [ "tokio 1.34.0", ] -[[package]] -name = "ya-utils-scheduler" -version = "0.1.0" -dependencies = [ - "chrono", -] - [[package]] name = "ya-version" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 832b8ec4a7..84ba8a0223 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -215,7 +215,6 @@ members = [ "utils/path", "utils/process", "utils/std-utils", - "utils/scheduler", "utils/transfer", "utils/diesel-utils", "utils/fd-metrics", diff --git a/utils/scheduler/Cargo.toml b/utils/scheduler/Cargo.toml deleted file mode 100644 index c75380bb32..0000000000 --- a/utils/scheduler/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "ya-utils-scheduler" -version = "0.1.0" -authors = ["Golem Factory "] -edition = "2018" - -[dependencies] -chrono = "0.4" diff --git a/utils/scheduler/examples/scheduler.rs b/utils/scheduler/examples/scheduler.rs deleted file mode 100644 index 2d62bd9bb1..0000000000 --- a/utils/scheduler/examples/scheduler.rs +++ /dev/null @@ -1,20 +0,0 @@ -use chrono::Local; -use ya_utils_scheduler::{Interval, Scheduler, Task, Trigger}; - -fn do_void(number: i64) { - println!("Number is: {}", number) -} - -fn main() { - let mut scheduler = Scheduler::new("Scheduler", 50u64); - let task1 = Task::new("do_void_1", || do_void(69)); - let interval1 = Interval::new(0, 0, 1, 1); - let trigger1 = Trigger::new("trigger_1", Local::now(), interval1); - scheduler.schedule_task(task1, trigger1); - - let interval2 = Interval::new(0, 0, 0, 1); - let trigger2 = Trigger::new("trigger_2", Local::now(), interval2); - let task2 = Task::new("do_void2", || do_void(6969)); - scheduler.schedule_task(task2, trigger2); - scheduler.start(); -} diff --git a/utils/scheduler/src/lib.rs b/utils/scheduler/src/lib.rs deleted file mode 100644 index 0f86f641f2..0000000000 --- a/utils/scheduler/src/lib.rs +++ /dev/null @@ -1,124 +0,0 @@ -use std::thread; -use std::time::Duration; - -mod task; -mod trigger; - -pub use task::Task; -pub use trigger::{Interval, Trigger}; - -pub struct Job { - task: Task, - trigger: Trigger, -} - -impl Job { - fn new(task: Task, trigger: Trigger) -> Job { - Job { task, trigger } - } - - fn is_pending(&self) -> bool { - self.trigger.is_ready() - } - - fn execute(&mut self) { - self.task.execute(); - self.trigger.tick(); - } -} - -pub struct Scheduler { - name: String, - tick_time: u64, - jobs: Vec, -} - -impl Scheduler { - pub fn new(name: T, tick_time: u64) -> Scheduler - where - T: Into, - { - Scheduler { - name: name.into(), - tick_time, - jobs: vec![], - } - } - - pub fn schedule_task(&mut self, task: Task, trigger: Trigger) { - println!("Scheduled task: {:?} with trigger: {:?}", task, trigger); - let job = Job::new(task, trigger); - self.jobs.push(job); - } - - pub fn start(&mut self) { - println!("Scheduler started"); - loop { - self.run_pending(); - thread::sleep(Duration::from_millis(self.tick_time)); - } - } - - fn run_pending(&mut self) { - for job in &mut self.jobs { - if job.is_pending() { - job.execute(); - } - } - } - - pub fn shutdown(&mut self) { - println!("Scheduler stopped"); - } - - pub fn status(&self) { - println!("Current status of: {}", self.name); - } -} - -#[cfg(test)] -mod tests { - use super::*; - use chrono::{Duration, Local}; - #[test] - fn test_job_is_ready() { - let task = Task::new("task1", || println!("1")); - let interval = Interval::new(1, 0, 0, 0); - let first_run = Local::now(); - let trigger = Trigger::new("trigger1", first_run, interval); - let job = Job::new(task, trigger); - assert!(job.is_pending()); - } - - #[test] - fn test_job_is_not_ready() { - let task = Task::new("task1", || println!("1")); - let days = 1; - let interval = Interval::new(days, 0, 0, 0); - let trigger = Trigger::new("trigger1", Local::now() + Duration::days(2i64), interval); - let job = Job::new(task, trigger); - assert!(!job.is_pending()); - } - - #[test] - fn test_job_execute() { - let task = Task::new("task1", || println!("1")); - let days = 1; - let interval = Interval::new(days, 0, 0, 0); - let trigger = Trigger::new("trigger1", Local::now(), interval); - let mut job = Job::new(task, trigger); - job.execute(); - assert!(!job.is_pending()); - } - - #[test] - fn test_scheduler_schedule_task() { - let mut scheduler = Scheduler::new("Scheduler", 1u64); - let task = Task::new("task1", || println!("1")); - let interval = Interval::new(1, 0, 0, 0); - let trigger = Trigger::new("trigger1", Local::now(), interval); - assert_eq!(scheduler.jobs.len(), 0); - scheduler.schedule_task(task, trigger); - assert_eq!(scheduler.jobs.len(), 1); - } -} diff --git a/utils/scheduler/src/task.rs b/utils/scheduler/src/task.rs deleted file mode 100644 index 8499abd88e..0000000000 --- a/utils/scheduler/src/task.rs +++ /dev/null @@ -1,29 +0,0 @@ -use std::fmt; - -pub struct Task { - name: String, - task: Box, -} - -impl Task { - pub fn new(name: T, task: F) -> Task - where - T: Into, - F: 'static + FnMut() + Sync + Send, - { - Task { - name: name.into(), - task: Box::new(task), - } - } - - pub fn execute(&mut self) { - (*self.task)(); - } -} - -impl fmt::Debug for Task { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Task {{ name: {:?} }}", self.name) - } -} diff --git a/utils/scheduler/src/trigger.rs b/utils/scheduler/src/trigger.rs deleted file mode 100644 index e134ecf289..0000000000 --- a/utils/scheduler/src/trigger.rs +++ /dev/null @@ -1,131 +0,0 @@ -use chrono::prelude::{DateTime, Local, TimeZone}; -use chrono::Duration; -use std::fmt; - -pub struct Interval { - days: u32, - hours: u32, - minutes: u32, - seconds: u32, -} - -impl Interval { - pub fn new(days: u32, hours: u32, minutes: u32, seconds: u32) -> Interval { - Interval { - days, - hours, - minutes, - seconds, - } - } - - fn next(&self, from: DateTime) -> DateTime { - from + Duration::days(i64::from(self.days)) - + Duration::hours(i64::from(self.hours)) - + Duration::minutes(i64::from(self.minutes)) - + Duration::seconds(i64::from(self.seconds)) - } -} - -pub struct Trigger { - name: String, - interval: Interval, - next_run: DateTime, - last_run: Option>, -} - -impl Trigger { - pub fn new(name: T, start_from: DateTime, interval: Interval) -> Trigger - where - T: Into, - { - Trigger { - name: name.into(), - interval, - next_run: start_from, - last_run: None, - } - } - - pub fn tick(&mut self) { - let now = Local::now(); - self.next_run = self.interval.next(self.next_run); - self.last_run = Some(now); - } - - pub fn is_ready(&self) -> bool { - self.next_run <= Local::now() - } -} - -impl fmt::Debug for Trigger { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "Trigger {{ name: {:?}, next_run: {:?}, last_run: {:?} }}", - self.name, self.next_run, self.last_run - ) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use chrono::Duration; - use chrono::Local; - - #[test] - fn test_interval_next_time() { - let now = Local::now(); - let days = 1; - let hours = 2; - let minutes = 3; - let seconds = 4; - let interval = Interval::new(days, hours, minutes, seconds); - let time_with_offset = now - + Duration::days(i64::from(days)) - + Duration::hours(i64::from(hours)) - + Duration::minutes(i64::from(minutes)) - + Duration::seconds(i64::from(seconds)); - assert_eq!(interval.next(now), time_with_offset); - } - - #[test] - fn test_interval_next_time_strange_value() { - let now = Local::now(); - let strange_seconds = 121; - let seconds = strange_seconds % 60; - let minutes = strange_seconds / 60; - let strange_interval = Interval::new(0, 0, 0, strange_seconds); - let time_with_offset = - now + Duration::minutes(i64::from(minutes)) + Duration::seconds(i64::from(seconds)); - assert_eq!(strange_interval.next(now), time_with_offset); - } - - #[test] - fn test_trigger_is_ready() { - let seconds = 1; - let interval = Interval::new(0, 0, 0, seconds); - let trigger = Trigger::new("trigger1", Local::now(), interval); - assert!(trigger.is_ready()) - } - - #[test] - fn test_trigger_not_ready() { - let days = 1; - let interval = Interval::new(days, 0, 0, 0); - let trigger = Trigger::new("trigger1", Local::now() + Duration::days(1), interval); - assert!(!trigger.is_ready()); - } - - #[test] - fn test_trigger_tick() { - let days = 1; - let now = Local::now(); - let interval = Interval::new(days, 0, 0, 0); - let mut trigger = Trigger::new("trigger1", now, interval); - trigger.tick(); - assert_ne!(trigger.last_run, None); - assert_eq!(trigger.next_run, now + Duration::days(i64::from(days))); - } -}