ftest/src/runner/scheduler.rs

71 lines
1.9 KiB
Rust

// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::time::Duration;
use tokio::sync::oneshot::error::TryRecvError;
use tokio::{
sync::oneshot::{self, Receiver, Sender},
task::JoinHandle,
};
use crate::AppCtx;
pub struct Scheduler {
ctx: AppCtx,
rx: Receiver<()>,
}
pub struct SchedulerControler {
tx: Sender<()>,
handle: JoinHandle<()>,
}
impl SchedulerControler {
pub async fn stop(self) {
self.tx.send(()).unwrap();
self.handle.await.unwrap();
}
}
impl Scheduler {
pub async fn spawn(ctx: AppCtx) -> SchedulerControler {
let (tx, rx) = oneshot::channel();
let mut sc = Scheduler { ctx, rx };
let x = async move {
loop {
if let Err(TryRecvError::Empty) = sc.rx.try_recv() {
let task = sc.ctx.db.get_next_job_to_run().await;
if task.is_err() {
tokio::time::sleep(Duration::new(5, 0)).await;
continue;
}
let task = task.unwrap();
tracing::info!("Scheduling job for commit {}", task.commit_hash);
sc.ctx
.db
.mark_job_scheduled(&task.commit_hash)
.await
.unwrap();
super::run(sc.ctx.clone(), &task.commit_hash).await;
sc.ctx
.db
.mark_job_finished(&task.commit_hash)
.await
.unwrap();
} else {
tracing::info!("Terminating scheduler");
break;
}
}
};
SchedulerControler {
handle: tokio::spawn(x),
tx,
}
}
}