I would like to automatically launch a background thread at the start of my rails app and terminate it at stop (Ctrl + C in dev mode or kill signal in production)
I’ve no problem launching my thread at start, but I can’t manage to terminate it at stop. Either it doesn’t stop, either it prevent my rails app from quitting.
Is there an automatic way ? Or should I hook the rails stop ? How ?
Thanks in advance for your advices.
P.S. By the way, do you know how to use the “pid” feature of the rails thread ? I mean the way to put a small text file in tmp/pids at start and removing it automatically. I’m sure there are some functions to do that.
(Old question, but I don’t like unanswered questions 😉
A convenient way to do this is to set “finalizers” similar to rails “initializers”. You can then launch threads in initializers and close them in finalizers.
(we use ruby “at exit”, cf. Shutdown hook for Rails)
3. Then we can use something like that in a “finalizer” : (here we attempt to stop “workers” supposedly launched in initializers)