1

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!

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

can you see my comment on the other answer? could dj handle background data structures?
Can you clarify or have an example of your data structure? Is it a hash or something that's loaded with the user or does it live in memory across all requests? With several of the background job apps, you can pass in arguments that are then serialized into the database and reloaded.
Yes. So it will most likely be a large tree type structure that is constantly being modified. Do you think that's possible? thanks!
Which one ;) If it's a hash in memory, there's most likely going to be issues if you run unicorn/mongrel with multiple instances as they can't access each other's mem. But if it's in the db, then anything, including the background workers can access it.
1

Have a look at Workling and Starling.

1 Comment

So I've looked at them, and I understand the background jobs/methods you can run, but I am unsure as to how this would allow me to have a persistent data structure running in the background. Imagine a big binary tree that I want to be able to update constantly. Any ideas?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.