From 1c83c37f4e89086a4116db39dbcccc9f182fd528 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Wed, 24 Apr 2024 07:51:51 -0700 Subject: [PATCH] wip: more tracing, sleep at first --- magic-nix-cache/src/flakehub.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/magic-nix-cache/src/flakehub.rs b/magic-nix-cache/src/flakehub.rs index 8544f80..a987f13 100644 --- a/magic-nix-cache/src/flakehub.rs +++ b/magic-nix-cache/src/flakehub.rs @@ -196,6 +196,7 @@ pub async fn enqueue_paths(state: &State, store_paths: Vec) -> Result /// Refresh the GitHub Actions JWT every 2 minutes (slightly less than half of the default validity /// period) to ensure pushing / pulling doesn't stop working. +#[tracing::instrument(skip_all)] async fn refresh_github_actions_jwt_worker( netrc_path: std::path::PathBuf, mut github_jwt: String, @@ -207,6 +208,10 @@ async fn refresh_github_actions_jwt_worker( // breaks this. let next_refresh = std::time::Duration::from_secs(2 * 60); + // NOTE(cole-h): we sleep until the next refresh at first because we already got a token from + // GitHub recently, don't need to try again until we actually might need to get a new one. + tokio::time::sleep(next_refresh).await; + // NOTE(cole-h): https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-cloud-providers#requesting-the-jwt-using-environment-variables let mut headers = reqwest::header::HeaderMap::new(); headers.insert( @@ -257,6 +262,7 @@ async fn refresh_github_actions_jwt_worker( } } +#[tracing::instrument(skip_all)] async fn rewrite_github_actions_token( client: &reqwest::Client, netrc_path: &Path, @@ -319,7 +325,9 @@ async fn rewrite_github_actions_token( "symlink_metadata(netrc_path): {:?}", tokio::fs::symlink_metadata(&netrc_path).await ); + let new_netrc_contents = netrc_contents.replace(old_github_jwt, &new_github_jwt_string); + // NOTE(cole-h): create the temporary file right next to the real one so we don't run into // cross-device linking issues when renaming let netrc_path_tmp = netrc_path.with_extension("tmp"); @@ -335,6 +343,7 @@ async fn rewrite_github_actions_token( "symlink_metadata(netrc_path_tmp): {:?}", tokio::fs::symlink_metadata(&netrc_path_tmp).await ); + tokio::fs::rename(&netrc_path_tmp, &netrc_path) .await .with_context(|| format!("renaming {netrc_path_tmp:?} to {netrc_path:?}"))?;