Skip to content

Commit

Permalink
railway up --ci now waits for healthchecks (#506)
Browse files Browse the repository at this point in the history
* new schema

* update queries

* healthchecks arent needed to be requested from API

* ci now waits for healthchecks!

* make up.rs use a subscription

* remove testing directory

* if a build fails it now has the reason from the api
  • Loading branch information
Milo123459 authored Jun 6, 2024
1 parent 8267701 commit 09ae63a
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 540 deletions.
67 changes: 30 additions & 37 deletions src/commands/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{

use anyhow::bail;

use futures::StreamExt;
use gzp::{deflate::Gzip, ZBuilder};
use ignore::WalkBuilder;
use indicatif::{ProgressBar, ProgressFinish, ProgressIterator, ProgressStyle};
Expand All @@ -16,14 +17,15 @@ use synchronized_writer::SynchronizedWriter;
use tar::Builder;

use crate::{
commands::queries::deployment::DeploymentStatus,
consts::TICK_STRING,
controllers::{
deployment::{get_deployment, stream_build_logs, stream_deploy_logs},
deployment::{stream_build_logs, stream_deploy_logs},
environment::get_matched_environment,
project::get_project,
},
errors::RailwayError,
subscription::subscribe_graphql,
subscriptions::deployment_events::DeploymentEventStep,
util::{
logs::format_attr_log,
prompt::{prompt_select, PromptService},
Expand Down Expand Up @@ -76,11 +78,6 @@ pub struct UpErrorResponse {
pub message: String,
}

enum UpExitReason {
Deployed,
Failed,
}

pub async fn get_service_to_deploy(
configs: &Configs,
client: &Client,
Expand Down Expand Up @@ -348,24 +345,37 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
}
}));
}

let mut stream = subscribe_graphql::<subscriptions::DeploymentEvents>(
subscriptions::deployment_events::Variables {
deployment_id: deployment_id.clone(),
},
)
.await?;
// If the build fails, we want to terminate the process
tokio::task::spawn(async move {
match wait_for_exit_reason(deployment_id.clone()).await {
Ok(reason) => match reason {
UpExitReason::Deployed => {
if args.ci {
println!("{}", "Deploy complete".green().bold());
std::process::exit(0);
while let Some(Ok(input)) = stream.next().await {
if let Some(data) = input.data {
if let Some(payload) = data.deployment_events.payload {
if let Some(error) = payload.error {
println!(
"{}{}",
"Build failed: {}".red().bold(),
if !error.is_empty() {
format!(": {}", error.red().bold())
} else {
String::new()
}
);
std::process::exit(1);
}
}
_ => {
println!("{}", "Build failed".red().bold());
std::process::exit(1);
if (data.deployment_events.step == DeploymentEventStep::DRAIN_INSTANCES)
&& data.deployment_events.completed_at.is_some()
&& args.ci
{
println!("{}", "Deploy complete".green().bold());
std::process::exit(0);
}
},
Err(e) => {
eprintln!("Failed to fetch deployment status: {}", e);
}
}
});
Expand All @@ -374,20 +384,3 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {

Ok(())
}

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 {
match deployment.status {
DeploymentStatus::SUCCESS => return Ok(UpExitReason::Deployed),
DeploymentStatus::FAILED => return Ok(UpExitReason::Failed),
_ => {}
}
}
}
}
24 changes: 1 addition & 23 deletions src/controllers/deployment.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
use reqwest::Client;

use crate::{
client::post_graphql,
commands::{
queries::{self},
subscriptions::{self, build_logs, deployment_logs},
Configs,
},
errors::RailwayError,
commands::subscriptions::{self, build_logs, deployment_logs},
subscription::subscribe_graphql,
};
use anyhow::{Context, Result};
use futures::StreamExt;

pub async fn get_deployment(
client: &Client,
configs: &Configs,
deployment_id: String,
) -> Result<queries::RailwayDeployment, RailwayError> {
let vars = queries::deployment::Variables { id: deployment_id };

let deployment = post_graphql::<queries::Deployment, _>(client, configs.get_backboard(), vars)
.await?
.deployment;

Ok(deployment)
}

pub async fn stream_build_logs(
deployment_id: String,
on_log: impl Fn(build_logs::LogFields),
Expand Down
9 changes: 0 additions & 9 deletions src/gql/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,6 @@ pub struct VariablesForServiceDeployment;
)]
pub struct Deployments;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/gql/schema.graphql",
query_path = "src/gql/queries/strings/Deployment.graphql",
response_derives = "Debug, Serialize, Clone, PartialEq"
)]
pub struct Deployment;
pub type RailwayDeployment = deployment::DeploymentDeployment;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "src/gql/schema.graphql",
Expand Down
6 changes: 0 additions & 6 deletions src/gql/queries/strings/Deployment.graphql

This file was deleted.

Loading

0 comments on commit 09ae63a

Please sign in to comment.