Skip to content

Commit

Permalink
refactor: implementations for structs
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpitan committed Oct 21, 2024
1 parent 845e750 commit 6df19a9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 37 deletions.
21 changes: 20 additions & 1 deletion packages/scheduler/src/scheduler/cron.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
use wasm_bindgen::prelude::*;

use super::frequency::StFrequencyType;
use crate::scheduler::frequency::StFrequencyType;

#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StCronSchedule {
expression: String,
frequency: StFrequencyType,
}

#[wasm_bindgen]
impl StCronSchedule {
#[wasm_bindgen(constructor)]
pub fn new(expression: String, frequency: StFrequencyType) -> Self {
StCronSchedule {
expression,
frequency,
}
}

pub fn get_expression(&self) -> String {
self.expression.clone()
}

pub fn get_frequency(&self) -> StFrequencyType {
self.frequency
}
}
36 changes: 29 additions & 7 deletions packages/scheduler/src/scheduler/frequency.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use wasm_bindgen::prelude::*;

use super::cron::StCronSchedule;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StFrequency {
Regular(StRegularFrequency),
Expand All @@ -22,14 +20,38 @@ pub enum StFrequencyType {
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StRegularFrequency {
r#type: StFrequencyType,
until: u64,
pub ftype: StFrequencyType,
pub until: Option<u64>,
}

#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StCustomFrequency {
r#type: StFrequencyType,
until: u64,
crons: [StCronSchedule; 3],
pub ftype: StFrequencyType,
pub until: Option<u64>,
cron_expressions: Vec<String>,
}

#[wasm_bindgen]
impl StRegularFrequency {
#[wasm_bindgen(constructor)]
pub fn new(ftype: StFrequencyType, until: Option<u64>) -> Self {
StRegularFrequency { ftype, until }
}
}

#[wasm_bindgen]
impl StCustomFrequency {
#[wasm_bindgen(constructor)]
pub fn new(until: Option<u64>, cron_expressions: Vec<String>) -> Self {
StCustomFrequency {
ftype: StFrequencyType::Custom,
until,
cron_expressions,
}
}

pub fn get_crons_expressions(&self) -> Vec<String> {
self.cron_expressions.clone()
}
}
2 changes: 1 addition & 1 deletion packages/scheduler/src/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pub mod frequency;
pub mod priority;
pub mod schedule;
pub mod scheduler;
pub mod time;
pub mod time;
84 changes: 56 additions & 28 deletions packages/scheduler/src/scheduler/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::cmp::Ordering;
use std::cmp::{max, Ordering};

use wasm_bindgen::prelude::*;

use super::{
frequency::{StCustomFrequency, StFrequency, StRegularFrequency},
priority::StPriority,
};
use crate::scheduler::frequency::{StCustomFrequency, StFrequency, StRegularFrequency};
use crate::scheduler::priority::StPriority;
use crate::scheduler::time::{parse_cron_expr, utc_now, Timestamp};

#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -75,6 +74,10 @@ impl StSchedule {
}
}

// pub(crate) fn get_id_as_str(&self) -> &str {
// self.id.as_str()
// }

pub fn get_id(&self) -> String {
self.id.clone()
}
Expand Down Expand Up @@ -111,41 +114,66 @@ impl StSchedule {
None
}

pub fn get_next_schedule(&self) -> StSchedule {
pub fn is_passed(&self) -> bool {
Timestamp::Millis(self.timestamp) < utc_now()
}

pub fn get_upcoming_schedule(&self) -> Option<StSchedule> {
// Check if the current schedule has passed and generate the next
// schedule relative to now from the StSchedule struct
let next_schedule = self
.frequency
.as_ref()
.map(|f| {
if let StFrequency::Custom(cstm_freq) = f {
StSchedule::with_custom(
&self.id,
self.timestamp,
cstm_freq.clone(),
self.priority,
)
} else if let StFrequency::Regular(reg_freq) = f {
StSchedule::with_regular(&self.id, self.timestamp, *reg_freq, self.priority)
} else {
panic!("Unreachable code")
if !self.is_passed() {
return None;
}

match &self.frequency {
Some(freq) => match freq {
StFrequency::Custom(cstm_freq) => {
let crons = cstm_freq.get_crons_expressions();
let timestamp = crons
.iter()
.take(3)
.map(|c| {
let ref_time = Timestamp::Millis(self.timestamp);
let cur_time = utc_now();
let time = parse_cron_expr(
c.as_str(),
Some(max(ref_time, cur_time)),
);

Timestamp::Millis(time.timestamp_millis() as u64)
})
.max();

timestamp.map(|t| {
StSchedule::with_custom(
&self.id,
t.to_ms(),
cstm_freq.clone(),
self.priority,
)
})
}
})
.unwrap_or(StSchedule::new(&self.id, self.timestamp, self.priority));

next_schedule
StFrequency::Regular(reg_freq) => Some(StSchedule::with_regular(
&self.id,
self.timestamp,
*reg_freq,
self.priority,
)),
},
None => None,
}
}
}

impl Ord for StSchedule {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let order = self.timestamp.cmp(&other.timestamp);

if let Ordering::Equal = order {
return self.priority.cmp(&other.priority);
match order {
Ordering::Equal => self.priority.cmp(&other.priority),
_ => order,
}

order
}
}

Expand Down

0 comments on commit 6df19a9

Please sign in to comment.