botserver/src/basic/keywords/set_schedule.rs
Rodrigo Rodriguez (Pragmatismo) 73d0ab3a1a refactor: simplify UI panels, use pooled DB, add --noui flag
- Removed unused `id` and `app_state` fields from `ChatPanel`; updated constructor to accept but ignore the state, reducing memory footprint.
- Switched database access in `ChatPanel` from a raw `Mutex` lock to a connection pool (`app_state.conn.get()`), improving concurrency and error handling.
- Reordered and cleaned up imports in `status_panel.rs` and formatted struct fields for readability.
- Updated VS Code launch configuration to pass `--noui` argument, enabling headless mode for debugging.
- Bumped several crate versions in `Cargo.lock` (e.g., `bitflags` to 2.10.0, `syn` to 2.0.108, `cookie` to 0.16.2) and added the new `ashpd` dependency, aligning the project with latest library releases.
2025-11-11 09:42:52 -03:00

43 lines
1.4 KiB
Rust

use diesel::prelude::*;
use log::{trace};
use serde_json::{json, Value};
use uuid::Uuid;
use crate::shared::models::TriggerKind;
pub fn execute_set_schedule(conn: &mut diesel::PgConnection, cron: &str, script_name: &str, bot_uuid: Uuid) -> Result<Value, Box<dyn std::error::Error>> {
trace!("Scheduling SET SCHEDULE cron: {}, script: {}, bot_id: {:?}", cron, script_name, bot_uuid);
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)?;
if !bot_exists {
return Err(format!("Bot with id {} does not exist", bot_uuid).into());
}
use crate::shared::models::system_automations::dsl::*;
let new_automation = (
bot_id.eq(bot_uuid),
kind.eq(TriggerKind::Scheduled as i32),
schedule.eq(cron),
param.eq(script_name),
is_active.eq(true),
);
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((
schedule.eq(cron),
is_active.eq(true),
last_triggered.eq(None::<chrono::DateTime<chrono::Utc>>),
))
.execute(&mut *conn)?;
let result = if update_result == 0 {
diesel::insert_into(system_automations).values(&new_automation).execute(&mut *conn)?
} else {
update_result
};
Ok(json!({
"command": "set_schedule",
"schedule": cron,
"script": script_name,
"bot_id": bot_uuid.to_string(),
"rows_affected": result
}))
}