-
Notifications
You must be signed in to change notification settings - Fork 130
Add a command to restart a deployment #687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jzeuzs
wants to merge
2
commits into
railwayapp:master
Choose a base branch
from
jzeuzs:Add-a-command-to-restart-a-deployment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| use colored::*; | ||
| use std::time::Duration; | ||
|
|
||
| use crate::{ | ||
| consts::TICK_STRING, | ||
| controllers::project::{ensure_project_and_environment_exist, get_project}, | ||
| errors::RailwayError, | ||
| util::prompt::prompt_confirm_with_default, | ||
| }; | ||
|
|
||
| use super::*; | ||
| use anyhow::{anyhow, bail}; | ||
|
|
||
| /// Restart (no image pull) the latest deployment of a service and wait for healthchecks | ||
| #[derive(Parser)] | ||
| pub struct Args { | ||
| /// The service ID/name to restart | ||
| #[clap(long, short)] | ||
| service: Option<String>, | ||
|
|
||
| /// Skip confirmation dialog | ||
| #[clap(short = 'y', long = "yes")] | ||
| bypass: bool, | ||
| } | ||
|
|
||
| pub async fn command(args: Args) -> Result<()> { | ||
| let configs = Configs::new()?; | ||
| let client = GQLClient::new_authorized(&configs)?; | ||
| let linked_project = configs.get_linked_project().await?; | ||
|
|
||
| ensure_project_and_environment_exist(&client, &configs, &linked_project).await?; | ||
|
|
||
| let project = get_project(&client, &configs, linked_project.project.clone()).await?; | ||
|
|
||
| let service_id = args.service.or_else(|| linked_project.service.clone()).ok_or_else(|| anyhow!("No service found. Please link one via `railway link` or specify one via the `--service` flag."))?; | ||
| let service = project | ||
| .services | ||
| .edges | ||
| .iter() | ||
| .find(|s| { | ||
| s.node.id == service_id || s.node.name.to_lowercase() == service_id.to_lowercase() | ||
| }) | ||
| .ok_or_else(|| anyhow!(RailwayError::ServiceNotFound(service_id)))?; | ||
|
|
||
| let service_in_env = service | ||
| .node | ||
| .service_instances | ||
| .edges | ||
| .iter() | ||
| .find(|a| a.node.environment_id == linked_project.environment) | ||
| .ok_or_else(|| anyhow!("The service specified doesn't exist in the current environment"))?; | ||
|
|
||
| if let Some(ref latest) = service_in_env.node.latest_deployment { | ||
| if latest.can_redeploy { | ||
| if !args.bypass { | ||
| let env_name = linked_project | ||
| .environment_name | ||
| .clone() | ||
| .unwrap_or("unknown".to_string()); | ||
|
|
||
| let confirmed = prompt_confirm_with_default( | ||
| format!( | ||
| "Restart the container for service {} in environment {}?", | ||
| service.node.name, env_name | ||
| ) | ||
| .as_str(), | ||
| false, | ||
| )?; | ||
|
|
||
| if !confirmed { | ||
| return Ok(()); | ||
| } | ||
| } | ||
|
|
||
| let spinner = indicatif::ProgressBar::new_spinner() | ||
| .with_style( | ||
| indicatif::ProgressStyle::default_spinner() | ||
| .tick_chars(TICK_STRING) | ||
| .template("{spinner:.green} {msg}")?, | ||
| ) | ||
| .with_message(format!("Restarting service {}...", service.node.name)); | ||
| spinner.enable_steady_tick(Duration::from_millis(100)); | ||
|
|
||
| // Call restart mutation | ||
| post_graphql::<mutations::DeploymentRestart, _>( | ||
| &client, | ||
| configs.get_backboard(), | ||
| mutations::deployment_restart::Variables { | ||
| id: latest.id.clone(), | ||
| }, | ||
| ) | ||
| .await?; | ||
|
|
||
| // Wait for healthchecks via latest deployment status | ||
| let max_wait = Duration::from_secs(300); | ||
| let poll_interval = Duration::from_secs(2); | ||
| let start = std::time::Instant::now(); | ||
| loop { | ||
| if start.elapsed() > max_wait { | ||
| spinner.finish_and_clear(); | ||
| bail!("Timed out waiting for health checks after restart"); | ||
| } | ||
|
|
||
| let resp = post_graphql::<queries::LatestDeployment, _>( | ||
| &client, | ||
| configs.get_backboard(), | ||
| queries::latest_deployment::Variables { | ||
| service_id: service.node.id.clone(), | ||
| environment_id: linked_project.environment.clone(), | ||
| }, | ||
| ) | ||
| .await?; | ||
|
|
||
| let si = resp.service_instance; | ||
| if let Some(ld) = si.latest_deployment { | ||
| match ld.status { | ||
| queries::latest_deployment::DeploymentStatus::SUCCESS => { | ||
| spinner.finish_with_message(format!( | ||
| "Restart successful for service {}", | ||
| service.node.name.green() | ||
| )); | ||
| return Ok(()); | ||
| } | ||
| queries::latest_deployment::DeploymentStatus::FAILED | ||
| | queries::latest_deployment::DeploymentStatus::CRASHED => { | ||
| spinner.finish_and_clear(); | ||
| bail!("Restart completed but health checks failed"); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| tokio::time::sleep(poll_interval).await; | ||
| } | ||
| } | ||
|
Comment on lines
+94
to
+135
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intended to be here? Deployment restarts don't perform a healthcheck, so this is not relevant |
||
| } else { | ||
| bail!("No deployment found for service") | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| #![allow(unused_imports, dead_code)] | ||
|
|
||
| pub mod mutations; | ||
| pub mod queries; | ||
| pub mod subscriptions; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| mutation DeploymentRestart($id: String!) { | ||
| deploymentRestart(id: $id) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this check for restarting