tsk/commands/
stop_server.rs

1use super::Command;
2use crate::context::AppContext;
3use async_trait::async_trait;
4use std::error::Error;
5
6pub struct StopServerCommand;
7
8#[async_trait]
9impl Command for StopServerCommand {
10    async fn execute(&self, ctx: &AppContext) -> Result<(), Box<dyn Error>> {
11        println!("Stopping TSK server...");
12
13        let client = ctx.tsk_client();
14
15        if !client.is_server_available().await {
16            println!("Server is not running");
17            return Ok(());
18        }
19
20        match client.shutdown_server().await {
21            Ok(_) => {
22                println!("Server shutdown command sent successfully");
23
24                // Wait a bit for the server to shut down
25                tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
26
27                if client.is_server_available().await {
28                    eprintln!("Warning: Server may still be running");
29                } else {
30                    println!("Server stopped successfully");
31                }
32            }
33            Err(e) => {
34                eprintln!("Failed to stop server: {e}");
35                return Err(Box::new(std::io::Error::other(e.to_string())));
36            }
37        }
38
39        Ok(())
40    }
41}