Skip to content

Commit 1152ede

Browse files
authored
Merge pull request #170 from DeterminateSystems/push-qqkrkzwzrxkw
Port 0 handling, but less invasive
2 parents 59f4e54 + 0596f9e commit 1152ede

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

magic-nix-cache/src/main.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
257257
tracing::debug!("Running in {}", environment.to_string());
258258
args.validate(environment)?;
259259

260+
let listener = tokio::net::TcpListener::bind(&args.listen).await?;
261+
let listener_addr = listener.local_addr().expect("failed to get local address");
262+
260263
let metrics = Arc::new(telemetry::TelemetryReport::new(recorder.clone()));
261264

262265
let dnixd_uds_socket_dir: &Path = Path::new(&DETERMINATE_STATE_DIR);
@@ -411,6 +414,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
411414
format!("{:?}", args.github_cache_preference()).into(),
412415
)
413416
.await;
417+
414418
let gha_cache = if (args.github_cache_preference() == CacheTrinary::Enabled)
415419
|| (args.github_cache_preference() == CacheTrinary::NoPreference
416420
&& flakehub_state.is_none())
@@ -430,7 +434,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
430434
.with_context(|| "Failed to initialize GitHub Actions Cache API")?;
431435

432436
nix_conf
433-
.write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n", args.listen).as_bytes())
437+
.write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n", &listener_addr).as_bytes())
434438
.with_context(|| "Writing to nix.conf")?;
435439

436440
tracing::info!("Native GitHub Action cache is enabled.");
@@ -463,7 +467,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
463467
crate::pbh::subscribe_uds_post_build_hook(dnixd_uds_socket_path, state.clone()).await?;
464468
} else {
465469
tracing::info!("Patching nix.conf to use a post-build-hook.");
466-
crate::pbh::setup_legacy_post_build_hook(&args.listen, &mut nix_conf).await?;
470+
crate::pbh::setup_legacy_post_build_hook(&listener_addr, &mut nix_conf).await?;
467471
}
468472

469473
drop(nix_conf);
@@ -480,7 +484,9 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
480484

481485
let app = app.layer(Extension(state.clone()));
482486

483-
tracing::info!("Listening on {}", args.listen);
487+
let startup_blob = serde_json::json!({
488+
"address": listener_addr
489+
});
484490

485491
// Notify of startup via HTTP
486492
if let Some(startup_notification_url) = args.startup_notification_url {
@@ -489,7 +495,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
489495
let response = reqwest::Client::new()
490496
.post(startup_notification_url)
491497
.header(reqwest::header::CONTENT_TYPE, "application/json")
492-
.body("{}")
498+
.body(startup_blob.to_string())
493499
.send()
494500
.await;
495501
match response {
@@ -513,8 +519,6 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
513519

514520
// Notify of startup by writing "1" to the specified file
515521
if let Some(startup_notification_file_path) = args.startup_notification_file {
516-
let file_contents: &[u8] = b"1";
517-
518522
tracing::debug!("Startup notification via file at {startup_notification_file_path:?}");
519523

520524
if let Some(parent_dir) = startup_notification_file_path.parent() {
@@ -536,7 +540,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
536540
)
537541
})?;
538542
notification_file
539-
.write_all(file_contents)
543+
.write_all(startup_blob.to_string().as_bytes())
540544
.await
541545
.with_context(|| {
542546
format!(
@@ -548,14 +552,12 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
548552
tracing::debug!("Created startup notification file at {startup_notification_file_path:?}");
549553
}
550554

551-
let listener = tokio::net::TcpListener::bind(&args.listen).await?;
552555
let ret = axum::serve(listener, app.into_make_service())
553556
.with_graceful_shutdown(async move {
554557
shutdown_token.cancelled_owned().await;
555558
tracing::info!("Shutting down");
556559
})
557560
.await;
558-
559561
// Notify diagnostics endpoint
560562
state.metrics.send().await;
561563

0 commit comments

Comments
 (0)