Background jobs with Play Framework on Heroku

2020-07-22 19:45发布

问题:

On Heroku, and using the Play Framework, is it necessary to set up a background job processor (using Akka/RabbitMQ/etc.) in order to perform heavy tasks*?

I know that Play offers the ability to perform tasks asynchronously within requests, but would this be enough to avoid requiring a background job processor? On non-Heroku-deployed standalone Play apps, the asynchronicity features make it possible to do everything all in one process, but on Heroku apps, it seems like it would not be enough: according to the book Professional Heroku Programming, (page 254, in the Developing with Ruby section), the web dyno is blocked between the time a request is received and a response is delivered, and all other requests are queued during this time.

If a background job processor is necessary, are there any examples? I've seen examples of Play and Akka, and Play and Heroku, but not all three together.

*(By heavy tasks, I generally mean potentially long-running tasks that require an answer, such as the result of a complex database query or web-service call to be given to the end user, as opposed to fire-and-forget things like sending emails).

回答1:

You don't need an explicit worker when using Play. The common pattern with Play 2 is to use an Async response in a controller and Akka for longer running processes. All the examples on the Play Framework website should work out of the box on Heroku.