Skip to content

Commit

Permalink
feat(rust): simplify node create execution
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Dec 13, 2024
1 parent 441bacf commit 6108b3d
Show file tree
Hide file tree
Showing 19 changed files with 513 additions and 462 deletions.
33 changes: 26 additions & 7 deletions implementations/rust/ockam/ockam_api/src/cli_state/cli_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tokio::sync::broadcast::{channel, Receiver, Sender};

use ockam::SqlxDatabase;
use ockam_core::env::get_env_with_default;
use ockam_node::database::DatabaseConfiguration;
use ockam_node::database::{DatabaseConfiguration, OCKAM_IN_MEMORY};
use ockam_node::Executor;

use crate::cli_state::error::Result;
Expand Down Expand Up @@ -71,6 +71,13 @@ impl CliState {
Self::make_database_configuration(&self.dir)
}

pub fn is_using_in_memory_database(&self) -> Result<bool> {
match self.database_configuration()? {
DatabaseConfiguration::SqliteInMemory { .. } => Ok(true),
_ => Ok(false),
}
}

pub fn is_database_path(&self, path: &Path) -> bool {
let database_configuration = self.database_configuration().ok();
match database_configuration {
Expand Down Expand Up @@ -248,9 +255,15 @@ impl CliState {
pub(super) fn make_database_configuration(root_path: &Path) -> Result<DatabaseConfiguration> {
match DatabaseConfiguration::postgres()? {
Some(configuration) => Ok(configuration),
None => Ok(DatabaseConfiguration::sqlite(
root_path.join("database.sqlite3").as_path(),
)),
None => {
if get_env_with_default::<bool>(OCKAM_IN_MEMORY, false)? {
Ok(DatabaseConfiguration::sqlite_in_memory())
} else {
Ok(DatabaseConfiguration::sqlite(
root_path.join("database.sqlite3").as_path(),
))
}
}
}
}

Expand All @@ -260,9 +273,15 @@ impl CliState {
) -> Result<DatabaseConfiguration> {
match DatabaseConfiguration::postgres()? {
Some(configuration) => Ok(configuration),
None => Ok(DatabaseConfiguration::sqlite(
root_path.join("application_database.sqlite3").as_path(),
)),
None => {
if get_env_with_default::<bool>(OCKAM_IN_MEMORY, false)? {
Ok(DatabaseConfiguration::sqlite_in_memory())
} else {
Ok(DatabaseConfiguration::sqlite(
root_path.join("application_database.sqlite3").as_path(),
))
}
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions implementations/rust/ockam/ockam_api/src/nodes/models/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ use std::fmt::{Display, Formatter};
pub struct NodeStatus {
#[n(1)] pub name: String,
#[n(2)] pub identifier: Identifier,
#[n(3)] pub status: NodeProcessStatus,
#[n(3)] pub process_status: NodeProcessStatus,
}

impl NodeStatus {
pub fn new(name: impl Into<String>, identifier: Identifier, status: NodeProcessStatus) -> Self {
pub fn new(
name: impl Into<String>,
identifier: Identifier,
process_status: NodeProcessStatus,
) -> Self {
Self {
name: name.into(),
identifier,
status,
process_status,
}
}
}
Expand All @@ -41,7 +45,7 @@ impl From<&NodeInfo> for NodeStatus {
Self {
name: node.name(),
identifier: node.identifier(),
status: node.status(),
process_status: node.status(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;

use miette::{miette, IntoDiagnostic};
use minicbor::{Decode, Encode};
use ockam::identity::get_default_timeout;

use ockam::identity::get_default_timeout;
use ockam::tcp::{TcpConnection, TcpConnectionOptions, TcpTransport};
use ockam_core::api::{Reply, Request};
use ockam_core::Route;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use colorful::Colorful;
use miette::{miette, IntoDiagnostic, WrapErr};
use serde::{Deserialize, Serialize};
use tokio::fs::read_to_string;
use tokio::process::Child;
use tokio_retry::strategy::FixedInterval;
use tokio_retry::Retry;
use tracing::{debug, error, info};
Expand Down Expand Up @@ -140,7 +141,7 @@ impl CreateCommand {
pub(crate) async fn spawn_background_node(
&self,
opts: &CommandGlobalOpts,
) -> miette::Result<()> {
) -> miette::Result<Child> {
if !self.skip_is_running_check {
self.guard_node_is_not_already_running(opts).await?;
}
Expand Down Expand Up @@ -282,7 +283,8 @@ impl CreateCommand {
/// Given a Context start a node in a new OS process
async fn create_background_node(&self, opts: CommandGlobalOpts) -> miette::Result<()> {
// Spawn node in another, new process
self.spawn_background_node(&opts).await
self.spawn_background_node(&opts).await?;
Ok(())
}

/// Start an authority node:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct CommandGlobalOpts {
pub state: CliState,
pub terminal: Terminal<TerminalStream<Term>>,
pub rt: Arc<Runtime>,
tracing_guard: Option<Arc<TracingGuard>>,
pub tracing_guard: Option<Arc<TracingGuard>>,
}

impl CommandGlobalOpts {
Expand Down
Loading

0 comments on commit 6108b3d

Please sign in to comment.