|
|
|
// list of jobs: job (cmd, args) OR rust fn OR python func + cron-like timing
|
|
|
|
// job runner (thread)
|
|
|
|
// every job runs in other thread/process
|
|
|
|
/*
|
|
|
|
|
|
|
|
enum Schedule {
|
|
|
|
Persistent, // run forever, restart if stops (set max_retries)
|
|
|
|
Cron(CronSchedule),
|
|
|
|
Once
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
models::*,
|
|
|
|
UResult,
|
|
|
|
};
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use tokio::process::Command;
|
|
|
|
|
|
|
|
use futures::{lock::Mutex, prelude::*};
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use tokio::{prelude::*, spawn, task::JoinHandle};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
pub type FutRes = String;
|
|
|
|
type BoxFut<O> = Pin<Box<dyn Future<Output = O> + Send + 'static>>;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref FUT_RESULTS: Mutex<HashMap<Uuid, JoinHandle<FutRes>>> = Mutex::new(HashMap::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn apply_tasks(tasks: Vec<impl Future<Output=FutRes> + Send + 'static>) -> Vec<Uuid> {
|
|
|
|
let mut fids = Vec::<Uuid>::new();
|
|
|
|
for f in tasks.into_iter() {
|
|
|
|
let fid = Uuid::new_v4();
|
|
|
|
fids.push(fid);
|
|
|
|
let result = spawn(Box::pin(f));
|
|
|
|
FUT_RESULTS.lock().await.insert(fid, result);
|
|
|
|
}
|
|
|
|
fids
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn apply_task(task: impl Future<Output=FutRes> + Send + 'static) -> Uuid {
|
|
|
|
apply_tasks(vec![Box::pin(task)]).await[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn pop(fid: Uuid) -> Option<JoinHandle<FutRes>> {
|
|
|
|
FUT_RESULTS.lock().await.remove(&fid)
|
|
|
|
}
|