2025-10-11 20:25:08 -03:00
|
|
|
use diesel::prelude::*;
|
2025-10-06 10:30:17 -03:00
|
|
|
use log::info;
|
|
|
|
|
use serde_json::{json, Value};
|
2025-11-02 19:32:25 -03:00
|
|
|
use uuid::Uuid;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
2025-10-06 20:49:38 -03:00
|
|
|
use crate::shared::models::TriggerKind;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
|
2025-10-11 12:29:03 -03:00
|
|
|
pub fn execute_set_schedule(
|
2025-10-11 20:25:08 -03:00
|
|
|
conn: &mut diesel::PgConnection,
|
2025-10-06 10:30:17 -03:00
|
|
|
cron: &str,
|
2025-11-02 19:32:25 -03:00
|
|
|
script_name: &str,
|
|
|
|
|
bot_uuid: Uuid,
|
2025-10-06 10:30:17 -03:00
|
|
|
) -> Result<Value, Box<dyn std::error::Error>> {
|
|
|
|
|
info!(
|
2025-11-02 20:57:53 -03:00
|
|
|
"Scheduling SET SCHEDULE cron: {}, script: {}, bot_id: {:?}",
|
2025-11-02 19:32:25 -03:00
|
|
|
cron, script_name, bot_uuid
|
2025-10-06 10:30:17 -03:00
|
|
|
);
|
|
|
|
|
|
2025-11-02 19:32:25 -03:00
|
|
|
use crate::shared::models::system_automations::dsl::*;
|
2025-10-11 12:29:03 -03:00
|
|
|
|
|
|
|
|
let new_automation = (
|
2025-11-02 19:32:25 -03:00
|
|
|
bot_id.eq(bot_uuid),
|
|
|
|
|
kind.eq(TriggerKind::Scheduled as i32),
|
|
|
|
|
schedule.eq(cron),
|
|
|
|
|
param.eq(script_name),
|
|
|
|
|
is_active.eq(true),
|
2025-10-11 12:29:03 -03:00
|
|
|
);
|
|
|
|
|
|
2025-11-02 19:32:25 -03:00
|
|
|
let result = diesel::insert_into(system_automations)
|
2025-10-11 12:29:03 -03:00
|
|
|
.values(&new_automation)
|
2025-11-02 20:57:53 -03:00
|
|
|
.on_conflict((bot_id, kind, param))
|
2025-11-02 19:32:25 -03:00
|
|
|
.do_update()
|
|
|
|
|
.set((
|
|
|
|
|
schedule.eq(cron),
|
|
|
|
|
is_active.eq(true),
|
|
|
|
|
last_triggered.eq(None::<chrono::DateTime<chrono::Utc>>),
|
|
|
|
|
))
|
2025-10-11 20:25:08 -03:00
|
|
|
.execute(&mut *conn)?;
|
2025-10-06 10:30:17 -03:00
|
|
|
|
|
|
|
|
Ok(json!({
|
|
|
|
|
"command": "set_schedule",
|
|
|
|
|
"schedule": cron,
|
2025-11-02 19:32:25 -03:00
|
|
|
"script": script_name,
|
|
|
|
|
"bot_id": bot_uuid.to_string(),
|
2025-10-11 12:29:03 -03:00
|
|
|
"rows_affected": result
|
2025-10-06 10:30:17 -03:00
|
|
|
}))
|
|
|
|
|
}
|