Skip to content

How to save the data when the tonic is closed! #725

Answered by davidpdrsn
xiuno asked this question in Q&A
Discussion options

You must be logged in to vote

The data is in an Arc so you can share it between the GameServer and whatever code runs after the server has been shutdown.

I haven't tested this code but should work

#[tokio::main]
async fn main() {
    // pull this out into a variable so it can be shared between tasks
    let data = Arc::new(Mutex::new(vec![]));

    let game = Game { data: Arc::clone(&data) };
    let mut route = Server::builder().add_service(GameServer::new(game));

    // register callback when shutdown
    route.serve_with_shutdown(addr, async move {
        tokio::signal::ctrl_c().await.unwrap();
    }).await.unwrap();

    // somehow actually save the data
    let data = data.lock();
    println!("saving {:?}", data)

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@davidpdrsn
Comment options

@xiuno
Comment options

@davidpdrsn
Comment options

@xiuno
Comment options

@xiuno
Comment options

Answer selected by davidpdrsn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants