views:

32

answers:

1

I need to query mulitple apis to get certain data for each user. I need to do this as a background task. Now there are a lot of users. And ideally I would like all this to happen in parallel. So I could insert a job of each user/api combination. And say have 4 delayed_job workers process all the jobs.

The problem I am facing with this, each DJ worker loads up the entire Rails instance and is consuming 50Mb of memory. Thats 200Mb straight away gone to background workers. Why does each worker need its own copy of my Rails app? Why cant they all share one copy? Why cant they share the copy that passenger uses?

I am currently using Ruby 1.9.1, but I can switch to REE.

My main question is this:

Is there any background task gem/plugin for rails that can spawn multiple workers and yet consume as less memory as possible?

A: 

You'll need REE to do it (since it's copy-on-write friendly, which MRI is not), but you could easily achieve this by starting up a spawner process with script/runner and then forking off DelayedJob workers. That would let you share the rails environment and most of the framework memory usage between the DJ workers.

Chris Heald
Could you elaborate your answer a bit? If I understand correctly, you are saying that I start a custom ruby script using script runner. This will load the Rails environment in its memory. Then by using some mechanism, I start processes of delayed_job. As in child processes. Because the DJ processes are child processes, they can all access the Rails instance in the memory. Ok. But, there already is a script called delayed_job under the scripts folder that starts DJ as a daemon. It makes it easy to start/stop DJ. Can that be modified to use the same concept?
Amith George
And regarding REE, simply by using REE, will the existent script/delayed_job automatically share the Rails app code? Somehow that doesnt seem to make sense. Am using the collective_ideas fork of DJ. and their script to start/stop DJ.
Amith George
http://github.com/collectiveidea/delayed_job/issues#issue/25 is an issue related to replacing script/delayed_job with a pre-forking worker. Is a pre-forking worker similar to what you are proposing?
Amith George