Skip to content

Async jobs#540

Closed
ivan770 wants to merge 5 commits intohttp-rs:mainfrom
ivan770:master
Closed

Async jobs#540
ivan770 wants to merge 5 commits intohttp-rs:mainfrom
ivan770:master

Conversation

@ivan770
Copy link

@ivan770 ivan770 commented May 24, 2020

Currently, you can access shared state only in request scope.

This causes a lot of troubles, when you, for example, need to save state to file based on timer, or do any actions with state without HTTP request.

This PR introduces Job and JobContext, that represent async job and job context with app state respectively.

API is pretty straightforward:

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
    let mut app = tide::new();

    app.at("/").get(|_| async move {
        Ok("I am executed on HTTP request")
    });

    app.spawn(|_| async move {
        println!("I am executed when you call 'listen'!");
    });

    app.listen("127.0.0.1:8080").await?;
    Ok(())
}

Example with app state

@goto-bus-stop goto-bus-stop changed the base branch from master to main August 9, 2020 12:56
@Fishrock123
Copy link
Member

I think this was solved by #675 - you can now access state again off of the server, and so you could make an external task which does this.

Please let me know if I missed something though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants