Rails background process/data structure
I am running a rails app, which 开发者_运维技巧needs to have a continuous process in the background. The idea is that users are transactionally added to a list, which is inside the process, and then paired together according to an algorithm that runs continuously.
I can't make the algorithm simply run after each user is added because we also need to run the algorithm if a user has been waiting for a certain amount of time.
Is this possible? I basically need a data structure that is constantly running and being accessed at diff times in the background.
Thanks!
delayed_job works well and is super easy to integrate. You can even arbitrarily make anything run in the background. It loads the rails app as workers and keeps them in memory, unlike script/runner which constantly loads and unloads the app. DJ watches the db to look for jobs. I even setup a site where another server was processing the background tasks separate from the app server.
Have a look at Workling and Starling.
You could do it using cron and/or a shell script or batch file, and rake or Rail's script runner
.
With cron you could specify intervals the job runs at, say every five minutes. Or, you could write a script that gets launched at system startup, then sleeps for a while, wakes up and runs your code, then goes back to sleep again, looping indefinitely.
Or you could write a loop in your script that runs your actions then sleeps internally, waking and running again, then sleeping again.
The advantage to using Rail's script runner
is it has access to everything your Rails app is aware of. The disadvantage is the app takes longer to start because it has to run through the Rails initialization routines every time it's launched, and which is why I mentioned looping over doing something then sleeping.
精彩评论