From 8ac2e6ef55507a8d8ddc291a2f6ee685ef66f665 Mon Sep 17 00:00:00 2001 From: Eric Swanson Date: Mon, 16 Nov 2020 12:06:34 -0800 Subject: [PATCH] refactor: hoist runtime creation into command exec functions --- src/dfx/src/commands/canister/create.rs | 7 +++-- src/dfx/src/commands/deploy.rs | 11 +++++++- .../operations/canister/create_canister.rs | 16 +++++++----- .../operations/canister/deploy_canisters.rs | 26 +++++++++---------- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/dfx/src/commands/canister/create.rs b/src/dfx/src/commands/canister/create.rs index 72cda8011b..7174e2a957 100644 --- a/src/dfx/src/commands/canister/create.rs +++ b/src/dfx/src/commands/canister/create.rs @@ -3,6 +3,7 @@ use crate::lib::error::{DfxError, DfxResult}; use crate::lib::operations::canister::create_canister; use crate::util::expiry_duration; use clap::{App, ArgMatches, Clap, FromArgMatches, IntoApp}; +use tokio::runtime::Runtime; /// Creates an empty canister on the Internet Computer and /// associates the Internet Computer assigned Canister ID to the canister name. @@ -29,14 +30,16 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches) -> DfxResult { let timeout = expiry_duration(); + let mut runtime = Runtime::new().expect("Unable to create a runtime"); + if let Some(canister_name) = opts.canister_name { - create_canister(env, canister_name.as_str(), timeout)?; + runtime.block_on(create_canister(env, canister_name.as_str(), timeout))?; Ok(()) } else if opts.all { // Create all canisters. if let Some(canisters) = &config.get_config().canisters { for canister_name in canisters.keys() { - create_canister(env, canister_name, timeout)?; + runtime.block_on(create_canister(env, canister_name, timeout))?; } } Ok(()) diff --git a/src/dfx/src/commands/deploy.rs b/src/dfx/src/commands/deploy.rs index 77ea672735..c798a03224 100644 --- a/src/dfx/src/commands/deploy.rs +++ b/src/dfx/src/commands/deploy.rs @@ -4,6 +4,7 @@ use crate::lib::operations::canister::deploy_canisters; use crate::lib::provider::create_agent_environment; use crate::util::expiry_duration; use clap::{App, ArgMatches, Clap, FromArgMatches, IntoApp}; +use tokio::runtime::Runtime; /// Deploys all or a specific canister from the code in your project. By default, all canisters are deployed. #[derive(Clap)] @@ -39,5 +40,13 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches) -> DfxResult { let argument = opts.argument.as_deref(); let argument_type = opts.argument_type.as_deref(); - deploy_canisters(&env, canister_name, argument, argument_type, timeout) + let mut runtime = Runtime::new().expect("Unable to create a runtime"); + + runtime.block_on(deploy_canisters( + &env, + canister_name, + argument, + argument_type, + timeout, + )) } diff --git a/src/dfx/src/lib/operations/canister/create_canister.rs b/src/dfx/src/lib/operations/canister/create_canister.rs index f1032d4e95..e07e235b98 100644 --- a/src/dfx/src/lib/operations/canister/create_canister.rs +++ b/src/dfx/src/lib/operations/canister/create_canister.rs @@ -9,9 +9,12 @@ use ic_utils::interfaces::ManagementCanister; use slog::info; use std::format; use std::time::Duration; -use tokio::runtime::Runtime; -pub fn create_canister(env: &dyn Environment, canister_name: &str, timeout: Duration) -> DfxResult { +pub async fn create_canister( + env: &dyn Environment, + canister_name: &str, + timeout: Duration, +) -> DfxResult { let log = env.get_logger(); info!(log, "Creating canister {:?}...", canister_name); @@ -45,11 +48,10 @@ pub fn create_canister(env: &dyn Environment, canister_name: &str, timeout: Dura .ok_or(DfxError::CommandMustBeRunInAProject)?, ); - let mut runtime = Runtime::new().expect("Unable to create a runtime"); - let (cid,) = runtime.block_on( - mgr.create_canister() - .call_and_wait(waiter_with_timeout(timeout)), - )?; + let (cid,) = mgr + .create_canister() + .call_and_wait(waiter_with_timeout(timeout)) + .await?; let canister_id = cid.to_text(); info!( log, diff --git a/src/dfx/src/lib/operations/canister/deploy_canisters.rs b/src/dfx/src/lib/operations/canister/deploy_canisters.rs index 375804cd61..50a554e4e5 100644 --- a/src/dfx/src/lib/operations/canister/deploy_canisters.rs +++ b/src/dfx/src/lib/operations/canister/deploy_canisters.rs @@ -14,9 +14,8 @@ use ic_utils::interfaces::management_canister::{ComputeAllocation, InstallMode, use slog::{info, warn}; use std::convert::TryFrom; use std::time::Duration; -use tokio::runtime::Runtime; -pub fn deploy_canisters( +pub async fn deploy_canisters( env: &dyn Environment, some_canister: Option<&str>, argument: Option<&str>, @@ -37,7 +36,7 @@ pub fn deploy_canisters( info!(log, "Deploying all canisters."); } - register_canisters(env, &canister_names, &initial_canister_id_store, timeout)?; + register_canisters(env, &canister_names, &initial_canister_id_store, timeout).await?; build_canisters(env, &canister_names, &config)?; @@ -49,7 +48,8 @@ pub fn deploy_canisters( argument, argument_type, timeout, - )?; + ) + .await?; info!(log, "Deployed canisters."); @@ -64,7 +64,7 @@ fn canisters_to_deploy(config: &Config, some_canister: Option<&str>) -> DfxResul Ok(canister_names) } -fn register_canisters( +async fn register_canisters( env: &dyn Environment, canister_names: &[String], canister_id_store: &CanisterIdStore, @@ -80,7 +80,7 @@ fn register_canisters( } else { info!(env.get_logger(), "Creating canisters..."); for canister_name in &canisters_to_create { - create_canister(env, &canister_name, timeout)?; + create_canister(env, &canister_name, timeout).await?; } } Ok(()) @@ -94,7 +94,7 @@ fn build_canisters(env: &dyn Environment, canister_names: &[String], config: &Co canister_pool.build_or_fail(BuildConfig::from_config(&config)?) } -fn install_canisters( +async fn install_canisters( env: &dyn Environment, canister_names: &[String], initial_canister_id_store: &CanisterIdStore, @@ -109,8 +109,6 @@ fn install_canisters( .get_agent() .ok_or(DfxError::CommandMustBeRunInAProject)?; - let mut runtime = Runtime::new().expect("Unable to create a runtime"); - let canister_id_store = CanisterIdStore::for_env(env)?; for canister_name in canister_names { @@ -143,7 +141,7 @@ fn install_canisters( .expect("Memory allocation must be between 0 and 2^48 (i.e 256TB), inclusively.") }); - let result = runtime.block_on(install_canister( + let result = install_canister( env, &agent, &canister_info, @@ -152,7 +150,8 @@ fn install_canisters( first_mode, memory_allocation, timeout, - )); + ) + .await; match result { Err(DfxError::AgentError(AgentError::ReplicaError { reject_code, @@ -168,7 +167,7 @@ fn install_canisters( env.get_logger(), "replica error. attempting {}", mode_description ); - runtime.block_on(install_canister( + install_canister( env, &agent, &canister_info, @@ -177,7 +176,8 @@ fn install_canisters( second_mode, memory_allocation, timeout, - )) + ) + .await } other => other, }?;