aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2021-05-15 00:11:59 -0400
committerChristine Dodrill <me@christine.website>2021-05-15 00:11:59 -0400
commit50e57c427c8f8978d9125721c677bd935bb854e7 (patch)
treef5d1cd848d7fdb4795fd7e71be9efb3c4d096485 /src
parent95bfc64097942affe48269ef322a318db104901a (diff)
downloadxesite-50e57c427c8f8978d9125721c677bd935bb854e7.tar.xz
xesite-50e57c427c8f8978d9125721c677bd935bb854e7.zip
start cheating systemdcheat-systemd
Signed-off-by: Christine Dodrill <me@christine.website>
Diffstat (limited to 'src')
-rwxr-xr-xsrc/bin/decoy.py13
-rw-r--r--src/main.rs69
2 files changed, 59 insertions, 23 deletions
diff --git a/src/bin/decoy.py b/src/bin/decoy.py
new file mode 100755
index 0000000..f985413
--- /dev/null
+++ b/src/bin/decoy.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env nix-shell
+#! nix-shell -p python3 -i python3
+
+import os
+import time
+
+pid = os.fork()
+if pid == 0:
+ for fd in {0, 1, 2}:
+ os.close(fd)
+ time.sleep(1)
+else:
+ print(pid)
diff --git a/src/main.rs b/src/main.rs
index cac19cf..4472623 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,8 +4,6 @@ extern crate tracing;
use color_eyre::eyre::Result;
use hyper::{header::CONTENT_TYPE, Body, Response};
use prometheus::{Encoder, TextEncoder};
-use std::net::IpAddr;
-use std::str::FromStr;
use std::sync::Arc;
use tokio::net::UnixListener;
use tokio_stream::wrappers::UnixListenerStream;
@@ -33,6 +31,16 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
info!("starting up commit {}", env!("GITHUB_SHA"));
+ #[cfg(all(feature = "systemd", target_os = "linux"))]
+ {
+ use std::process::Command;
+ let pid = Command::new("./src/bin/decoy.py").output()?.stdout;
+ let pid = String::from_utf8(pid)?.trim().parse::<u32>()?;
+ if let Ok(ref mut n) = sdnotify::SdNotify::from_env() {
+ n.set_main_pid(pid)?;
+ }
+ }
+
let state = Arc::new(
app::init(
std::env::var("CONFIG_FNAME")
@@ -211,8 +219,11 @@ async fn main() -> Result<()> {
.with(warp::log(APPLICATION_NAME))
.recover(handlers::rejection);
- #[cfg(target_os = "linux")]
+ let server = warp::serve(site);
+
+ #[cfg(feature = "systemd")]
{
+ #[cfg(target_os = "linux")]
match sdnotify::SdNotify::from_env() {
Ok(ref mut n) => {
// shitty heuristic for detecting if we're running in prod
@@ -234,30 +245,42 @@ async fn main() -> Result<()> {
}
Err(why) => error!("not running under systemd with Type=notify: {}", why),
}
- }
- let server = warp::serve(site);
+ let mut lfd = listenfd::ListenFd::from_env();
- match std::env::var("SOCKPATH") {
- Ok(sockpath) => {
- let _ = std::fs::remove_file(&sockpath);
- let listener = UnixListener::bind(sockpath)?;
- let incoming = UnixListenerStream::new(listener);
+ if let Some(lis) = lfd.take_unix_listener(0)? {
+ let incoming = UnixListenerStream::new(UnixListener::from_std(lis)?);
server.run_incoming(incoming).await;
-
- Ok(())
}
- Err(_) => {
- server
- .run((
- IpAddr::from_str(&std::env::var("HOST").unwrap_or("::".into()))?,
- std::env::var("PORT")
- .unwrap_or("3030".into())
- .parse::<u16>()?,
- ))
- .await;
-
- Ok(())
+ Ok(())
+ }
+
+ #[cfg(not(feature = "systemd"))]
+ {
+ use std::net::IpAddr;
+ use std::str::FromStr;
+
+ match std::env::var("SOCKPATH") {
+ Ok(sockpath) => {
+ let _ = std::fs::remove_file(&sockpath);
+ let listener = UnixListener::bind(sockpath)?;
+ let incoming = UnixListenerStream::new(listener);
+ server.run_incoming(incoming).await;
+
+ Ok(())
+ }
+ Err(_) => {
+ server
+ .run((
+ IpAddr::from_str(&std::env::var("HOST").unwrap_or("::".into()))?,
+ std::env::var("PORT")
+ .unwrap_or("3030".into())
+ .parse::<u16>()?,
+ ))
+ .await;
+
+ Ok(())
+ }
}
}
}