Aravinth Manivannan
8afea6fc81
SUMMARY ftest when run as a local dev tool will only run one job during its runtime. Database and settings are not required for this purpose. This commit defines MinAppContext for running in a single job execution mode and FullAppContext for running as a daemon
71 lines
1.9 KiB
Rust
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::AppFullCtx;
|
|
|
|
pub struct Scheduler {
|
|
ctx: AppFullCtx,
|
|
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: AppFullCtx) -> 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,
|
|
}
|
|
}
|
|
}
|