From 0531cd66935802ad377ecb60ce4d4da33f3a5e59 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 1 Aug 2020 21:27:49 +0100 Subject: [PATCH] working out stuff --- src/config.rs | 36 +++++++++++++++++++++--- src/context.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/job.rs | 32 +++++++++++++++++++++ src/live.rs | 28 +++++++++++++++++++ src/main.rs | 39 +++++++++++++++++--------- 5 files changed, 193 insertions(+), 17 deletions(-) create mode 100644 src/context.rs create mode 100644 src/job.rs create mode 100644 src/live.rs diff --git a/src/config.rs b/src/config.rs index 21b4c32..79c53b9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,11 +2,14 @@ use super::*; use std::{ str, fmt, + path::{ + PathBuf, + }, }; /// Target for oneshots -#[derive(Debug)] +#[derive(Debug,Eq,PartialEq,Hash)] pub enum Target { /// Fired when daemon starts @@ -18,7 +21,7 @@ pub enum Target } /// When the job should be ran -#[derive(Debug)] +#[derive(Debug, PartialEq,Eq,Hash)] pub enum When { /// Run when a target is reached @@ -28,7 +31,7 @@ pub enum When } /// A command for `Job` -#[derive(Debug)] +#[derive(Debug, Eq,PartialEq,Hash)] pub struct Command { program: String, @@ -36,7 +39,7 @@ pub struct Command } /// Output for `define-job` -#[derive(Debug)] +#[derive(Debug, PartialEq,Eq,Hash)] pub struct Job { name: String, @@ -44,7 +47,32 @@ pub struct Job what: Command, } +/// Type of access +#[derive(Debug, PartialEq,Eq,Hash)] +pub enum Access +{ + Allow, + Deny, +} + +/// Access rule for user +#[derive(Debug, PartialEq,Eq,Hash)] +pub struct UserRule +{ + user: String, + mode: Access, +} +const DEFAULT_JOB_DIR: &str = "/etc/rori.kron"; + +/// User config +#[derive(Debug)] +pub struct Config +{ + pub job_dirs: Vec, + pub user_rules: Vec, + +} #[derive(Debug)] pub enum Error { diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..2c4a179 --- /dev/null +++ b/src/context.rs @@ -0,0 +1,75 @@ +//! Context that is passed to all workers and children + +use super::*; + +use std::sync::{Arc, Weak}; +use tokio::{ + sync::{ + RwLock, + mpsc, + }, + task, +}; + +/// Command to interrupt an `Imouto` worker +#[derive(Debug)] +pub enum Command { + +} + +/// The child worker for a `Context` interval or target. +#[derive(Debug)] +pub struct Imouto +{ + parent: Weak>, + + worker: task::JoinHandle<()>, + handler: mpsc::Sender, +} + +impl Imouto +{ + /// Get the parent of this worker if it still exists + pub fn oneesan(&self) -> Option + { + 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, + + /// Worker for this context, that updates the children when needed. + worker: task::JoinHandle<()>, + /// Hook to send `live::Oneesan` events to. + live_hook: mpsc::Sender, +} + +/// The parent job Context, contains all worker child informations +#[derive(Debug)] +pub struct Context(Arc>); + +// 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 diff --git a/src/job.rs b/src/job.rs new file mode 100644 index 0000000..d4c8293 --- /dev/null +++ b/src/job.rs @@ -0,0 +1,32 @@ +//! A single running job +use super::*; + +use tokio::{ + sync::{ + mpsc, + }, +}; + +/// Commands to interrupt jobs +#[derive(Debug,PartialEq,Eq,Hash,Clone)] +pub enum Command +{ + /// Start graceful shutdown, wait for children. + Stop, + /// Stop immediately + Abort, + /// Interrupt the current iteration, and restart the iterator if needed + Reseed(interval::Time), + /// Run the command now + Oneshot, + /// Config reload + Reload(context::Context), + /// Just restart the iterator + Restart, +} + +#[derive(Debug)] +pub struct Job +{ + handler: mpsc::Sender, +} diff --git a/src/live.rs b/src/live.rs new file mode 100644 index 0000000..30257f3 --- /dev/null +++ b/src/live.rs @@ -0,0 +1,28 @@ +//! Live config reloader +use super::*; +use std::{ + path::{ + PathBuf, + }, +}; + +/// An event to be passed to `Context`. +#[derive(Debug)] +pub enum Event +{ + +} + +/// A watcher context, we hook specific `Context`s here, to be dispatched to on file change +#[derive(Debug)] +pub struct Oneesan { + path: PathBuf, + //TODO: Hooks +} + +/// Start watching this path for changes of files +pub fn watch(path: PathBuf) -> Oneesan +{ + +} + diff --git a/src/main.rs b/src/main.rs index 6458f35..1f84e24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use tokio::{ Duration, }, sync::{ - oneshot, + mpsc, }, task, }; @@ -17,24 +17,30 @@ use tokio::{ mod interval; mod config; -async fn do_thing_every() -> Result<(oneshot::Sender<()>, task::JoinHandle<()>), Box> +async fn do_thing_every() -> Result<(mpsc::Sender<()>, task::JoinHandle<()>), Box> { - let mut interval = time::interval(Duration::from_secs(1)); - let (tx, mut rx) = oneshot::channel(); + let mut interval = time::interval(Duration::from_secs(10)); + let (tx, mut rx) = mpsc::channel(16); let handle = tokio::spawn(async move { println!("starting?"); loop { - select!{ - _ = interval.tick() => { - // Do the things - println!("yes"); - } - command = &mut rx => { - // We got interrupt, interpret `command` here - println!("no"); - break; + let mut tick = interval.tick(); + tokio::pin!(tick); + loop { + select!{ + _ = &mut tick => { + // Do the things + println!("yes"); + break; + } + command = rx.recv() => { + // We got interrupt, interpret `command` here. + // `continue` to continue waiting on this interval, break to go to next, return to stop + println!("no"); + continue; + } } } } @@ -46,5 +52,12 @@ async fn do_thing_every() -> Result<(oneshot::Sender<()>, task::JoinHandle<()>), #[tokio::main] async fn main() -> Result<(), Box> { + let (mut tx, h) = do_thing_every().await?; + + loop { + time::delay_for(Duration::from_secs(6)).await; + tx.send(()).await?; + } + h.await; Ok(()) }