From a742dd7f12a5a68e85b2ab883adbc830a0a65007 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sun, 29 Sep 2024 21:15:14 -0400 Subject: [PATCH] wip: rewrite loop to handle assigning tasks --- backend/task/src/main.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/backend/task/src/main.rs b/backend/task/src/main.rs index b568bc6..c838981 100644 --- a/backend/task/src/main.rs +++ b/backend/task/src/main.rs @@ -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, @@ -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!(), }; })); - }); + } } }