working out stuff

master
Avril 4 years ago
parent a69a7a46ba
commit 0531cd6693
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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<PathBuf>,
pub user_rules: Vec<UserRule>,
}
#[derive(Debug)]
pub enum Error {

@ -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<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 `live::Oneesan` events to.
live_hook: mpsc::Sender<live::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

@ -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<Command>,
}

@ -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
{
}

@ -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<dyn std::error::Error>>
async fn do_thing_every() -> Result<(mpsc::Sender<()>, task::JoinHandle<()>), Box<dyn std::error::Error>>
{
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<dyn std::error::Error>> {
let (mut tx, h) = do_thing_every().await?;
loop {
time::delay_for(Duration::from_secs(6)).await;
tx.send(()).await?;
}
h.await;
Ok(())
}

Loading…
Cancel
Save