feat: job events
This commit is contained in:
12
.sqlx/query-2f483d054029e2d69baa929756510f35731444a700acd9b5d6ab016e1fa2aa14.json
generated
Normal file
12
.sqlx/query-2f483d054029e2d69baa929756510f35731444a700acd9b5d6ab016e1fa2aa14.json
generated
Normal 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"
|
||||||
|
}
|
||||||
12
.sqlx/query-bc16f766f022e5062c43080f65e66bf41056eeac67fa550bf126c762e25ecfaa.json
generated
Normal file
12
.sqlx/query-bc16f766f022e5062c43080f65e66bf41056eeac67fa550bf126c762e25ecfaa.json
generated
Normal 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
7
Cargo.lock
generated
@@ -268,6 +268,12 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.100"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "atoi"
|
name = "atoi"
|
||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
@@ -514,6 +520,7 @@ dependencies = [
|
|||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"actix-web-httpauth",
|
"actix-web-httpauth",
|
||||||
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ edition = "2024"
|
|||||||
actix-files = "0.6.9"
|
actix-files = "0.6.9"
|
||||||
actix-web = "4.12.1"
|
actix-web = "4.12.1"
|
||||||
actix-web-httpauth = "0.8.2"
|
actix-web-httpauth = "0.8.2"
|
||||||
|
anyhow = "1.0.100"
|
||||||
chrono = "0.4.42"
|
chrono = "0.4.42"
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
futures-util = "0.3.31"
|
futures-util = "0.3.31"
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ fn start_polling_thread(openid_response: auth::OpenidResponse) -> JoinHandle<()>
|
|||||||
thread
|
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 client = reqwest::Client::new();
|
||||||
let url = "https://www.servercontrolpanel.de/realms/scp/protocol/openid-connect/token";
|
let url = "https://www.servercontrolpanel.de/realms/scp/protocol/openid-connect/token";
|
||||||
let refresh_token = helper::get_refresh_token().await?;
|
let refresh_token = helper::get_refresh_token().await?;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use sqlx::query;
|
|||||||
|
|
||||||
use crate::db;
|
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 pool = db::get_pool().await?;
|
||||||
let tok = query!("SELECT * FROM tokens WHERE is_refresh = 1 ORDER BY expires_at DESC LIMIT 1")
|
let tok = query!("SELECT * FROM tokens WHERE is_refresh = 1 ORDER BY expires_at DESC LIMIT 1")
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
|
|||||||
54
src/jobs.rs
54
src/jobs.rs
@@ -1,9 +1,35 @@
|
|||||||
use std::{error::Error, time::Duration};
|
use std::{error::Error, option, time::Duration};
|
||||||
|
|
||||||
use quartz::{Job, Scheduler, Trigger};
|
use quartz::{Job, Scheduler, Trigger};
|
||||||
use sqlx::query;
|
use sqlx::query;
|
||||||
use tokio::runtime::Handle;
|
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};
|
use crate::{auth, db};
|
||||||
pub fn init_scheduler(rt: Handle) -> Scheduler {
|
pub fn init_scheduler(rt: Handle) -> Scheduler {
|
||||||
let scheduler = Scheduler::new();
|
let scheduler = Scheduler::new();
|
||||||
@@ -39,17 +65,33 @@ pub fn init_scheduler(rt: Handle) -> Scheduler {
|
|||||||
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 pool = db::get_pool().await?;
|
||||||
let now = chrono::Utc::now();
|
let now = chrono::Utc::now();
|
||||||
let now = now.to_rfc3339();
|
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)
|
.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn refresh_access_token() -> Result<(), Box<dyn Error>> {
|
pub async fn refresh_access_token() -> Result<(), anyhow::Error> {
|
||||||
auth::create_new_access_token().await?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user