102 lines
2.6 KiB
Rust
102 lines
2.6 KiB
Rust
use chrono::Utc;
|
|
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
|
|
use tasks::import_posts;
|
|
use std::env;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
//mod config;
|
|
mod tasks;
|
|
mod utils;
|
|
|
|
pub struct TaskManager<'a> {
|
|
pool: Pool<Postgres>,
|
|
jobs: Vec<TaskJob>,
|
|
last_activated: Option<chrono::DateTime<Utc>>,
|
|
last_job: Option<TaskJob>,
|
|
scheduler: job_scheduler::JobScheduler<'a>,
|
|
}
|
|
|
|
#[derive(Debug, sqlx::FromRow)]
|
|
pub struct TaskLog {
|
|
log_id: u8,
|
|
task_id: u8,
|
|
created_at: chrono::DateTime<Utc>,
|
|
task_status: TaskStatus,
|
|
finished_at: Option<chrono::DateTime<Utc>>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
enum TaskStatus {
|
|
Pending(String),
|
|
Completed(String),
|
|
Failed(String),
|
|
}
|
|
|
|
#[derive(Debug, sqlx::FromRow, Clone)]
|
|
pub struct TaskJob {
|
|
pub task_id: i32,
|
|
pub task_name: String,
|
|
pub schedule: String,
|
|
pub is_active: bool,
|
|
pub created_at: chrono::DateTime<Utc>,
|
|
pub deleted_at: Option<chrono::DateTime<Utc>>,
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
println!("Hello, world!");
|
|
|
|
dotenvy::dotenv().unwrap();
|
|
let database_url =
|
|
env::var("DATABASE_URL").expect("Environment variable DATABASE_URL is not found");
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(10)
|
|
.acquire_timeout(Duration::from_secs(5))
|
|
.connect(&database_url)
|
|
.await
|
|
.expect("Failed to connect to the database");
|
|
|
|
let mut manager = TaskManager::new(pool);
|
|
manager.register_jobs().await;
|
|
|
|
loop {
|
|
manager.scheduler.tick();
|
|
std::thread::sleep(std::time::Duration::from_millis(500));
|
|
}
|
|
}
|
|
|
|
impl<'a> TaskManager<'a> {
|
|
fn new(pool: Pool<Postgres>) -> Self {
|
|
TaskManager {
|
|
pool,
|
|
jobs: Vec::new(),
|
|
last_activated: None,
|
|
last_job: None,
|
|
scheduler: job_scheduler::JobScheduler::new(),
|
|
}
|
|
}
|
|
|
|
pub async fn register_jobs(&self) {
|
|
// let jobs: Vec<Job> = Vec::new();
|
|
let results = sqlx::query_as::<_, TaskJob>("SELECT task_id, task_name, schedule, is_active, created_at, deleted_at FROM tasks WHERE is_active = true AND deleted_at IS NULL")
|
|
.fetch_all(&self.pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
let mut scheduler = job_scheduler::JobScheduler::new();
|
|
results.iter().for_each(|r| {
|
|
println!("Registering job: {:?}", r.task_name);
|
|
|
|
let job: _ = job_scheduler::Job::new(r.schedule.parse().unwrap(), || {
|
|
match r.task_id {
|
|
1 => import_posts::register(&Arc::new(&self.pool)),
|
|
_ => panic!(),
|
|
}
|
|
});
|
|
|
|
scheduler.add(job);
|
|
});
|
|
}
|
|
}
|