Skip to content
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

feat: show progress bar on stopping service #628

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cargo-shuttle/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Client {
.await
}

pub async fn stop_service(&self, project: &ProjectName) -> Result<service::Detailed> {
pub async fn stop_service(&self, project: &ProjectName) -> Result<service::Summary> {
let path = format!(
"/projects/{}/services/{}",
project.as_str(),
Expand Down
67 changes: 45 additions & 22 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod config;
mod factory;
mod init;

use indicatif::ProgressBar;
use shuttle_common::project::ProjectName;
use std::collections::BTreeMap;
use std::ffi::OsString;
Expand Down Expand Up @@ -267,7 +268,22 @@ impl Shuttle {
}

async fn stop(&self, client: &Client) -> Result<()> {
let service = client.stop_service(self.ctx.project_name()).await?;
let mut service = client.stop_service(self.ctx.project_name()).await?;

let progress_bar = create_spinner();
loop {
let Some(ref deployment) = service.deployment else {
break;
};

if let shuttle_common::deployment::State::Stopped = deployment.state {
break;
}

progress_bar.set_message(format!("Stopping {}", deployment.id));
service = client.get_service_summary(self.ctx.project_name()).await?;
}
progress_bar.finish_and_clear();
oddgrd marked this conversation as resolved.
Show resolved Hide resolved

println!(
r#"{}
Expand Down Expand Up @@ -591,34 +607,17 @@ impl Shuttle {
Fut: std::future::Future<Output = Result<project::Response>> + 'a,
{
let mut project = f(client, project_name).await?;
let pb = indicatif::ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::from_millis(350));
pb.set_style(
indicatif::ProgressStyle::with_template("{spinner:.orange} {msg}")
.unwrap()
.tick_strings(&[
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"( ●)",
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"(● )",
"(●●●●●●)",
]),
);

let progress_bar = create_spinner();
loop {
if states_to_check.contains(&project.state) {
break;
}

pb.set_message(format!("{project}"));
progress_bar.set_message(format!("{project}"));
project = client.get_project(project_name).await?;
}
pb.finish_and_clear();
progress_bar.finish_and_clear();
println!("{project}");
Ok(())
}
Expand Down Expand Up @@ -749,6 +748,30 @@ impl Shuttle {
}
}

fn create_spinner() -> ProgressBar {
let pb = indicatif::ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::from_millis(350));
pb.set_style(
indicatif::ProgressStyle::with_template("{spinner:.orange} {msg}")
.unwrap()
.tick_strings(&[
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"( ●)",
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"(● )",
"(●●●●●●)",
]),
);

pb
}

pub enum CommandOutcome {
Ok,
DeploymentFailure,
Expand Down
21 changes: 9 additions & 12 deletions deployer/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,16 @@ async fn post_service(
async fn stop_service(
Extension(persistence): Extension<Persistence>,
Extension(deployment_manager): Extension<DeploymentManager>,
Extension(proxy_fqdn): Extension<FQDN>,
Path((project_name, service_name)): Path<(String, String)>,
) -> Result<Json<shuttle_common::models::service::Detailed>> {
) -> Result<Json<shuttle_common::models::service::Summary>> {
if let Some(service) = persistence.get_service_by_name(&service_name).await? {
let old_deployments = persistence.get_deployments(&service.id).await?;
let running_deployment = persistence.get_active_deployment(&service.id).await?;

for deployment in old_deployments.iter() {
if let Some(ref deployment) = running_deployment {
deployment_manager.kill(deployment.id).await;
} else {
return Err(Error::NotFound);
}

let resources = persistence
Expand All @@ -268,18 +271,12 @@ async fn stop_service(
.into_iter()
.map(Into::into)
.collect();
let secrets = persistence
.get_secrets(&service.id)
.await?
.into_iter()
.map(Into::into)
.collect();

let response = shuttle_common::models::service::Detailed {
let response = shuttle_common::models::service::Summary {
name: service.name,
deployments: old_deployments.into_iter().map(Into::into).collect(),
deployment: running_deployment.map(Into::into),
resources,
secrets,
uri: format!("https://{proxy_fqdn}"),
};

Ok(Json(response))
Expand Down