Skip to content

Commit 9731df2

Browse files
Fix listening on port 0
1 parent 59f4e54 commit 9731df2

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

magic-nix-cache/src/main.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
411411
format!("{:?}", args.github_cache_preference()).into(),
412412
)
413413
.await;
414+
414415
let gha_cache = if (args.github_cache_preference() == CacheTrinary::Enabled)
415416
|| (args.github_cache_preference() == CacheTrinary::NoPreference
416417
&& flakehub_state.is_none())
@@ -429,10 +430,6 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
429430
)
430431
.with_context(|| "Failed to initialize GitHub Actions Cache API")?;
431432

432-
nix_conf
433-
.write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n", args.listen).as_bytes())
434-
.with_context(|| "Writing to nix.conf")?;
435-
436433
tracing::info!("Native GitHub Action cache is enabled.");
437434
Some(gha_cache)
438435
} else {
@@ -458,16 +455,6 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
458455
shutdown_token: shutdown_token.clone(),
459456
});
460457

461-
if dnixd_available == Dnixd::Available {
462-
tracing::info!("Subscribing to Determinate Nixd build events.");
463-
crate::pbh::subscribe_uds_post_build_hook(dnixd_uds_socket_path, state.clone()).await?;
464-
} else {
465-
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?;
467-
}
468-
469-
drop(nix_conf);
470-
471458
let app = Router::new()
472459
.route("/", get(root))
473460
.merge(api::get_router())
@@ -480,7 +467,31 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
480467

481468
let app = app.layer(Extension(state.clone()));
482469

483-
tracing::info!("Listening on {}", args.listen);
470+
let listener = tokio::net::TcpListener::bind(&args.listen).await?;
471+
let serve = axum::serve(listener, app.into_make_service()).with_graceful_shutdown(async move {
472+
shutdown_token.cancelled_owned().await;
473+
tracing::info!("Shutting down");
474+
});
475+
476+
let addr = serve.local_addr().unwrap();
477+
478+
tracing::info!("Listening on {addr}");
479+
480+
if state.gha_cache.is_some() {
481+
nix_conf
482+
.write_all(format!("extra-substituters = http://{addr}?trusted=1&compression=zstd&parallel-compression=true&priority=1\n").as_bytes())
483+
.with_context(|| "Writing to nix.conf")?;
484+
}
485+
486+
if dnixd_available == Dnixd::Available {
487+
tracing::info!("Subscribing to Determinate Nixd build events.");
488+
crate::pbh::subscribe_uds_post_build_hook(dnixd_uds_socket_path, state.clone()).await?;
489+
} else {
490+
tracing::info!("Patching nix.conf to use a post-build-hook.");
491+
crate::pbh::setup_legacy_post_build_hook(&addr, &mut nix_conf).await?;
492+
}
493+
494+
drop(nix_conf);
484495

485496
// Notify of startup via HTTP
486497
if let Some(startup_notification_url) = args.startup_notification_url {
@@ -489,7 +500,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
489500
let response = reqwest::Client::new()
490501
.post(startup_notification_url)
491502
.header(reqwest::header::CONTENT_TYPE, "application/json")
492-
.body("{}")
503+
.body(format!("{{\"address\": \"{addr}\"}}"))
493504
.send()
494505
.await;
495506
match response {
@@ -513,7 +524,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
513524

514525
// Notify of startup by writing "1" to the specified file
515526
if let Some(startup_notification_file_path) = args.startup_notification_file {
516-
let file_contents: &[u8] = b"1";
527+
let file_contents = format!("{addr}");
517528

518529
tracing::debug!("Startup notification via file at {startup_notification_file_path:?}");
519530

@@ -536,7 +547,7 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
536547
)
537548
})?;
538549
notification_file
539-
.write_all(file_contents)
550+
.write_all(file_contents.as_bytes())
540551
.await
541552
.with_context(|| {
542553
format!(
@@ -548,18 +559,10 @@ async fn main_cli(args: Args, recorder: detsys_ids_client::Recorder) -> Result<(
548559
tracing::debug!("Created startup notification file at {startup_notification_file_path:?}");
549560
}
550561

551-
let listener = tokio::net::TcpListener::bind(&args.listen).await?;
552-
let ret = axum::serve(listener, app.into_make_service())
553-
.with_graceful_shutdown(async move {
554-
shutdown_token.cancelled_owned().await;
555-
tracing::info!("Shutting down");
556-
})
557-
.await;
558-
559562
// Notify diagnostics endpoint
560563
state.metrics.send().await;
561564

562-
ret?;
565+
serve.await?;
563566

564567
Ok(())
565568
}

0 commit comments

Comments
 (0)