From f6c21a91847aa3c39fae1a94ed366c46a815607e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 23 Feb 2024 15:34:22 +0100 Subject: [PATCH 1/2] Send startup notification via HTTP --- magic-nix-cache/src/main.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 015eda3..0a1d24f 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -23,7 +23,6 @@ use std::collections::HashSet; use std::fs::{self, create_dir_all, OpenOptions}; use std::io::Write; use std::net::SocketAddr; -use std::os::fd::FromRawFd; use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -103,9 +102,9 @@ struct Args { #[arg(long)] use_flakehub: bool, - /// File descriptor on which to send startup notification. + /// URL to which to post startup notification. #[arg(long)] - notify_fd: Option, + startup_notification_url: Option, } /// The global server state. @@ -300,9 +299,27 @@ async fn main_cli() { tracing::info!("Listening on {}", args.listen); - if let Some(notify_fd) = args.notify_fd { - let mut f = unsafe { std::fs::File::from_raw_fd(notify_fd) }; - writeln!(&mut f, "INIT").unwrap(); + if let Some(startup_notification_url) = args.startup_notification_url { + let response = reqwest::Client::new() + .post(startup_notification_url) + .header("Content-Type", "application/json") + .body("{}") + .send() + .await; + match response { + Ok(response) => { + if !response.status().is_success() { + panic!( + "Startup notification returned an error: {}\n{}", + response.status(), + response.text().await.unwrap_or_else(|_| "".to_owned()) + ); + } + } + Err(err) => { + panic!("Startup notification failed: {}", err); + } + } } let ret = axum::Server::bind(&args.listen) From 0607f5efa46edfc43540b99d9f65cf6abdb31579 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 23 Feb 2024 18:18:34 +0100 Subject: [PATCH 2/2] Use reqwest::header::CONTENT_TYPE --- magic-nix-cache/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 0a1d24f..ae6e1f2 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -302,7 +302,7 @@ async fn main_cli() { if let Some(startup_notification_url) = args.startup_notification_url { let response = reqwest::Client::new() .post(startup_notification_url) - .header("Content-Type", "application/json") + .header(reqwest::header::CONTENT_TYPE, "application/json") .body("{}") .send() .await; @@ -356,7 +356,7 @@ async fn post_build_hook(out_paths: &str) { let response = reqwest::Client::new() .post(format!("http://{}/api/enqueue-paths", &args.server)) - .header("Content-Type", "application/json") + .header(reqwest::header::CONTENT_TYPE, "application/json") .body(serde_json::to_string(&request).unwrap()) .send() .await;