···11use eyre::{ContextCompat, Result, bail};
22-use futures_util::{Stream, StreamExt};
22+use futures_util::StreamExt;
33use luminary_macros::wrap_err;
4455use crate::core::{LuminaryAction, LuminaryEngine};
···4747 return Ok(());
4848 }
49495050- /// Waits for the given stream to complete and sets the project's action to Idle once done.
5151- /// TODO: Eventually we may want to stream this back to the client instead of just waiting.
5252- fn wait<T>(
5050+ /// A helper function to run a given command for a project and optionally, a specific service within that project.
5151+ // TODO: Eventually we may want to stream this back to the client instead of just waiting.
5252+ async fn run(
5353 &self,
5454+ action: LuminaryAction,
5455 project: String,
5556 service: Option<String>,
5656- mut stream: impl Stream<Item = T> + Unpin + Send + 'static,
5757- ) -> tokio::task::JoinHandle<()> {
5858- let this = self.clone();
5959- return tokio::spawn(async move {
6060- while let Some(_) = stream.next().await {}
6161- match this.set_action(project, service, LuminaryAction::Idle).await {
6262- Err(e) => log::error!("Failed to reset action: {:?}", e),
6363- Ok(_) => (),
6464- }
6565- });
5757+ mut args: Vec<&str>,
5858+ ) -> Result<()> {
5959+ self.set_action(project.clone(), service.clone(), action).await?;
6060+6161+ if let Some(service) = &service {
6262+ args.push(service);
6363+ }
6464+6565+ let mut stream = self.cli(&project, args)?;
6666+ while let Some(_) = stream.next().await {}
6767+ self.set_action(project, service, LuminaryAction::Idle).await?;
6868+ return Ok(());
6669 }
67706871 /// Restarts the given project and optionally, a specific service within that project.
6972 #[wrap_err("Failed to restart project/service")]
7070- pub async fn restart(
7171- &self,
7272- project: String,
7373- service: Option<String>,
7474- ) -> Result<tokio::task::JoinHandle<()>> {
7575- self.set_action(project.clone(), service.clone(), LuminaryAction::Restarting)
7373+ pub async fn restart(&self, project: String, service: Option<String>) -> Result<()> {
7474+ self.run(LuminaryAction::Restarting, project, service, vec!["restart"])
7675 .await?;
7676+ Ok(())
7777+ }
77787878- let mut args = vec!["restart"];
7979- if let Some(service) = &service {
8080- args.push(service);
8181- }
7979+ /// Starts the given project and optionally, a specific service within that project.
8080+ #[wrap_err("Failed to start project/service")]
8181+ pub async fn start(&self, project: String, service: Option<String>) -> Result<()> {
8282+ self.run(LuminaryAction::Starting, project, service, vec!["up", "-d"])
8383+ .await?;
8484+ Ok(())
8585+ }
82868383- let stream = self.cli(&project, args)?;
8484- return Ok(self.wait(project, service, stream));
8787+ /// Stops the given project and optionally, a specific service within that project.
8888+ #[wrap_err("Failed to stop project/service")]
8989+ pub async fn stop(&self, project: String, service: Option<String>) -> Result<()> {
9090+ self.run(LuminaryAction::Stopping, project, service, vec!["down"])
9191+ .await?;
9292+ Ok(())
8593 }
8694}
+2
packages/node/src/core/model.rs
···159159pub enum LuminaryAction {
160160 Idle,
161161 Restarting,
162162+ Stopping,
163163+ Starting,
162164}
163165164166/// Stores the log channel and buffer for a project.