Browse Source

Add background processing

Mauricio Cassola 2 years ago
parent
commit
b4eb9dd6ff
1 changed files with 12 additions and 14 deletions
  1. 12 14
      src/main.rs

+ 12 - 14
src/main.rs

@@ -11,8 +11,8 @@ use tower::{Service, ServiceExt};
 use tracing as log;
 use tracing::Instrument;
 use triagebot::{db, github, handlers::Context, notification_listing, payload, EventName};
-// use std::{time::Duration, thread};
-// use tokio::task;
+use std::{time::Duration, thread};
+use tokio::task;
 
 async fn handle_agenda_request(req: String) -> anyhow::Result<String> {
     if req == "/agenda/lang/triage" {
@@ -237,21 +237,19 @@ async fn run_server(addr: SocketAddr) -> anyhow::Result<()> {
     let pool = db::ClientPool::new();
     db::run_migrations(&*pool.get().await)
         .await
-        .context("database migrations")?;
+        .context("database migrations")?;  
 
-    db::run_scheduled_events(&*pool.get().await)
-        .await
-        .context("database scheduled_events")?;
+    task::spawn(async move {
+        let pool = db::ClientPool::new();
 
-    // task::spawn(async move {
-    //     loop {
-    //         thread::sleep(Duration::from_secs(60)); // every one minute
+        loop {
+            thread::sleep(Duration::from_secs(60)); // every one minute
 
-    //         db::run_scheduled_events(&*pool.get().await)
-    //             .await
-    //             .context("database scheduled_events")?; 
-    //     }
-    // });
+            db::run_scheduled_events(&*pool.get().await)
+                .await
+                .context("database scheduled_events").unwrap(); 
+        }
+    });
 
     let client = Client::new();
     let gh = github::GithubClient::new_with_default_token(client.clone());