Search code examples
haskellyesodhaskell-wai

Where to add 'always running' thread to Yesod applications


I'm writing a Yesod application, but it also needs to fork several non-web services. (UDP listeners, TCP listening port, etc.)

Where is the correct place to splice in a fork, such that this work seamlessly, regardless of whether my app is running in 'yesod devel' or deployed for production.

P.S. I really just want to add a pseudo-Main, which will be forked (at service start) by whichever webserver runs the app through WAI.


Solution

  • You should put it in the makeApplication function in the scaffolded Application.hs file.

    This function will run once for every instance of your web application that is started1, and since you almost always only run one instance of your application, this is the place that you should do it.

    1Note that it theoretically is possible to run multiple instances of the same WAI application in the same process, e.g. if you want two instances that listen on different ports, but by default this will never happen