Skip to content

Commit

Permalink
Update up.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
maddsua committed Jan 21, 2024
1 parent 975a01d commit 9f49185
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/commands/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub struct Args {
/// Don't attach to the log stream
detach: bool,

#[clap(short, long)]
/// Only stream build logs and exit after it's done
cicd: bool,

#[clap(short, long)]
/// Service to deploy to (defaults to linked service)
service: Option<String>,
Expand All @@ -61,6 +65,11 @@ pub struct UpErrorResponse {
pub message: String,
}

enum UpExitReason {
DEPLOYED = 0,

Check warning on line 69 in src/commands/up.rs

View workflow job for this annotation

GitHub Actions / Lints

name `DEPLOYED` contains a capitalized acronym
FAILED = 1,

Check warning on line 70 in src/commands/up.rs

View workflow job for this annotation

GitHub Actions / Lints

name `FAILED` contains a capitalized acronym
}

pub async fn get_service_to_deploy(
configs: &Configs,
client: &Client,
Expand Down Expand Up @@ -294,34 +303,45 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
return Ok(());
}

// Stream both build and deploy logs
let build_deployment_id = deployment_id.clone();
let deploy_deployment_id = deployment_id.clone();

let tasks = vec![
tokio::task::spawn(async move {
if let Err(e) =
stream_build_logs(build_deployment_id, |log| println!("{}", log.message)).await
{
eprintln!("Failed to stream build logs: {}", e);
}
}),
tokio::task::spawn(async move {
// Create vector of log streaming tasks
// Always stream build logs
let mut tasks = vec![tokio::task::spawn(async move {
if let Err(e) =
stream_build_logs(build_deployment_id, |log| println!("{}", log.message)).await
{
eprintln!("Failed to stream build logs: {}", e);
}
})];

// Stream deploy logs only if cicd flag is not set
if !args.cicd {
tasks.push(tokio::task::spawn(async move {
if let Err(e) =
stream_deploy_logs(deploy_deployment_id, |log| println!("{}", log.message)).await
{
eprintln!("Failed to stream deploy logs: {}", e);
}
}),
];
}));
}

// If the build fails, we want to terminate the process
tokio::task::spawn(async move {
match wait_for_failure(deployment_id.clone()).await {
Ok(_) => {
println!("{}", "Build failed".red().bold());
std::process::exit(1);
}
match wait_for_exit_reason(deployment_id.clone()).await {
Ok(reason) => match reason {
UpExitReason::DEPLOYED => {
if args.cicd {
println!("{}", "Deploy complete".green().bold());
std::process::exit(0);
}
}
_ => {
println!("{}", "Build failed".red().bold());
std::process::exit(reason as i32);
}
},
Err(e) => {
eprintln!("Failed to fetch deployment status: {}", e);
}
Expand All @@ -333,19 +353,19 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
Ok(())
}

async fn wait_for_failure(deployment_id: String) -> Result<(), anyhow::Error> {
async fn wait_for_exit_reason(deployment_id: String) -> Result<UpExitReason, anyhow::Error> {
let configs = Configs::new()?;
let client = GQLClient::new_authorized(&configs)?;

loop {
tokio::time::sleep(Duration::from_secs(5)).await;

if let Ok(deployment) = get_deployment(&client, &configs, deployment_id.clone()).await {
if deployment.status == DeploymentStatus::FAILED {
break;
match deployment.status {
DeploymentStatus::SUCCESS => return Ok(UpExitReason::DEPLOYED),
DeploymentStatus::FAILED => return Ok(UpExitReason::FAILED),
_ => {}
}
}
}

Ok(())
}

0 comments on commit 9f49185

Please sign in to comment.