Skip to main content

max / makenotwork

1.0 KB · 36 lines History Blame Raw
1 use chrono::{DateTime, Utc};
2 use sqlx::{FromRow, PgPool};
3
4 use crate::error::Result;
5
6 #[derive(FromRow)]
7 pub struct SchedulerJobRun {
8 pub job_name: String,
9 pub last_ran_at: DateTime<Utc>,
10 pub rows_affected: i64,
11 }
12
13 /// Upsert the last-run timestamp and rows affected for a scheduled job.
14 pub async fn record_job_run(pool: &PgPool, job_name: &str, rows_affected: i64) -> Result<()> {
15 sqlx::query(
16 "INSERT INTO scheduler_job_runs (job_name, last_ran_at, rows_affected)
17 VALUES ($1, NOW(), $2)
18 ON CONFLICT (job_name) DO UPDATE SET last_ran_at = NOW(), rows_affected = $2",
19 )
20 .bind(job_name)
21 .bind(rows_affected)
22 .execute(pool)
23 .await?;
24 Ok(())
25 }
26
27 /// Fetch all job run records for health page display.
28 pub async fn get_job_runs(pool: &PgPool) -> Result<Vec<SchedulerJobRun>> {
29 let rows = sqlx::query_as::<_, SchedulerJobRun>(
30 "SELECT job_name, last_ran_at, rows_affected FROM scheduler_job_runs ORDER BY job_name",
31 )
32 .fetch_all(pool)
33 .await?;
34 Ok(rows)
35 }
36