Skip to content

Commit b482bec

Browse files
committed
feat: init cron adapter
1 parent 49a6a44 commit b482bec

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

adapter/cron/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "cron"
3+
version.workspace = true
4+
edition.workspace = true
5+
6+
[dependencies]
7+
tokio = {workspace = true}
8+
cron-parser = {workspace = true}
9+
chrono = {workspace = true}
10+
cron = {workspace = true}
11+
async-nats = {workspace = true}
12+
base = {workspace = true}
13+
tucana = {workspace = true}
14+
async-trait = {workspace = true}
15+
anyhow = {workspace = true}

adapter/cron/src/main.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::str::FromStr;
2+
use async_trait::async_trait;
3+
use chrono::{Local, Utc};
4+
use cron::Schedule;
5+
use base::extract_flow_setting_field;
6+
use base::runner::{ServerContext, ServerRunner};
7+
use base::traits::{IdentifiableFlow, LoadConfig, Server};
8+
9+
#[derive(Default)]
10+
struct Cron {
11+
}
12+
13+
#[derive(Clone)]
14+
struct CronConfig {}
15+
16+
impl LoadConfig for CronConfig {
17+
fn load() -> Self {
18+
Self {}
19+
}
20+
}
21+
22+
#[tokio::main]
23+
async fn main() {
24+
let server = Cron::default();
25+
let runner = ServerRunner::new(server).await.unwrap();
26+
runner.serve().await.unwrap();
27+
}
28+
29+
struct Time {
30+
utc: Utc
31+
}
32+
33+
impl IdentifiableFlow for Time {
34+
fn identify(&self, flow: &tucana::shared::ValidationFlow) -> bool {
35+
let Some(minute) = extract_flow_setting_field(&flow.settings, "CRON_MINUTE", "minute") else {
36+
return false;
37+
};
38+
let Some(hour) = extract_flow_setting_field(&flow.settings, "CRON_MOUR", "hour") else {
39+
return false;
40+
};
41+
let Some(dom) = extract_flow_setting_field(&flow.settings, "CRON_DAY_OF_MONTH", "day_of_month") else {
42+
return false;
43+
};
44+
let Some(month) = extract_flow_setting_field(&flow.settings, "CRON_MONTH", "month") else {
45+
return false;
46+
};
47+
let Some(dow) = extract_flow_setting_field(&flow.settings, "CRON_DAY_OF_WEEK", "day_of_week") else {
48+
return false;
49+
};
50+
51+
let expression = format!("{} {} {} {} {}", minute, hour, dom, month, dow);
52+
53+
54+
55+
56+
todo!()
57+
}
58+
}
59+
60+
#[async_trait]
61+
impl Server<CronConfig> for Cron {
62+
async fn init(&mut self, _ctx: &ServerContext<CronConfig>) -> anyhow::Result<()> {
63+
Ok(())
64+
}
65+
66+
async fn run(&mut self, ctx: &ServerContext<CronConfig>) -> anyhow::Result<()> {
67+
let expression = "0 * * * * *";
68+
let schedule = Schedule::from_str(expression).expect("Failed to parse CRON expression");
69+
70+
loop {
71+
let now = Utc::now();
72+
if let Some(next) = schedule.upcoming(Utc).take(1).next() {
73+
let until_next = next - now;
74+
tokio::time::sleep(until_next.to_std()?).await;
75+
println!(
76+
"Running every minute. Current time: {}",
77+
Local::now().format("%Y-%m-%d %H:%M:%S")
78+
);
79+
}
80+
}
81+
Ok(())
82+
}
83+
84+
async fn shutdown(&mut self, _ctx: &ServerContext<CronConfig>) -> anyhow::Result<()> {
85+
Ok(())
86+
}
87+
}

0 commit comments

Comments
 (0)