Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 7, 2024
1 parent 42719d8 commit d79f573
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use interprocess::local_socket::{GenericFilePath, GenericNamespaced, NameType, ToFsName, ToNsName};
use once_cell::sync::Lazy;
pub use std::env::*;
use std::path::PathBuf;
Expand Down
47 changes: 35 additions & 12 deletions src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use crate::state_file::{StateFile, StateFileDaemon, StateFileDaemonStatus};
use crate::{async_watcher, env, Result};
use duct::cmd;
use interprocess::local_socket::tokio::prelude::*;
use interprocess::local_socket::{GenericFilePath, GenericNamespaced, ListenerOptions};
use interprocess::local_socket::{GenericFilePath, ListenerOptions};
use std::io;
use std::path::PathBuf;
use std::process::exit;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
#[cfg(unix)]
use tokio::signal::unix::{signal, SignalKind};
use tokio::{fs, select, time, try_join};
use tokio::signal::unix::{SignalKind};
use tokio::sync::mpsc;
use tokio::sync::mpsc::Receiver;
use tokio::{fs, select, signal, time, try_join};

pub struct Supervisor {
state_file: StateFile,
Expand All @@ -21,14 +23,10 @@ const INTERVAL: Duration = Duration::from_secs(10);

enum Event {
FileChange(Vec<PathBuf>),
Signal(CrossPlatformSignal),
Signal,
Interval,
}

enum CrossPlatformSignal {
Sigterm,
}

impl Supervisor {
pub fn new(pid_file: StateFile) -> Self {
Self { state_file: pid_file, last_run: time::Instant::now() }
Expand All @@ -41,7 +39,7 @@ impl Supervisor {
let _ = fs::remove_file(&*env::IPC_SOCK_PATH).await;
let opts = ListenerOptions::new().name(env::IPC_SOCK_PATH.clone().to_fs_name::<GenericFilePath>()?);
let listener = opts.create_tokio()?;

self.state_file.daemons.insert("pitchfork".to_string(), StateFileDaemon { pid, status: StateFileDaemonStatus::Running });
self.state_file.write()?;

Expand All @@ -53,15 +51,24 @@ impl Supervisor {
]).await?;

#[cfg(unix)]
let mut sigterm = signal(SignalKind::terminate())?;
let mut sigterm = signals(vec![
SignalKind::terminate(),
SignalKind::alarm(),
SignalKind::interrupt(),
SignalKind::quit(),
SignalKind::hangup(),
SignalKind::pipe(),
SignalKind::user_defined1(),
SignalKind::user_defined2(),
])?;

self.refresh(Event::Interval).await?;

loop {
#[cfg(unix)]
select! {
_ = sigterm.recv() => {
if let Err(err) = self.refresh(Event::Signal(CrossPlatformSignal::Sigterm)).await {
if let Err(err) = self.refresh(Event::Signal).await {
error!("supervisor error: {:?}", err);
}
},
Expand Down Expand Up @@ -149,7 +156,7 @@ impl Supervisor {
// self.pid_file = PidFile::read(&self.pid_file.path)?;
// }
}
Event::Signal(CrossPlatformSignal::Sigterm) => {
Event::Signal => {
info!("received SIGTERM, stopping");
exit(0);
}
Expand All @@ -174,3 +181,19 @@ impl Supervisor {
exit(0);
}
}


fn signals(signals: Vec<SignalKind>) -> io::Result<Receiver<Event>> {
let (tx, rx) = mpsc::channel(1);
for signal in signals {
let tx = tx.clone();
tokio::spawn(async move {
let mut stream = signal::unix::signal(signal).unwrap();
loop {
stream.recv().await;
tx.send(Event::Signal).await.unwrap();
}
});
}
Ok(rx)
}

0 comments on commit d79f573

Please sign in to comment.