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