Derrek Long
2008-Jul-19 18:44 UTC
[Backgroundrb-devel] Advanced Rails Recipe 43 doesn''t work, can''t figure a work around
Hello, I''m trying to learn how to use BackgroundRB to run a long running process while showing status to the user via a webpage. 1. I tried using Advanced Rails Recipe 43, however, it appears as though backgroundrb has been updated since that book was published (2 months ago) as methods like "register_status", "ask_stutus" no longer exist. 2. I tried reading the everything on http://backgroundrb.rubyforge.org/ 3. I tried to update the recipe code with what I learned from reading the backgroundrb webpage 4. I failed spectacularly Here''s my code reworked to be simpler: --------------- class BillingWorker < BackgrounDRb::MetaWorker set_worker_name :billing_worker set_no_auto_load(true) def create(args = nil) cache[worker_key] = 0 index = 0 args.times do sleep(4) index = index + 1 cache[worker_key] =(index * 100) / args end exit end end class PaymentsController < ApplicationController def async_test session[:job_key] = Bill.find(1).start_test redirect_to :action => ''check_async_test_status'' end def check_async_test_status @percent_complete = Bill.test_status(session[:job_key]) if request.xhr? if @percent_complete == 100 render :update do |page| flash[:notice] = "test is complete" session[:job_key] = nil page.redirect_to :action => "pending" end else render :update do |page| page[:billingStatus].setStyle :width => "#{@percent_complete *2}px" page[:billingStatus].replace_html "#{@percent_complete}%" end end end end end class Bill < ActiveRecord::Base def start_test MiddleMan.new_worker(:worker => :billing_worker, :worker_key => "foobar", :data => 5) end def self.test_status(job_key) MiddleMan.worker(:billing_worker, "foobar").ask_result(job_key) # also tried MiddleMan.worker(:billing_worker).ask_result(job_key) end end --------------- hitting /payments/async_test always results in Packet::InvalidWorker /usr/local/lib/ruby/gems/1.8/gems/packet-0.1.8/lib/packet/packet_connection.rb:52:in `ask_worker'' /home/derrek/repos/prod/riderway/trunk/vendor/plugins/backgroundrb/server/lib/master_worker.rb:123:in `get_result_object'' I can''t seem to get at the cache[worker_key] value. Any help would be greatly appreciated. Thanks, -Derrek
hemant
2008-Jul-19 19:29 UTC
[Backgroundrb-devel] Advanced Rails Recipe 43 doesn''t work, can''t figure a work around
On Sun, Jul 20, 2008 at 12:14 AM, Derrek Long <derrek at ridecharge.com> wrote:> Hello, > > I''m trying to learn how to use BackgroundRB to run a long running process while showing status to the user via a webpage. > 1. I tried using Advanced Rails Recipe 43, however, it appears as though backgroundrb has been updated since that book was published (2 months ago) as methods like "register_status", "ask_stutus" no longer exist.Being one of the contributors for the recipe. I am terribly sorry for this, but newer caching policies are a lot more stable. I am trying to talk with Mike for releasing a new pdf that covers new version.> 2. I tried reading the everything on http://backgroundrb.rubyforge.org/ > 3. I tried to update the recipe code with what I learned from reading the backgroundrb webpage > 4. I failed spectacularly > > Here''s my code reworked to be simpler: > > --------------- > class BillingWorker < BackgrounDRb::MetaWorker > set_worker_name :billing_worker > set_no_auto_load(true) > > def create(args = nil) > cache[worker_key] = 0 > index = 0 > args.times do > sleep(4) > index = index + 1 > cache[worker_key] =(index * 100) / args > end > exit > end > end > > class PaymentsController < ApplicationController > def async_test > session[:job_key] = Bill.find(1).start_test > redirect_to :action => ''check_async_test_status'' > end > def check_async_test_status > @percent_complete = Bill.test_status(session[:job_key]) > if request.xhr? > if @percent_complete == 100 > render :update do |page| > flash[:notice] = "test is complete" > session[:job_key] = nil > page.redirect_to :action => "pending" > end > else > render :update do |page| > page[:billingStatus].setStyle :width => "#{@percent_complete *2}px" > page[:billingStatus].replace_html "#{@percent_complete}%" > end > end > end > end > end > > class Bill < ActiveRecord::Base > def start_test > MiddleMan.new_worker(:worker => :billing_worker, :worker_key => "foobar", :data => 5) > end > > def self.test_status(job_key) > MiddleMan.worker(:billing_worker, "foobar").ask_result(job_key) > # also tried MiddleMan.worker(:billing_worker).ask_result(job_key) > end > end > --------------- > > hitting /payments/async_test always results in > Packet::InvalidWorker > /usr/local/lib/ruby/gems/1.8/gems/packet-0.1.8/lib/packet/packet_connection.rb:52:in `ask_worker'' > /home/derrek/repos/prod/riderway/trunk/vendor/plugins/backgroundrb/server/lib/master_worker.rb:123:in `get_result_object'' > > I can''t seem to get at the cache[worker_key] value. Any help would be greatly appreciated.Two things are causing problem: 1. MiddleMan.new_worker(...) was not returning the "worker_key" with which it was created and hence, your session[:job_key] was probably wrong. I fixed it in latest git push and have added testcase to cover that also, hence shouldn''t happen again. 2. Now, in your worker, you are using worker_key to cache the result: cache[worker_key] = 0 Think of cache object as hash and whatever key you used here, must be used while getting the results back and hence your code for fetching the results should be: MiddleMan.worker(:billing_worker, "foobar").ask_result("foobar")
Rob Lacey
2008-Jul-19 20:02 UTC
[Backgroundrb-devel] Advanced Rails Recipe 43 doesn''t work, can''t figure a work around
I got the same problem when I was trying to implement something in production, having read the same Recipie. Exactly the same error in fact. I replicated it in the rails console when trying to query the worker. Dropping out of the console and starting again actually worked but of course doesn''t help when you want to query straight away. Packet::InvalidWorker I got the impression that it was trying to query the worker just a fraction before the worker was ready. I couldn''t gather much more information than that. In the end I had to abandon BackgrounDRb for my project and use a cronjob and basic queue due to time constraints, so I''d really like to know what the problem was so that I don''t give up on BDRb alltogether. I have to say I found the rubyforge site documentation far too lacking in details so again I look forward to see more in depth examples. If it is all fixed now then its a bit late for this project so maybe I''ll give it another look for the next time it BDRb seems like an appropriate solution. RobL hemant wrote:> On Sun, Jul 20, 2008 at 12:14 AM, Derrek Long <derrek at ridecharge.com> wrote: > >> Hello, >> >> I''m trying to learn how to use BackgroundRB to run a long running process while showing status to the user via a webpage. >> 1. I tried using Advanced Rails Recipe 43, however, it appears as though backgroundrb has been updated since that book was published (2 months ago) as methods like "register_status", "ask_stutus" no longer exist. >> > > Being one of the contributors for the recipe. I am terribly sorry for > this, but newer caching policies are a lot more stable. I am trying to > talk with Mike for releasing a new pdf that covers new version. > > > >> 2. I tried reading the everything on http://backgroundrb.rubyforge.org/ >> 3. I tried to update the recipe code with what I learned from reading the backgroundrb webpage >> 4. I failed spectacularly >> >> Here''s my code reworked to be simpler: >> >> --------------- >> class BillingWorker < BackgrounDRb::MetaWorker >> set_worker_name :billing_worker >> set_no_auto_load(true) >> >> def create(args = nil) >> cache[worker_key] = 0 >> index = 0 >> args.times do >> sleep(4) >> index = index + 1 >> cache[worker_key] =(index * 100) / args >> end >> exit >> end >> end >> >> class PaymentsController < ApplicationController >> def async_test >> session[:job_key] = Bill.find(1).start_test >> redirect_to :action => ''check_async_test_status'' >> end >> def check_async_test_status >> @percent_complete = Bill.test_status(session[:job_key]) >> if request.xhr? >> if @percent_complete == 100 >> render :update do |page| >> flash[:notice] = "test is complete" >> session[:job_key] = nil >> page.redirect_to :action => "pending" >> end >> else >> render :update do |page| >> page[:billingStatus].setStyle :width => "#{@percent_complete *2}px" >> page[:billingStatus].replace_html "#{@percent_complete}%" >> end >> end >> end >> end >> end >> >> class Bill < ActiveRecord::Base >> def start_test >> MiddleMan.new_worker(:worker => :billing_worker, :worker_key => "foobar", :data => 5) >> end >> >> def self.test_status(job_key) >> MiddleMan.worker(:billing_worker, "foobar").ask_result(job_key) >> # also tried MiddleMan.worker(:billing_worker).ask_result(job_key) >> end >> end >> --------------- >> >> hitting /payments/async_test always results in >> Packet::InvalidWorker >> /usr/local/lib/ruby/gems/1.8/gems/packet-0.1.8/lib/packet/packet_connection.rb:52:in `ask_worker'' >> /home/derrek/repos/prod/riderway/trunk/vendor/plugins/backgroundrb/server/lib/master_worker.rb:123:in `get_result_object'' >> >> I can''t seem to get at the cache[worker_key] value. Any help would be greatly appreciated. >> > > Two things are causing problem: > > 1. MiddleMan.new_worker(...) was not returning the "worker_key" with > which it was created and hence, your session[:job_key] was probably > wrong. I fixed it in latest git push and have added testcase to cover > that also, hence shouldn''t happen again. > > 2. Now, in your worker, you are using worker_key to cache the result: > cache[worker_key] = 0 > > Think of cache object as hash and whatever key you used here, must be > used while getting the results back and hence your code for fetching > the results should be: > > MiddleMan.worker(:billing_worker, "foobar").ask_result("foobar") > _______________________________________________ > Backgroundrb-devel mailing list > Backgroundrb-devel at rubyforge.org > http://rubyforge.org/mailman/listinfo/backgroundrb-devel >
hemant
2008-Jul-20 03:50 UTC
[Backgroundrb-devel] Advanced Rails Recipe 43 doesn''t work, can''t figure a work around
On Sun, Jul 20, 2008 at 1:32 AM, Rob Lacey <robl at mail.pigdestroyer.co.uk> wrote:> I got the same problem when I was trying to implement something in > production, having read the same Recipie. Exactly the same error in fact. I > replicated it in the rails console when trying to query the worker. Dropping > out of the console and starting again actually worked but of course doesn''t > help when you want to query straight away. > > Packet::InvalidWorker > > I got the impression that it was trying to query the worker just a fraction > before the worker was ready. I couldn''t gather much more information than > that. In the end I had to abandon BackgrounDRb for my project and use a > cronjob and basic queue due to time constraints, so I''d really like to know > what the problem was so that I don''t give up on BDRb alltogether. I have to > say I found the rubyforge site documentation far too lacking in details so > again I look forward to see more in depth examples. > > If it is all fixed now then its a bit late for this project so maybe I''ll > give it another look for the next time it BDRb seems like an appropriate > solution.I have a fix for Linux for the above mentioned problem, looking for a fix for OS X though. Basically, it boils down to "read_nonblock()" doesn''t behave in OS X, as it should. In OSX, read_nonblock , *blocks*, hence we had to switch to recv_nonblock(), which doesn''t block in both Linux and OS X. But recv_nonblock is a something of a beast and doesn''t work well for newly created sockets.
Derrek Long
2008-Jul-21 14:49 UTC
[Backgroundrb-devel] Advanced Rails Recipe 43 doesn''t work, can''t figure a work around
> I have a fix for Linux for the above mentioned problem, looking for a >fix for OS X though. Basically, it boils down to "read_nonblock()" >doesn''t behave in OS X, as it should. In OSX, read_nonblock , >*blocks*, hence we had to switch to recv_nonblock(), which doesn''t >block in both Linux and OS X. But recv_nonblock is a something of a >beast and doesn''t work well for newly created sockets.I pulled from git this morning and just spent another 20 minutes but couldn''t get anything but the Packet::InvalidWorker error. Is there any example of working code I can look at that accesses the backgroundrb cache? My system is Ubuntu 7.10, rails 2.1, and ruby 1.8.6. Thanks, -Derrek
hemant
2008-Jul-22 05:00 UTC
[Backgroundrb-devel] Advanced Rails Recipe 43 doesn''t work, can''t figure a work around
On Mon, Jul 21, 2008 at 8:19 PM, Derrek Long <derrek at ridecharge.com> wrote:>> I have a fix for Linux for the above mentioned problem, looking for a >>fix for OS X though. Basically, it boils down to "read_nonblock()" >>doesn''t behave in OS X, as it should. In OSX, read_nonblock , >>*blocks*, hence we had to switch to recv_nonblock(), which doesn''t >>block in both Linux and OS X. But recv_nonblock is a something of a >>beast and doesn''t work well for newly created sockets. > > I pulled from git this morning and just spent another 20 minutes but couldn''t get anything but the Packet::InvalidWorker error. Is there any example of working code I can look at that accesses the backgroundrb cache? > > My system is Ubuntu 7.10, rails 2.1, and ruby 1.8.6.Please update to packet 0.1.9, it will work in Linux.