|
|
|
//! Context that is passed to all workers and children
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
use tokio::{
|
|
|
|
sync::{
|
|
|
|
RwLock,
|
|
|
|
mpsc,
|
|
|
|
},
|
|
|
|
task,
|
|
|
|
};
|
|
|
|
|
|
|
|
use job::Command;
|
|
|
|
|
|
|
|
/// The child worker for a `Context` interval or target.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Imouto
|
|
|
|
{
|
|
|
|
parent: Weak<RwLock<InnerContext>>,
|
|
|
|
|
|
|
|
worker: task::JoinHandle<()>,
|
|
|
|
handler: mpsc::Sender<Command>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Imouto
|
|
|
|
{
|
|
|
|
/// Get the parent of this worker if it still exists
|
|
|
|
pub fn oneesan(&self) -> Option<Context>
|
|
|
|
{
|
|
|
|
self.parent.upgrade().map(|x| Context(x))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Is this a zombie worker?
|
|
|
|
pub fn is_orphan(&self) -> bool
|
|
|
|
{
|
|
|
|
self.oneesan().is_none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct InnerContext
|
|
|
|
{
|
|
|
|
/// Name of the job
|
|
|
|
name: String,
|
|
|
|
/// All active child workers for this job
|
|
|
|
children: Vec<Imouto>,
|
|
|
|
|
|
|
|
/// Worker for this context, that updates the children when needed.
|
|
|
|
worker: task::JoinHandle<()>,
|
|
|
|
/// Hook to send `hot::Oneesan` events to.
|
|
|
|
live_hook: mpsc::Sender<hot::Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The parent job Context, contains all worker child informations
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Context(Arc<RwLock<InnerContext>>);
|
|
|
|
|
|
|
|
// Job (context::Context)
|
|
|
|
// | -- interval 1 (context::Imouto)
|
|
|
|
// | -- interval 2
|
|
|
|
// \ -- target 1
|
|
|
|
|
|
|
|
// Job 2
|
|
|
|
// \ -- interval
|
|
|
|
|
|
|
|
// Def updated for `Job`:
|
|
|
|
// - Lock the Context
|
|
|
|
// - Update the context
|
|
|
|
// - Interrupt each `Imouto`
|
|
|
|
// - Unlock Context
|