2025-11-22 12:26:16 -03:00
|
|
|
use crate::shared::models::TriggerKind;
|
2025-10-11 20:25:08 -03:00
|
|
|
use diesel::prelude::*;
|
2025-11-22 12:26:16 -03:00
|
|
|
use log::trace;
|
2025-10-06 10:30:17 -03:00
|
|
|
use serde_json::{json, Value};
|
2025-11-02 19:32:25 -03:00
|
|
|
use uuid::Uuid;
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
pub fn parse_natural_schedule(input: &str) -> Result<String, String> {
|
|
|
|
|
let input = input.trim().to_lowercase();
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let parts: Vec<&str> = input.split_whitespace().collect();
|
|
|
|
|
if parts.len() == 5 && is_cron_expression(&parts) {
|
|
|
|
|
return Ok(input);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
parse_natural_language(&input)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_cron_expression(parts: &[&str]) -> bool {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
parts.iter().all(|part| {
|
|
|
|
|
part.chars()
|
|
|
|
|
.all(|c| c.is_ascii_digit() || c == '*' || c == '/' || c == '-' || c == ',')
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_natural_language(input: &str) -> Result<String, String> {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let input = input
|
|
|
|
|
.replace("every ", "every_")
|
|
|
|
|
.replace(" at ", "_at_")
|
|
|
|
|
.replace(" from ", "_from_")
|
|
|
|
|
.replace(" to ", "_to_")
|
|
|
|
|
.replace(" during ", "_during_");
|
|
|
|
|
|
|
|
|
|
let input = input.trim();
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(cron) = parse_simple_interval(input) {
|
|
|
|
|
return Ok(cron);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(cron) = parse_at_time(input) {
|
|
|
|
|
return Ok(cron);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(cron) = parse_day_pattern(input) {
|
|
|
|
|
return Ok(cron);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(cron) = parse_combined_pattern(input) {
|
|
|
|
|
return Ok(cron);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(cron) = parse_business_hours(input) {
|
|
|
|
|
return Ok(cron);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(format!(
|
|
|
|
|
"Could not parse schedule '{}'. Use patterns like 'every hour', 'every 5 minutes', \
|
|
|
|
|
'at 9am', 'every monday at 9am', 'weekdays at 8am', or raw cron '0 * * * *'",
|
|
|
|
|
input.replace('_', " ")
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_simple_interval(input: &str) -> Option<String> {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if input == "every_minute" || input == "every_1_minute" {
|
|
|
|
|
return Some("* * * * *".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(rest) = input.strip_prefix("every_") {
|
|
|
|
|
if let Some(num_str) = rest.strip_suffix("_minutes") {
|
|
|
|
|
if let Ok(n) = num_str.parse::<u32>() {
|
|
|
|
|
if n > 0 && n <= 59 {
|
|
|
|
|
return Some(format!("*/{} * * * *", n));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if rest == "hour" || rest == "1_hour" {
|
|
|
|
|
return Some("0 * * * *".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(num_str) = rest.strip_suffix("_hours") {
|
|
|
|
|
if let Ok(n) = num_str.parse::<u32>() {
|
|
|
|
|
if n > 0 && n <= 23 {
|
|
|
|
|
return Some(format!("0 */{} * * *", n));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if rest == "day" {
|
|
|
|
|
return Some("0 0 * * *".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if rest == "week" {
|
|
|
|
|
return Some("0 0 * * 0".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if rest == "month" {
|
|
|
|
|
return Some("0 0 1 * *".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if rest == "year" {
|
|
|
|
|
return Some("0 0 1 1 *".to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
match input {
|
|
|
|
|
"daily" => Some("0 0 * * *".to_string()),
|
|
|
|
|
"weekly" => Some("0 0 * * 0".to_string()),
|
|
|
|
|
"monthly" => Some("0 0 1 * *".to_string()),
|
|
|
|
|
"yearly" | "annually" => Some("0 0 1 1 *".to_string()),
|
|
|
|
|
"hourly" => Some("0 * * * *".to_string()),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_at_time(input: &str) -> Option<String> {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let time_str = if input.starts_with("_at_") {
|
|
|
|
|
&input[4..]
|
|
|
|
|
} else if input.starts_with("at_") {
|
|
|
|
|
&input[3..]
|
|
|
|
|
} else {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
parse_time_to_cron(time_str, "*", "*")
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 21:09:43 -03:00
|
|
|
fn parse_time_to_cron(time_str: &str, _hour_default: &str, dow: &str) -> Option<String> {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if time_str == "midnight" {
|
|
|
|
|
return Some(format!("0 0 * * {}", dow));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if time_str == "noon" {
|
|
|
|
|
return Some(format!("0 12 * * {}", dow));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let (hour, minute) = parse_time_value(time_str)?;
|
|
|
|
|
|
|
|
|
|
Some(format!("{} {} * * {}", minute, hour, dow))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_time_value(time_str: &str) -> Option<(u32, u32)> {
|
|
|
|
|
let time_str = time_str.trim();
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let (time_part, is_pm) = if time_str.ends_with("am") {
|
|
|
|
|
(&time_str[..time_str.len() - 2], false)
|
|
|
|
|
} else if time_str.ends_with("pm") {
|
|
|
|
|
(&time_str[..time_str.len() - 2], true)
|
|
|
|
|
} else {
|
|
|
|
|
(time_str, false)
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let (hour, minute) = if time_part.contains(':') {
|
|
|
|
|
let parts: Vec<&str> = time_part.split(':').collect();
|
|
|
|
|
if parts.len() != 2 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let h: u32 = parts[0].parse().ok()?;
|
|
|
|
|
let m: u32 = parts[1].parse().ok()?;
|
|
|
|
|
(h, m)
|
|
|
|
|
} else {
|
|
|
|
|
let h: u32 = time_part.parse().ok()?;
|
|
|
|
|
(h, 0)
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if minute > 59 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let hour = if is_pm && hour < 12 {
|
|
|
|
|
hour + 12
|
|
|
|
|
} else if !is_pm && hour == 12 && time_str.ends_with("am") {
|
|
|
|
|
0
|
|
|
|
|
} else {
|
|
|
|
|
hour
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if hour > 23 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some((hour, minute))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_day_pattern(input: &str) -> Option<String> {
|
|
|
|
|
let dow = get_day_of_week(input)?;
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(at_pos) = input.find("_at_") {
|
|
|
|
|
let time_str = &input[at_pos + 4..];
|
|
|
|
|
return parse_time_to_cron(time_str, "0", &dow.to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
Some(format!("0 0 * * {}", dow))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_day_of_week(input: &str) -> Option<String> {
|
|
|
|
|
let input_lower = input.to_lowercase();
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let day_part = input_lower.strip_prefix("every_").unwrap_or(&input_lower);
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let day_part = if let Some(at_pos) = day_part.find("_at_") {
|
|
|
|
|
&day_part[..at_pos]
|
|
|
|
|
} else {
|
|
|
|
|
day_part
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match day_part {
|
|
|
|
|
"sunday" | "sun" => Some("0".to_string()),
|
|
|
|
|
"monday" | "mon" => Some("1".to_string()),
|
|
|
|
|
"tuesday" | "tue" | "tues" => Some("2".to_string()),
|
|
|
|
|
"wednesday" | "wed" => Some("3".to_string()),
|
|
|
|
|
"thursday" | "thu" | "thurs" => Some("4".to_string()),
|
|
|
|
|
"friday" | "fri" => Some("5".to_string()),
|
|
|
|
|
"saturday" | "sat" => Some("6".to_string()),
|
|
|
|
|
"weekday" | "weekdays" => Some("1-5".to_string()),
|
|
|
|
|
"weekend" | "weekends" => Some("0,6".to_string()),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_combined_pattern(input: &str) -> Option<String> {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if input.starts_with("every_day_at_") {
|
|
|
|
|
let time_str = &input[13..];
|
|
|
|
|
return parse_time_to_cron(time_str, "0", "*");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if input.starts_with("every_weekday_at_") || input.starts_with("weekdays_at_") {
|
|
|
|
|
let time_str = if input.starts_with("every_weekday_at_") {
|
|
|
|
|
&input[17..]
|
|
|
|
|
} else {
|
|
|
|
|
&input[12..]
|
|
|
|
|
};
|
|
|
|
|
return parse_time_to_cron(time_str, "0", "1-5");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if input.starts_with("every_weekend_at_") || input.starts_with("weekends_at_") {
|
|
|
|
|
let time_str = if input.starts_with("every_weekend_at_") {
|
|
|
|
|
&input[17..]
|
|
|
|
|
} else {
|
|
|
|
|
&input[12..]
|
|
|
|
|
};
|
|
|
|
|
return parse_time_to_cron(time_str, "0", "0,6");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if input.starts_with("every_hour_from_") {
|
|
|
|
|
let rest = &input[16..];
|
|
|
|
|
if let Some(to_pos) = rest.find("_to_") {
|
|
|
|
|
let start: u32 = rest[..to_pos].parse().ok()?;
|
|
|
|
|
let end: u32 = rest[to_pos + 4..].parse().ok()?;
|
|
|
|
|
if start <= 23 && end <= 23 {
|
|
|
|
|
return Some(format!("0 {}-{} * * *", start, end));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_business_hours(input: &str) -> Option<String> {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if input.contains("business_hours") || input.contains("business hours") {
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
|
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if input.starts_with("every_") {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if let Some(rest) = input.strip_prefix("every_") {
|
|
|
|
|
if let Some(minutes_pos) = rest.find("_minutes") {
|
|
|
|
|
let num_str = &rest[..minutes_pos];
|
|
|
|
|
if let Ok(n) = num_str.parse::<u32>() {
|
|
|
|
|
if n > 0 && n <= 59 {
|
|
|
|
|
return Some(format!("*/{} 9-17 * * 1-5", n));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
if rest.starts_with("hour") {
|
|
|
|
|
return Some("0 9-17 * * 1-5".to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
return Some("0 9-17 * * 1-5".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
pub fn execute_set_schedule(
|
|
|
|
|
conn: &mut diesel::PgConnection,
|
2025-11-30 12:20:48 -03:00
|
|
|
cron_or_natural: &str,
|
2025-11-22 12:26:16 -03:00
|
|
|
script_name: &str,
|
|
|
|
|
bot_uuid: Uuid,
|
|
|
|
|
) -> Result<Value, Box<dyn std::error::Error>> {
|
2025-12-23 18:40:58 -03:00
|
|
|
|
2025-11-30 12:20:48 -03:00
|
|
|
let cron = parse_natural_schedule(cron_or_natural)?;
|
|
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
trace!(
|
2025-11-30 12:20:48 -03:00
|
|
|
"Scheduling SET SCHEDULE cron: {} (from: '{}'), script: {}, bot_id: {:?}",
|
2025-11-22 12:26:16 -03:00
|
|
|
cron,
|
2025-11-30 12:20:48 -03:00
|
|
|
cron_or_natural,
|
2025-11-22 12:26:16 -03:00
|
|
|
script_name,
|
|
|
|
|
bot_uuid
|
|
|
|
|
);
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
use crate::shared::models::bots::dsl::bots;
|
|
|
|
|
let bot_exists: bool = diesel::select(diesel::dsl::exists(
|
|
|
|
|
bots.filter(crate::shared::models::bots::dsl::id.eq(bot_uuid)),
|
|
|
|
|
))
|
|
|
|
|
.get_result(conn)?;
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
if !bot_exists {
|
|
|
|
|
return Err(format!("Bot with id {} does not exist", bot_uuid).into());
|
|
|
|
|
}
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
use crate::shared::models::system_automations::dsl::*;
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
let new_automation = (
|
|
|
|
|
bot_id.eq(bot_uuid),
|
|
|
|
|
kind.eq(TriggerKind::Scheduled as i32),
|
2025-11-30 12:20:48 -03:00
|
|
|
schedule.eq(&cron),
|
2025-11-22 12:26:16 -03:00
|
|
|
param.eq(script_name),
|
|
|
|
|
is_active.eq(true),
|
|
|
|
|
);
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
let update_result = diesel::update(system_automations)
|
|
|
|
|
.filter(bot_id.eq(bot_uuid))
|
|
|
|
|
.filter(kind.eq(TriggerKind::Scheduled as i32))
|
|
|
|
|
.filter(param.eq(script_name))
|
|
|
|
|
.set((
|
2025-11-30 12:20:48 -03:00
|
|
|
schedule.eq(&cron),
|
2025-11-22 12:26:16 -03:00
|
|
|
is_active.eq(true),
|
|
|
|
|
last_triggered.eq(None::<chrono::DateTime<chrono::Utc>>),
|
|
|
|
|
))
|
|
|
|
|
.execute(&mut *conn)?;
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
let result = if update_result == 0 {
|
|
|
|
|
diesel::insert_into(system_automations)
|
|
|
|
|
.values(&new_automation)
|
|
|
|
|
.execute(&mut *conn)?
|
|
|
|
|
} else {
|
|
|
|
|
update_result
|
|
|
|
|
};
|
2025-11-30 12:20:48 -03:00
|
|
|
|
2025-11-22 12:26:16 -03:00
|
|
|
Ok(json!({
|
2025-11-30 12:20:48 -03:00
|
|
|
"command": "set_schedule",
|
|
|
|
|
"schedule": cron,
|
|
|
|
|
"original_input": cron_or_natural,
|
|
|
|
|
"script": script_name,
|
|
|
|
|
"bot_id": bot_uuid.to_string(),
|
|
|
|
|
"rows_affected": result
|
2025-11-22 12:26:16 -03:00
|
|
|
}))
|
2025-10-06 10:30:17 -03:00
|
|
|
}
|