Skip to content

Is there an idiomatic way to periodically execute a method on the app's state? #768

Answered by yoshuawuyts
ohmree asked this question in Q&A
Discussion options

You must be logged in to vote

The way I'd approach this would be to create a copy of State on creation and move it into a separate task which can then indeed operate on a loop. State is already required to be Clone + Send, so that shouldn't be a problem. I'd imagine this would look something like this:

// Create an instance of your `State`
let state = State::new();

// Clone your `State` and pass it into a task where we'll perform the periodic cleanup
let cleanup_state = state.clone();
let handle = task::spawn(async move {
    while let Some(_) = async_std::task::sleep(Duration::from_secs(10)) {
        // SQL cleanup here
        cleanup_state.db().cleanup().await?;
    }
    Ok(())
});

// Create the tide app, and l…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ohmree
Comment options

Answer selected by ohmree
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
Converted from issue

This discussion was converted from issue #768 on December 18, 2020 22:53.