astra icon indicating copy to clipboard operation
astra copied to clipboard

How to shut down the server?

Open super1207 opened this issue 11 months ago • 3 comments

super1207 avatar Feb 27 '25 06:02 super1207

There's currently no way to do this. I'd like to know your use case to design a useful API that allows for (graceful?) shutdown.

ibraheemdev avatar Feb 27 '25 06:02 ibraheemdev

I want to create a dynamic library loading/unloading tool, which starts a network service when the dynamic library is loaded, and shuts down the network service and releases the port before the dynamic library is unloaded.

Similar to the following code, or is there a better approach?

// static mut SERVER_HANDLE: Option<Arc<ServerHandle>> = None;

#[no_mangle]
pub extern "system" fn plugin_init() {
    thread::spawn(||{
        let server = Server::bind("localhost:3000");

        //  SERVER_HANDLE = Some(server);

        server.serve(|_req, info|{
            ResponseBuilder::new()
            .header("Content-Type", "text/html")
            .body(Body::new("hello world"))
            .unwrap()
        }).unwrap();
    });
    println!("Plugin initialized");
}


#[no_mangle]
pub extern "system" fn plugin_quit() {
    // Do some cleanup here
    // if let Some(handle) = &SERVER_HANDLE {
    //     handle.quit();
    // }
    println!("Plugin quited");
}

super1207 avatar Feb 27 '25 07:02 super1207

We could probably expose an incoming requests iterator to allow users to control the server loop, and use signal handling to exit as necessary. We would also need an API to wait/timeout on active requests in the thread pool.

ibraheemdev avatar Sep 23 '25 05:09 ibraheemdev