Skip to content

Commit

Permalink
Merge pull request #843 from hendrikmaus/#788_retry-publish
Browse files Browse the repository at this point in the history
feat: try to publish a crate until it succeeds
  • Loading branch information
hannobraun authored Jul 20, 2022
2 parents ff55533 + 73b9da0 commit 80bc6ce
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions tools/release-operator/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::{Display, Formatter};
use std::path::PathBuf;
use std::process::Command;
use std::str::FromStr;
use std::thread::sleep;
use std::time::{Duration, Instant};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -168,30 +169,52 @@ impl Crate {
fn submit(&self, token: &SecUtf8, dry_run: bool) -> anyhow::Result<()> {
log::info!("{self} publishing new version");

let mut command = Command::new("cargo");
command
.arg("publish")
.args(["--token", token.unsecure()])
.current_dir(&self.path);
let ours = self.get_local_version()?;
let delay = Duration::from_secs(10);
let start_time = Instant::now();
let timeout = Duration::from_secs(600);
let mut attempt = 1;
let maximum_attempts = 10;

if dry_run {
command.arg("--dry-run");
}
loop {
if Instant::now() - start_time > timeout {
bail!("{self} could not be published to the registry within {timeout:?}");
}

let exit_status = command.status()?;
if attempt > maximum_attempts {
bail!("{self} could not be published to the registry; tried {attempt} times");
}

if !exit_status.success() {
match exit_status.code() {
Some(code) => {
bail!("`cargo publish` failed with exit code {code}")
}
None => {
bail!("`cargo publish` was terminated by signal")
let mut command = Command::new("cargo");
command
.arg("publish")
.args(["--token", token.unsecure()])
.current_dir(&self.path);

if dry_run {
command.arg("--dry-run");
}

let exit_status = command.status()?;

if !exit_status.success() {
match exit_status.code() {
Some(code) => {
log::warn!("`cargo publish` failed with exit code {code}; trying again in {delay:?}");
sleep(delay);
attempt += 1;
continue;
}
None => {
bail!("`cargo publish` was terminated by signal")
}
}
}

log::info!("{self} published as {ours}");
break;
}

let ours = self.get_local_version()?;
let delay = Duration::from_secs(10);
let start_time = Instant::now();
let timeout = Duration::from_secs(600);
Expand Down

0 comments on commit 80bc6ce

Please sign in to comment.