I have another noobie question. After looking at last night
presentation about Rails 3, I am wondering where do we put piece of
code that runs on a separate thread for a long time every 5 seconds?
e.g
Thread.new do
while true
queue = check_whether_there_is_something_new_in_the_queue
while queue > 0
Trends.create(:data => queue.data)
end
sleep 5
end
end
1. I don't really like using cron to call `script/rails runner` as
that will load the whole rails library which will choke the server
because this process is run every 5 seconds.
2. I don't know about resque (which was mentioned by Ryan) yet, but
since resque is a separate webapp, would resque know anything about
the models in my Rails app?
Any input is highly appreciated.
Kind regards,
Josh
There's a reasonably good comparison here: http://4loc.wordpress.com/2010/03/10/background-jobs-in-ruby-on-rails/
> --
> You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
> To post to this group, send email to rails-...@googlegroups.com.
> To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.
>
If you have the choice of server, I'd consider using Thin and doing
this using EventMachine timers.
Clifford Heath.
This is sweet as it would only load the rails stack once and have
access to my models. Just like what I need. Let me try it out.
Cheers,
Joshua
Ivan
Thanks for the suggestion. Didn't thought of that also. My next
question would be, where do I put the code to start the em timers?
Initializers? Thanks for the enlightment.
Kind regards,
joshua.
It's a good question - I've done this stuff in async_sinatra not Rails
yet.
See my fork of that gem, there's an example of using an EM::Channel.
I'd start it in initializers (check that works though!) but I think
I'd want to
keep some global indicator of the timer being set, so that any code
which
accesses the Trends can verify that it's still running, and kicks it
off again.
Come to that, you might be able to start it on first use and just keep
it
running... depending on whether you need can or need to create the
interim data retroactively.
Clifford Heath.
http://github.com/cjheath
def trendy_action
something_trendy
Trend.send_later :data => data
end
Using threads in any way within a rails instance is a bad idea, because it will cause problems with passenger and friends. It might seem to work in development for a bit, but you'll definitely have problems down the road.
―ben_h
Thanks for the feedback. From what I understand delayed_job puts in a
job in a queue with the assumption that the apps that created the job
is the same rails apps that is going to consume the queue. CMIIW. My
problem is the queue is created by another external system that is
non-rails. At this point I want to evaluate bluepill and see how it
fits.
Cheers,
Josh.