Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Rust API): Implement Rust Cron Module #124

Merged
merged 32 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
30c491e
feat: implement mkcron
saibatizoku Feb 11, 2024
d624691
feat: filter and dedup cron_times
saibatizoku Feb 12, 2024
39351d4
fix: lints
saibatizoku Feb 12, 2024
762b83d
fix: more lints, tidy up
saibatizoku Feb 12, 2024
59dfec8
fix: remove unwrap, add docstring with tests
saibatizoku Feb 12, 2024
52003ed
fix: update mkcron to merge overlapping ranges
saibatizoku Feb 13, 2024
a82cd76
feat: implement cron add and ls
saibatizoku Feb 13, 2024
f9f87a2
fix: lints by allowing expect_used
saibatizoku Feb 13, 2024
a06c281
doc: add some documentation examples
saibatizoku Feb 14, 2024
e710a16
fix: updates to use new crate module hierarchy
saibatizoku Feb 14, 2024
6ed6192
Merge remote-tracking branch 'origin/main' into feat/cron-api-rust
saibatizoku Feb 15, 2024
817e13a
deps: add time crate
saibatizoku Feb 15, 2024
77923ab
feat: implement cron 'delay'
saibatizoku Feb 15, 2024
233b6b5
fix: doctest dependencies
saibatizoku Feb 15, 2024
fa75d1e
fix: lints
saibatizoku Feb 16, 2024
d8f38ba
fix: lints
saibatizoku Feb 16, 2024
0b083d2
chore: refactor code to keep convention
saibatizoku Feb 16, 2024
6c392ec
chore: refactor code for testability, improve docstrings
saibatizoku Feb 16, 2024
746002a
test: add unit tests for cron_time_to_cron_schedule
saibatizoku Feb 16, 2024
19bed23
test: add unit tests for cron functions
saibatizoku Feb 16, 2024
bf44d37
fix: merging overlapping components
saibatizoku Feb 16, 2024
0856f52
chore: cleanup
saibatizoku Feb 16, 2024
8bd4d81
fix: lints
saibatizoku Feb 16, 2024
879f40f
fix: docs for CronComponent::merge method
saibatizoku Feb 16, 2024
7239ac4
test: add testing for mkdelay_crontab
saibatizoku Feb 16, 2024
888f5d9
tests: allow unwrap_used in cron tests
saibatizoku Feb 17, 2024
9ca2e62
fix: more lints
saibatizoku Feb 17, 2024
4dfaeae
chore: specify delay with i64
saibatizoku Feb 19, 2024
27c1b97
fix: remove sorts and simplify iterations
saibatizoku Feb 20, 2024
0869e75
fix: fix merge to detect overlapping ranges
saibatizoku Feb 20, 2024
fb7d941
fix: tidy up
saibatizoku Feb 20, 2024
2c7b8bf
docs: add return types for helper functions
saibatizoku Feb 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ miniprotocols
mithril
mitigations
mkcron
mkdelay
mkdirat
moderations
multiera
Expand Down
1 change: 1 addition & 0 deletions hermes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ anyhow = "1.0.71"
blake2b_simd = "1.0.2"
hex-literal = "0.4.1"
thiserror = "1.0.56"
time = "0.3.34"
1 change: 1 addition & 0 deletions hermes/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ anyhow = { workspace = true }
blake2b_simd = { workspace = true }
hex-literal = { workspace = true }
thiserror = { workspace = true }
time = { workspace = true }
54 changes: 39 additions & 15 deletions hermes/bin/src/runtime_extensions/hermes/cron/host.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! Cron host implementation for WASM runtime.

use crate::{
runtime_extensions::bindings::{
hermes::cron::api::{CronEventTag, CronSched, CronTagged, CronTime, Host},
wasi::clocks::monotonic_clock::Instant,
runtime_extensions::{
bindings::{
hermes::cron::api::{CronEventTag, CronSched, CronTagged, CronTime, Host},
wasi::clocks::monotonic_clock::Instant,
},
hermes::cron::{mkcron_impl, mkdelay_crontab, CronTab},
},
state::HermesState,
};
Expand Down Expand Up @@ -33,8 +36,12 @@ impl Host for HermesState {
/// If the crontab entry already exists, the retrigger flag can be changed by calling
/// this function. This could be useful where a retriggering crontab event is desired
/// to be stopped, but ONLY after it has triggered once more.
fn add(&mut self, _entry: CronTagged, _retrigger: bool) -> wasmtime::Result<bool> {
todo!()
fn add(&mut self, entry: CronTagged, retrigger: bool) -> wasmtime::Result<bool> {
self.hermes
.cron
.crontabs
.insert(entry.tag.clone(), CronTab { entry, retrigger });
Ok(true)
}

/// # Schedule A Single cron event after a fixed delay.
Expand All @@ -60,8 +67,10 @@ impl Host for HermesState {
/// It is added as a non-retriggering event.
/// Listing the crontabs after this call will list the delay in addition to all other
/// crontab entries.
fn delay(&mut self, _duration: Instant, _tag: CronEventTag) -> wasmtime::Result<bool> {
todo!()
fn delay(&mut self, duration: Instant, tag: CronEventTag) -> wasmtime::Result<bool> {
let crontab = mkdelay_crontab(duration, tag)?;
self.add(crontab, false)?;
Ok(true)
}

/// # List currently active cron schedule.
Expand All @@ -81,8 +90,21 @@ impl Host for HermesState {
/// may times before a later one.
/// - `0` - `cron-tagged` - The Tagged crontab event.
/// - `1` - `bool` - The state of the retrigger flag.
fn ls(&mut self, _tag: Option<CronEventTag>) -> wasmtime::Result<Vec<(CronTagged, bool)>> {
todo!()
fn ls(&mut self, tag: Option<CronEventTag>) -> wasmtime::Result<Vec<(CronTagged, bool)>> {
if let Some(tag) = tag {
match self.hermes.cron.crontabs.get(&tag) {
Some(cron) => Ok(vec![(cron.entry.clone(), cron.retrigger)]),
None => Ok(vec![]),
}
} else {
Ok(self
.hermes
.cron
.crontabs
.values()
.map(|cron| (cron.entry.clone(), cron.retrigger))
.collect())
}
}

/// # Remove the requested crontab.
Expand All @@ -98,13 +120,16 @@ impl Host for HermesState {
///
/// - `true`: The requested crontab was deleted and will not trigger.
/// - `false`: The requested crontab does not exist.
fn rm(&mut self, _entry: CronTagged) -> wasmtime::Result<bool> {
todo!()
fn rm(&mut self, entry: CronTagged) -> wasmtime::Result<bool> {
match self.hermes.cron.crontabs.remove(&entry.tag) {
Some(_) => Ok(true),
None => Ok(false),
}
}

/// # Make a crontab entry from individual time values.
///
/// Crates the properly formatted cron entry
/// Creates the properly formatted cron entry
/// from numeric cron time components.
/// Convenience function to make building cron strings simpler when they are
/// calculated from data.
Expand All @@ -129,9 +154,8 @@ impl Host for HermesState {
/// - For example specifying a `month` as `3` and `2-4` will
/// remove the individual month and only produce the range.
fn mkcron(
&mut self, _dow: CronTime, _month: CronTime, _day: CronTime, _hour: CronTime,
_minute: CronTime,
&mut self, dow: CronTime, month: CronTime, day: CronTime, hour: CronTime, minute: CronTime,
) -> wasmtime::Result<CronSched> {
todo!()
Ok(mkcron_impl(&dow, &month, &day, &hour, &minute))
}
}