I decided to use BackgrounDrb 0.2.1 to launch a periodic task that would run every 15 seconds.Warning: After going through the hassle of setting up BackgrounDrb, I found out that it isn’t even supported in Windows (my dev environment)! See step 8. So, these instructions haven’t been fully tested yet on a *.nix
environment. Good luck and please post comments if you have any helpful suggestions.
- Install BackgroundDrb:
svn co http://svn.devjavu.com/backgroundrb/tags/release-0.2.1 backgroundrb
- Setup BackgrounDrb:
rake backgroundrb:setup
- Generate worker thread (
Foo
is capitalized):ruby script/generate worker Foo
- In one of your controllers, create new worker with
Middleman
object (:foo_worker
is lowercase with underscore)MiddleMan.new_worker(:class => :foo_worker, :args => "Arguments used to instantiate a new HeartratepostWorker object",
- Instantiate
Middleman
object inenvironment.rb (you might not need this with version 0.2.1 of BackgrounDrb)
require "drb" DRb.start_service MiddleMan = DRbObject.new(nil, "druby://localhost:22222"
- Install the daemons gem if you haven’t already:
gem install daemons
- Install the slave gem if you haven’t already:
gem install slave
- Start the BackgroundDrb server in a separate command window:
ruby script\backgroundrb start
- If you get the error “
ERROR: there is already one or more instance(s) of the program running
”, deletelog\backgroundrb.pid
- Windows users are out of luck! You’ll get this message:
`fork': the fork() function is unimplemented on this machine
. Here’s a message from Ezra (the devleoper of BackgroundDrb) about Windows support:- “Unfortunately that won’t cut it in this case, I wish it would. Slave does more then just fork. It uses ipc and a few other things that just don’t work yet on windows. There is a possibility it could be made to work but right now it does not, even with win32 process (gem). -Ezra Wed Nov 29 16:19:03 EST 2006”
- If you get the error “
- Debugging methods if things don’t work well
- Check out
log/backgroundrb.log
- BackgroundDrb mailing list
- See examples in
vendor/plugins/backgroundrb/misc/examples
- Check out
Notes
- You might want to restart the backgroundrb server after you’ve modified the
*_worker.rb
file tolib/workers
. It could cause an error otherwise. - In case you get this error when starting Mongel, copy the
vendor/plugins/backgroundrb
directory to a temp directory and it will work
already initialized constant OPTIONS
- To set up a periodic task, set up the
Middleman
object this way:session[:job_key] = worker MiddleMan.schedule_worker( :class => :heartrate_post_worker, :args => "some arg to do_work", :job_key => :simple_schedule, :trigger_args => { :start => Time.now, :end => Time.now + 10.minutes, :repeat_interval => 15.seconds } )
So basically with backgroundRB > 0.2 windows users are out of luck? These instructions include back-slashes, I assume the author ran out of luck, then? Thanks!
Roger, take a look at
http://openwferu.rubyforge.org/
I haven’t tried it myself but hear good things from a developer that I trust.
Я конечно в этом не особо разбираюсь, но после вашего поста стал гораздо больше понимать. Спасибо :)