Skip to content

Commit

Permalink
Merge pull request #2 from calebpitan/feat/scheduler
Browse files Browse the repository at this point in the history
Enhancements to core scheduler modules
  • Loading branch information
calebpitan authored Nov 6, 2024
2 parents 923a9ce + d49f0dc commit 2eb4be3
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 57 deletions.
7 changes: 7 additions & 0 deletions packages/scheduler/src/core/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::core::frequency::{StFrequency, StRegularFrequency};
use crate::core::priority::StPriority;
use crate::core::time::{parse_cron_expr, utc_timestamp, Timestamp};
use crate::core::time::{DAY_MILLIS, HOUR_MILLIS, WEEK_MILLIS};
use crate::traits::ID;

#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -21,6 +22,12 @@ pub struct StSchedule {
frequency: Option<StFrequency>,
}

impl ID for StSchedule {
fn get_id(&self) -> String {
self.get_id()
}
}

#[wasm_bindgen]
impl StSchedule {
#[wasm_bindgen(constructor)]
Expand Down
35 changes: 9 additions & 26 deletions packages/scheduler/src/core/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
use std::marker::PhantomData;

use async_std::task;
use wasm_bindgen::prelude::*;

use crate::console_log;
use crate::queue::priority_queue::{Comparator, PriorityQueue};
use crate::core::schedule::StSchedule;
use crate::core::time::utc_timestamp;
use crate::core::time::Timestamp;

pub struct ClosureComparator<F, T>
where
F: Fn(&T, &T) -> bool,
{
func: F,
_marker: PhantomData<T>,
}

impl<F, T> Comparator<T> for ClosureComparator<F, T>
where
F: Fn(&T, &T) -> bool,
{
fn compare(&self, a: &T, b: &T) -> bool {
(self.func)(a, b)
}
}
use crate::queue::priority_queue::PQComparator;
use crate::queue::priority_queue::PriorityQueue;

#[wasm_bindgen]
pub struct StScheduler {
Expand All @@ -42,10 +24,7 @@ pub struct StSchedulerRunner {
#[wasm_bindgen]
impl StScheduler {
pub fn new() -> StScheduler {
let comparator = ClosureComparator {
func: |a: &StSchedule, b: &StSchedule| a < b,
_marker: PhantomData,
};
let comparator = PQComparator::new(|a: &StSchedule, b: &StSchedule| a < b);

StScheduler {
pq: PriorityQueue::new(Box::new(comparator)),
Expand Down Expand Up @@ -87,7 +66,7 @@ impl StScheduler {
self.receiver = Some(receiver)
}

pub fn abort(&mut self) {
fn abort(&mut self) {
if self._aborted == false {
console_log!("scheduler aborted!");
self._aborted = true;
Expand All @@ -107,11 +86,15 @@ impl StScheduler {

pub async fn run(&mut self) {
self.unabort();
let sleep_ts = Timestamp::Millis(1000);
let sleep_ts = Timestamp::Millis(900);

console_log!("scheduler running");

// let arc = Arc::new(Mutex::new(self));

loop {
// let self = &mut arc.lock().unwrap();

if self.isaborted() {
break;
}
Expand Down
18 changes: 1 addition & 17 deletions packages/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod utils;

pub mod core;
pub mod queue;
pub mod traits;

use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -63,20 +64,3 @@ pub fn st_const_weekday_from_value(value: u8) -> StConstWeekday {
pub fn st_ordinals_from_value(value: u8) -> StOrdinals {
StOrdinals::from_value(&value)
}

// macro_rules! doc_inherited {
// ($name:ident) => {
// #[doc = "This function performs some operation."]
// pub fn $name() {
// println!("{} called", stringify!($name));
// }
// };
// }

// doc_inherited!(function_a);
// doc_inherited!(function_b);

// fn main() {
// function_a(); // Output: function_a called
// function_b(); // Output: function_b called
// }
Loading

0 comments on commit 2eb4be3

Please sign in to comment.