feat: job events

This commit is contained in:
2025-12-31 00:54:23 +01:00
parent 0fba6c1445
commit 0049fa8167
7 changed files with 82 additions and 8 deletions

View File

@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO job_events (job_name, event_type, created_at) VALUES (?, ?, ?)",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "2f483d054029e2d69baa929756510f35731444a700acd9b5d6ab016e1fa2aa14"
}

View File

@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO job_events (job_name, event_type, event_data, created_at) VALUES (?, ?, ?, ?)",
"describe": {
"columns": [],
"parameters": {
"Right": 4
},
"nullable": []
},
"hash": "bc16f766f022e5062c43080f65e66bf41056eeac67fa550bf126c762e25ecfaa"
}

7
Cargo.lock generated
View File

@@ -268,6 +268,12 @@ dependencies = [
"libc",
]
[[package]]
name = "anyhow"
version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "atoi"
version = "2.0.0"
@@ -514,6 +520,7 @@ dependencies = [
"actix-files",
"actix-web",
"actix-web-httpauth",
"anyhow",
"chrono",
"dotenv",
"futures-util",

View File

@@ -7,6 +7,7 @@ edition = "2024"
actix-files = "0.6.9"
actix-web = "4.12.1"
actix-web-httpauth = "0.8.2"
anyhow = "1.0.100"
chrono = "0.4.42"
dotenv = "0.15.0"
futures-util = "0.3.31"

View File

@@ -114,7 +114,7 @@ fn start_polling_thread(openid_response: auth::OpenidResponse) -> JoinHandle<()>
thread
}
pub async fn create_new_access_token() -> Result<(), Box<dyn Error>> {
pub async fn create_new_access_token() -> Result<(), anyhow::Error> {
let client = reqwest::Client::new();
let url = "https://www.servercontrolpanel.de/realms/scp/protocol/openid-connect/token";
let refresh_token = helper::get_refresh_token().await?;

View File

@@ -5,7 +5,7 @@ use sqlx::query;
use crate::db;
pub async fn get_refresh_token() -> Result<String, Box<dyn Error>> {
pub async fn get_refresh_token() -> Result<String, anyhow::Error> {
let pool = db::get_pool().await?;
let tok = query!("SELECT * FROM tokens WHERE is_refresh = 1 ORDER BY expires_at DESC LIMIT 1")
.fetch_one(&pool)

View File

@@ -1,9 +1,35 @@
use std::{error::Error, time::Duration};
use std::{error::Error, option, time::Duration};
use quartz::{Job, Scheduler, Trigger};
use sqlx::query;
use tokio::runtime::Handle;
async fn log_job_event(job_name: &str, event: &str, event_data: Option<&str>) {
let pool = db::get_pool().await.unwrap();
let now = chrono::Utc::now();
let now = now.to_rfc3339();
if let Some(data) = event_data {
let _ = query!(
"INSERT INTO job_events (job_name, event_type, event_data, created_at) VALUES (?, ?, ?, ?)",
job_name,
event,
data,
now
)
.execute(&pool)
.await;
} else {
let _ = query!(
"INSERT INTO job_events (job_name, event_type, created_at) VALUES (?, ?, ?)",
job_name,
event,
now
)
.execute(&pool)
.await;
}
}
use crate::{auth, db};
pub fn init_scheduler(rt: Handle) -> Scheduler {
let scheduler = Scheduler::new();
@@ -39,17 +65,33 @@ pub fn init_scheduler(rt: Handle) -> Scheduler {
scheduler
}
pub async fn cleanup_old_tokens() -> Result<(), Box<dyn Error>> {
pub async fn cleanup_old_tokens() -> Result<(), anyhow::Error> {
log_job_event("CLEANUP_OLD_TOKENS", "START", None).await;
let pool = db::get_pool().await?;
let now = chrono::Utc::now();
let now = now.to_rfc3339();
query!("DELETE FROM tokens WHERE expires_at < ?", now)
let res = query!("DELETE FROM tokens WHERE expires_at < ?", now)
.execute(&pool)
.await?;
.await;
if let Err(e) = res {
log_job_event("CLEANUP_OLD_TOKENS", "ERROR", Some(&e.to_string())).await;
}
log_job_event("CLEANUP_OLD_TOKENS", "FINISHED", None).await;
Ok(())
}
pub async fn refresh_access_token() -> Result<(), Box<dyn Error>> {
auth::create_new_access_token().await?;
pub async fn refresh_access_token() -> Result<(), anyhow::Error> {
log_job_event("REFRESH_ACCESS_TOKEN", "START", None).await;
let res = auth::create_new_access_token().await;
if let Err(e) = res {
let clone_err = e.to_string().clone();
let _ = log_job_event(
"REFRESH_ACCESS_TOKEN",
"ERROR",
Some(&clone_err.to_string()),
)
.await;
}
log_job_event("REFRESH_ACCESS_TOKEN", "FINISHED", None).await;
Ok(())
}