Skip to main content

actr_cli/commands/
start.rs

1//! Start command - re-launch a stopped detached runtime instance
2
3use crate::commands::run::RunCommand;
4use crate::commands::runtime_state::{RuntimeStateStore, RuntimeStatus, resolve_hyper_dir};
5use crate::core::{Command, CommandContext, CommandResult, ComponentType};
6use crate::error::ActrCliError;
7use anyhow::Result;
8use async_trait::async_trait;
9use clap::Args;
10use std::path::PathBuf;
11
12#[derive(Args, Debug)]
13pub struct StartCommand {
14    /// WID (or unique prefix, min 8 chars) of the runtime to start
15    #[arg(value_name = "WID")]
16    pub wid: String,
17
18    /// Override runtime configuration file
19    #[arg(short = 'c', long = "config", value_name = "FILE")]
20    pub config: Option<PathBuf>,
21
22    /// Hyper data directory
23    #[arg(long = "hyper-dir", value_name = "DIR")]
24    pub hyper_dir: Option<PathBuf>,
25}
26
27#[async_trait]
28impl Command for StartCommand {
29    async fn execute(&self, ctx: &CommandContext) -> Result<CommandResult> {
30        let hyper_dir = resolve_hyper_dir(self.config.as_deref(), self.hyper_dir.as_deref())?;
31        let store = RuntimeStateStore::new(hyper_dir);
32        let entry = store.resolve_wid_prefix(&self.wid).await?;
33
34        if entry.status == RuntimeStatus::Running {
35            return Err(ActrCliError::command_error(format!(
36                "Runtime {} is already running (pid {}). Use `restart` to restart it.",
37                entry.wid_short(),
38                entry.record.pid
39            ))
40            .into());
41        }
42
43        let config_path = self
44            .config
45            .clone()
46            .unwrap_or_else(|| entry.record.config_path.clone());
47
48        RunCommand {
49            config: Some(config_path),
50            hyper_dir: self.hyper_dir.clone(),
51            detach: true,
52            internal_detached_child: false,
53            internal_wid: Some(entry.record.wid.clone()),
54            web: false,
55            port: None,
56        }
57        .execute(ctx)
58        .await
59    }
60
61    fn required_components(&self) -> Vec<ComponentType> {
62        vec![]
63    }
64
65    fn name(&self) -> &str {
66        "start"
67    }
68
69    fn description(&self) -> &str {
70        "Start a stopped detached runtime instance"
71    }
72}