You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.6 KiB

// 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
use crate::{models::*, UResult, UError};
4 years ago
use std::collections::HashMap;
use std::pin::Pin;
use std::thread::sleep;
use std::time::{Duration, Instant};
use std::task::Poll;
use tokio::process::Command;
4 years ago
4 years ago
use futures::{lock::Mutex, prelude::*, poll};
use lazy_static::lazy_static;
use tokio::{prelude::*, spawn, task::JoinHandle};
use uuid::Uuid;
4 years ago
pub type FutRes = UResult<JobResult>;
4 years ago
lazy_static! {
static ref FUT_RESULTS: Mutex<HashMap<Uuid, JoinHandle<FutRes>>> = Mutex::new(HashMap::new());
4 years ago
}
4 years ago
//TODO: waiter struct
pub async fn append_task(task: impl Future<Output=FutRes> + Send + 'static) -> Uuid {
let fid = Uuid::new_v4();
let result = spawn(Box::pin(task));
FUT_RESULTS.lock().await.insert(fid, result);
fid
}
pub async fn append_tasks(tasks: Vec<impl Future<Output=FutRes> + Send + 'static>) -> Vec<Uuid> {
let mut fids = Vec::<Uuid>::new();
for f in tasks.into_iter() {
4 years ago
let fid = append_task(f).await;
fids.push(fid);
}
fids
4 years ago
}
pub async fn pop_task(fid: Uuid) -> JoinHandle<FutRes> {
FUT_RESULTS.lock().await.remove(&fid).expect(&UError::NoTask(fid).to_string())
}
4 years ago
pub async fn task_present(fid: Uuid) -> bool {
FUT_RESULTS.lock().await.get(&fid).is_some()
}
pub async fn pop_task_if_completed(fid: Uuid) -> Option<FutRes>{
4 years ago
let mut tasks = FUT_RESULTS
.lock()
4 years ago
.await;
let task = tasks
.get_mut(&fid)
.expect(&UError::NoTask(fid).to_string());
4 years ago
let status = match poll!(task) {
Poll::Pending => None,
Poll::Ready(r) => Some(r.unwrap())
};
if status.is_some() {
4 years ago
pop_task(fid).await;
}
status
}
pub async fn pop_completed(fids: Option<Vec<Uuid>>) -> Vec<Option<FutRes>> {
4 years ago
let fids = match fids {
Some(v) => v,
None => FUT_RESULTS.lock()
.await
.keys()
.map(|k| *k)
.collect::<Vec<Uuid>>()
4 years ago
};
let mut completed: Vec<Option<FutRes>> = vec![];
for fid in fids {
completed.push(pop_task_if_completed(fid).await)
}
completed
}
pub async fn wait_for_task(fid: Uuid) -> FutRes {
pop_task(fid).await.await.unwrap()
}
pub async fn wait_for_tasks(fids: Vec<Uuid>) -> Vec<FutRes> {
let mut results = vec![];
for fid in fids {
results.push(wait_for_task(fid).await);
}
results
}
pub async fn run_until_complete(task: impl Future<Output=FutRes> + Send + 'static) -> FutRes {
4 years ago
let fid = append_task(task).await;
wait_for_task(fid).await
}