qrush_engine/services/
cron_service.rs1use actix_web::{web, HttpResponse, Responder};
4use serde::Deserialize;
5use tera::Context;
6use crate::cron::cron_scheduler::CronScheduler;
7use crate::services::template_service::render_template;
8
9#[derive(Deserialize)]
10pub struct CronActionRequest {
11 pub action: String,
12 pub job_id: String,
13 pub enabled: Option<bool>,
14}
15
16#[derive(Deserialize)]
17pub struct CreateCronJobRequest {
18 pub name: String,
19 pub queue: String,
20 pub cron_expression: String,
21 pub job_type: String,
22 pub payload: serde_json::Value,
23}
24
25pub async fn render_cron_jobs() -> impl Responder {
26 match CronScheduler::list_cron_jobs().await {
27 Ok(cron_jobs) => {
28 let mut ctx = Context::new();
29 ctx.insert("title", "Cron Jobs");
30 ctx.insert("cron_jobs", &cron_jobs);
31 render_template("cron_jobs.html.tera", ctx).await
32 }
33 Err(e) => {
34 eprintln!("Failed to fetch cron jobs: {:?}", e);
35 HttpResponse::InternalServerError().body("Failed to fetch cron jobs")
36 }
37 }
38}
39
40pub async fn cron_action(payload: web::Json<CronActionRequest>) -> impl Responder {
41 let action = &payload.action;
42 let job_id = &payload.job_id;
43
44 let result = match action.as_str() {
45 "toggle" => {
46 let enabled = payload.enabled.unwrap_or(true);
47 CronScheduler::toggle_cron_job(job_id.clone(), enabled).await
48 }
49 "delete" => {
50 CronScheduler::delete_cron_job(job_id.clone()).await
51 }
52 "run_now" => {
53 match CronScheduler::run_now(job_id.clone()).await {
54 Ok(enqueued_id) => {
55 return HttpResponse::Ok().json(serde_json::json!({
56 "status": "success",
57 "action": "run_now",
58 "enqueued_job_id": enqueued_id
59 }));
60 }
61 Err(e) => Err(e)
62 }
63 }
64 _ => {
65 return HttpResponse::BadRequest().json(serde_json::json!({
66 "error": "Invalid action"
67 }));
68 }
69 };
70
71 match result {
72 Ok(_) => HttpResponse::Ok().json(serde_json::json!({
73 "status": "success",
74 "action": action
75 })),
76 Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({
77 "error": e.to_string()
78 }))
79 }
80}
81
82pub async fn create_cron_job(_payload: web::Json<CreateCronJobRequest>) -> impl Responder {
83 HttpResponse::Ok().json(serde_json::json!({
86 "status": "Cron job creation endpoint - implement based on your job types"
87 }))
88}