wip: rewrite loop to handle assigning tasks

This commit is contained in:
Wyatt J. Miller 2024-09-29 21:15:14 -04:00
parent 7aa1dd07f4
commit a742dd7f12

View File

@ -1,12 +1,14 @@
use chrono::{prelude::*, Duration as ChronoDuration}; use chrono::{prelude::*, Duration as ChronoDuration};
use job_scheduler::{Job, JobScheduler};
// use once_cell::sync::Lazy; // use once_cell::sync::Lazy;
use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use tasks::import_posts::{self, import_posts};
use std::env; use std::env;
use std::time::Duration; use std::time::Duration;
mod config; //mod config;
mod tasks; mod tasks;
mod util; mod utils;
pub struct TaskManager<'a> { pub struct TaskManager<'a> {
pool: Pool<Postgres>, pool: Pool<Postgres>,
@ -29,7 +31,7 @@ enum TaskStatus {
Failed, Failed,
} }
#[derive(Debug, sqlx::FromRow)] #[derive(Debug, sqlx::FromRow, Clone)]
pub struct TaskJob { pub struct TaskJob {
pub task_id: i32, pub task_id: i32,
pub task_name: String, pub task_name: String,
@ -81,15 +83,15 @@ impl<'a> TaskManager<'a> {
.unwrap(); .unwrap();
let mut scheduler = job_scheduler::JobScheduler::new(); let mut scheduler = job_scheduler::JobScheduler::new();
results.iter().for_each(|j| { for result in results {
scheduler.add(job_scheduler::Job::new(j.schedule.parse().unwrap(), || { let r = result.clone();
println!("Registering task - task name: {:?}", j.task_name); scheduler.add(job_scheduler::Job::new(r.schedule.parse().unwrap(), move || {
println!("Registering job: {}", r.task_name);
match j.task_id { match r.task_id {
1 => tasks::import_posts::import_posts("/app", &self.pool), 1 => async { import_posts::import_posts("/app", &self.pool).await },
_ => panic!(), _ => panic!(),
}; };
})); }));
}); }
} }
} }