Hi all, I''m a newbie to backgroundrb and am having trouble integrating it into my rails-app. I''m using namespaces to differentiate between the parts of my app. When I want to create a new worker, it seems that it cannot find the worker object. Here''s my code: -------------------- controller app/passwd/index -------------------- class Passwd::IndexController < ApplicationController layout ''default'' def index $subtitle = "AIX user inventory tool" end def createReport Passwdentry.delete_all User.delete_all session[:job_key] = MiddleMan.new_worker(:class => "passwd:get_passwds_worker", :args => "") end def get_progress if request.xhr? progress_percent = MiddleMan.get_worker(session[:job_key]).progress render :update do |page| page.call(''progressPercent'', ''progressbar'', progress_percent) page.redirect_to( :action => ''done'') if progress_percent >= 100 end else redirect_to :action => ''index'' end end def done render :text => "De database is bijgewerkt" MiddleMan.delete_worker(session[:job_key]) end end --------------------lib/workers/get_passwds_worker.rb-------------------- # Put your code that runs your task inside the do_work method it will be # run automatically in a thread. You have access to all of your rails # models. You also get logger and results method inside of this class # by default. class Passwd::GetPasswdsWorker < BackgrounDRb::Worker::RailsBase attr_reader :progress def do_work(args) @progress = 0 @progressinterval = 100/System.count System.find_all.each do |sys| getAndParsePasswdFile(sys.name) @progress += @progressinterval end end def getAndParsePasswdFile(sys) output = %x[some command-line-magic ;)] output.each do |outputline| outputline = outputline.split(":") thisline = Passwdentry.new thisline.setvalues(outputline[0], sys, outputline[2]); thisline.save end end end Passwd::GetPasswdsWorker.register --------------------app/views/passwd/index/createReport.rhtml-------------------- <h2>De database wordt voorzien van de laatste data.</h2> <div id=''progressbar'' class="progress"></div> <%= periodically_call_remote(:url => {:action => ''get_progress''}, :frequency => 1) %> --------------------conf/backgroundrb.yml-------------------- --- :host: localhost :port: 2000 :rails_env: development :load_rails: true :worker_dir: lib/workers Your help is *very much* appreciated! Thanks, Ger Apeldoorn
Hi Tieg, Thanks a lot for your reply! Unfortunately, I''m not there yet.. After I adapt the new_worker call, this is the error I get: uninitialized constant Passwd So, it really doesnt know how to handle namespaces it appears. Can you see any way to get around that? If I generate the worker without the namespace, can I call it from within the passwd namespace? Thanks again, Ger. On Mon, 2007-09-24 at 09:59 -0400, Tieg Zaharia wrote:> I''m not sure if this is your problem, but I think you should include > two colons in the worker klass name, like this: > > ...new_worker(:class => "passwd::get_passwds_worker", :args => "")... > > Also, BackgrounDRb uses a method in the middleman.rb file called > worker_klass_constant(), from whence it derives the constant name. > This is the constant that BackgrounDRb will look for after passing > your class name to that method: > > "Passwd::getPasswdsWorker" > (whereas you want Passwd::GetPasswdsWorker) > > That might be a bug (or a feature enhancement if BDrb doesn''t support > namespaced workers yet), so the right way would be to patch that > method. Orrr, you could just pass the real name of the worker in your > controller, like this.. > > session[:job_key] = MiddleMan.new_worker(:class => > "Passwd::GetPasswdsWorker", :args => {})... > > > -tieg > > > On 9/24/07, Ger Apeldoorn <gapeldoorn at wehkamp.nl> wrote: > Hi all, > > I''m a newbie to backgroundrb and am having trouble integrating > it into > my rails-app. I''m using namespaces to differentiate between > the parts of > my app. > > When I want to create a new worker, it seems that it cannot > find the > worker object. Here''s my code: > > -------------------- controller app/passwd/index > -------------------- > class Passwd::IndexController < ApplicationController > layout ''default'' > > def index > $subtitle = "AIX user inventory tool" > end > > def createReport > Passwdentry.delete_all > User.delete_all > session[:job_key] = MiddleMan.new_worker(:class => > "passwd:get_passwds_worker", :args => "") > end > > def get_progress > if request.xhr? > progress_percent > MiddleMan.get_worker(session[:job_key]).progress > render :update do |page| > page.call(''progressPercent'', ''progressbar'', > progress_percent) > page.redirect_to( :action => ''done'') if > progress_percent >= 100 > end > else > redirect_to :action => ''index'' > end > end > > def done > render :text => "De database is bijgewerkt" > MiddleMan.delete_worker(session[:job_key]) > end > end > > --------------------lib/workers/get_passwds_worker.rb-------------------- > # Put your code that runs your task inside the do_work method > it will be > # run automatically in a thread. You have access to all of > your rails > # models. You also get logger and results method inside of > this class > # by default. > class Passwd::GetPasswdsWorker < > BackgrounDRb::Worker::RailsBase > attr_reader :progress > def do_work(args) > @progress = 0 > @progressinterval = 100/System.count > System.find_all.each do |sys| > getAndParsePasswdFile(sys.name ) > @progress += @progressinterval > end > end > > def getAndParsePasswdFile(sys) > output = %x[some command-line-magic ;)] > output.each do |outputline| > outputline = outputline.split (":") > thisline = Passwdentry.new > thisline.setvalues(outputline[0], sys, outputline[2]); > thisline.save > end > end > end > Passwd::GetPasswdsWorker.register > > --------------------app/views/passwd/index/createReport.rhtml-------------------- > <h2>De database wordt voorzien van de laatste data.</h2> > <div id=''progressbar'' class="progress"></div> > <%= periodically_call_remote(:url => {:action => > ''get_progress''}, :frequency => 1) %> > > --------------------conf/backgroundrb.yml-------------------- > --- > :host: localhost > :port: 2000 > :rails_env: development > :load_rails: true > :worker_dir: lib/workers > > Your help is *very much* appreciated! > > Thanks, > Ger Apeldoorn > > _______________________________________________ > Backgroundrb-devel mailing list > Backgroundrb-devel at rubyforge.org > http://rubyforge.org/mailman/listinfo/backgroundrb-devel >
Hi Tieg, Unfortunately, it is not that easy. I''ve tried your suggestions, but they didnt work. If I put the worker-class in the same file as the controller, I get ''uninitialized constant BackgrounDRb::Worker'' instead of ''uninitialized constant Passwd''. Now, I''ve generated a non-namespaced worker, which gives the same results! (uninitialized constant GetPasswds) Shouldn''t I include anything into environment.rb or something? Thanks again, Ger. On Mon, 2007-09-24 at 10:22 -0400, Tieg Zaharia wrote:> Hey Ger, > > This should be pretty easy. You just need to initialize the Module > name, which you could do like this: > > module Passwd > class GetPasswdsWorker > ... > end > end > > or you could initialize it and then wrap it like this: > > module Passwd end > class Passwd::GetPasswdsWorker > ... > end > > That should work, but is there any specific reason to namespace it? > Could you just call the class PasswdWorker? > > > -tieg > > On 9/24/07, Ger Apeldoorn <gapeldoorn at wehkamp.nl> wrote: > Hi Tieg, > > Thanks a lot for your reply! > > Unfortunately, I''m not there yet.. After I adapt the > new_worker call, > this is the error I get: > > uninitialized constant Passwd > > So, it really doesnt know how to handle namespaces it appears. > Can you > see any way to get around that? If I generate the worker > without the > namespace, can I call it from within the passwd namespace? > > Thanks again, > Ger. > > On Mon, 2007-09-24 at 09:59 -0400, Tieg Zaharia wrote: > > I''m not sure if this is your problem, but I think you should > include > > two colons in the worker klass name, like this: > > > > ...new_worker(:class => "passwd::get_passwds_worker", :args > => "")... > > > > Also, BackgrounDRb uses a method in the middleman.rb file > called > > worker_klass_constant(), from whence it derives the constant > name. > > This is the constant that BackgrounDRb will look for after > passing > > your class name to that method: > > > > "Passwd::getPasswdsWorker" > > (whereas you want Passwd::GetPasswdsWorker) > > > > That might be a bug (or a feature enhancement if BDrb > doesn''t support > > namespaced workers yet), so the right way would be to patch > that > > method. Orrr, you could just pass the real name of the > worker in your > > controller, like this.. > > > > session[:job_key] = MiddleMan.new_worker (:class => > > "Passwd::GetPasswdsWorker", :args => {})... > > > > > > -tieg > > > > > > On 9/24/07, Ger Apeldoorn <gapeldoorn at wehkamp.nl> wrote: > > Hi all, > > > > I''m a newbie to backgroundrb and am having trouble > integrating > > it into > > my rails-app. I''m using namespaces to differentiate > between > > the parts of > > my app. > > > > When I want to create a new worker, it seems that it > cannot > > find the > > worker object. Here''s my code: > > > > -------------------- controller app/passwd/index > > -------------------- > > class Passwd::IndexController < > ApplicationController > > layout ''default'' > > > > def index > > $subtitle = "AIX user inventory tool" > > end > > > > def createReport > > Passwdentry.delete_all > > User.delete_all > > session[:job_key] = MiddleMan.new_worker(:class > => > > "passwd:get_passwds_worker", :args => "") > > end > > > > def get_progress > > if request.xhr? > > progress_percent > > MiddleMan.get_worker(session[:job_key]).progress > > render :update do |page| > > page.call (''progressPercent'', ''progressbar'', > > progress_percent) > > page.redirect_to( :action => ''done'') if > > progress_percent >= 100 > > end > > else > > redirect_to :action => ''index'' > > end > > end > > > > def done > > render :text => "De database is bijgewerkt" > > MiddleMan.delete_worker(session[:job_key]) > > end > > end > > > > > --------------------lib/workers/get_passwds_worker.rb-------------------- > > # Put your code that runs your task inside the > do_work method > > it will be > > # run automatically in a thread. You have access to > all of > > your rails > > # models. You also get logger and results method > inside of > > this class > > # by default. > > class Passwd::GetPasswdsWorker < > > BackgrounDRb::Worker::RailsBase > > attr_reader :progress > > def do_work(args) > > @progress = 0 > > @progressinterval = 100/System.count > > System.find_all.each do |sys| > > getAndParsePasswdFile(sys.name ) > > @progress += @progressinterval > > end > > end > > > > def getAndParsePasswdFile(sys) > > output = %x[some command-line-magic ;)] > > output.each do |outputline| > > outputline = outputline.split (":") > > thisline = Passwdentry.new > > thisline.setvalues(outputline[0], sys, > outputline[2]); > > thisline.save > > end > > end > > end > > Passwd::GetPasswdsWorker.register > > > > > --------------------app/views/passwd/index/createReport.rhtml-------------------- > > <h2>De database wordt voorzien van de laatste > data.</h2> > > <div id=''progressbar'' class="progress"></div> > > <%= periodically_call_remote(:url => {:action => > > ''get_progress''}, :frequency => 1) %> > > > > > --------------------conf/backgroundrb.yml-------------------- > > --- > > :host: localhost > > :port: 2000 > > :rails_env: development > > :load_rails: true > > :worker_dir: lib/workers > > > > Your help is *very much* appreciated! > > > > Thanks, > > Ger Apeldoorn > > > > _______________________________________________ > > Backgroundrb-devel mailing list > > Backgroundrb-devel at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/backgroundrb-devel > > >
Hi Tieg, Thanks for your help, I came a bit further.... :) I didnt need to add the loadpaths. Now, I''ve got another problem. In about 20% of the launched processes, the worker process dies instantly or it is not created at all. The MiddleMan.new_worker call returns the ID as it should, but a log message which is called on the first line in do_work doesnt appear. How might I make it more reliable? Thanks, Ger. On Tue, 2007-09-25 at 12:01 -0400, Tieg Zaharia wrote:> Hey Ger, > > I would just stick to the convention and keep the class in the file > "RAILS_ROOT/lib/workers/passwd_worker.rb". > > I can''t remember if it was necessary or not, but I''ve also added load > paths to the plugin in my environment.rb file before: > config.load_paths += % > W( #{RAILS_ROOT}/vendor/plugins/backgroundrb/server > #{RAILS_ROOT}/vendor/plugins/backgroundrb/server/lib ) > > I don''t think you need any special config though as long as you have > the plugin installed. Does that help? > > > Tieg > > > > > On 9/25/07, Ger Apeldoorn <gapeldoorn at wehkamp.nl> wrote: > Hi Tieg, > > Unfortunately, it is not that easy. I''ve tried your > suggestions, but > they didnt work. If I put the worker-class in the same file as > the > controller, I get ''uninitialized constant > BackgrounDRb::Worker'' instead > of ''uninitialized constant Passwd''. > > Now, I''ve generated a non-namespaced worker, which gives the > same > results! (uninitialized constant GetPasswds) > > Shouldn''t I include anything into environment.rb or something? > > Thanks again, > Ger. > > On Mon, 2007-09-24 at 10:22 -0400, Tieg Zaharia wrote: > > Hey Ger, > > > > This should be pretty easy. You just need to initialize the > Module > > name, which you could do like this: > > > > module Passwd > > class GetPasswdsWorker > > ... > > end > > end > > > > or you could initialize it and then wrap it like this: > > > > module Passwd end > > class Passwd::GetPasswdsWorker > > ... > > end > > > > That should work, but is there any specific reason to > namespace it? > > Could you just call the class PasswdWorker? > > > > > > -tieg > > > > On 9/24/07, Ger Apeldoorn < gapeldoorn at wehkamp.nl> wrote: > > Hi Tieg, > > > > Thanks a lot for your reply! > > > > Unfortunately, I''m not there yet.. After I adapt > the > > new_worker call, > > this is the error I get: > > > > uninitialized constant Passwd > > > > So, it really doesnt know how to handle namespaces > it appears. > > Can you > > see any way to get around that? If I generate the > worker > > without the > > namespace, can I call it from within the passwd > namespace? > > > > Thanks again, > > Ger. > > > > On Mon, 2007-09-24 at 09:59 -0400, Tieg Zaharia > wrote: > > > I''m not sure if this is your problem, but I think > you should > > include > > > two colons in the worker klass name, like this: > > > > > > ...new_worker(:class => > "passwd::get_passwds_worker", :args > > => "")... > > > > > > Also, BackgrounDRb uses a method in the > middleman.rb file > > called > > > worker_klass_constant(), from whence it derives > the constant > > name. > > > This is the constant that BackgrounDRb will look > for after > > passing > > > your class name to that method: > > > > > > "Passwd::getPasswdsWorker" > > > (whereas you want Passwd::GetPasswdsWorker) > > > > > > That might be a bug (or a feature enhancement if > BDrb > > doesn''t support > > > namespaced workers yet), so the right way would be > to patch > > that > > > method. Orrr, you could just pass the real name of > the > > worker in your > > > controller, like this.. > > > > > > session[:job_key] = MiddleMan.new_worker (:class > => > > > "Passwd::GetPasswdsWorker", :args => {})... > > > > > > > > > -tieg > > > > > > > > > On 9/24/07, Ger Apeldoorn < gapeldoorn at wehkamp.nl> > wrote: > > > Hi all, > > > > > > I''m a newbie to backgroundrb and am having > trouble > > integrating > > > it into > > > my rails-app. I''m using namespaces to > differentiate > > between > > > the parts of > > > my app. > > > > > > When I want to create a new worker, it > seems that it > > cannot > > > find the > > > worker object. Here''s my code: > > > > > > -------------------- controller > app/passwd/index > > > -------------------- > > > class Passwd::IndexController < > > ApplicationController > > > layout ''default'' > > > > > > def index > > > $subtitle = "AIX user inventory tool" > > > end > > > > > > def createReport > > > Passwdentry.delete_all > > > User.delete_all > > > session[:job_key] > MiddleMan.new_worker(:class > > => > > > "passwd:get_passwds_worker", :args => "") > > > end > > > > > > def get_progress > > > if request.xhr? > > > progress_percent > > > > MiddleMan.get_worker(session[:job_key]).progress > > > render :update do |page| > > > page.call (''progressPercent'', > ''progressbar'', > > > progress_percent) > > > page.redirect_to( :action => > ''done'') if > > > progress_percent >= 100 > > > end > > > else > > > redirect_to :action => ''index'' > > > end > > > end > > > > > > def done > > > render :text => "De database is > bijgewerkt" > > > MiddleMan.delete_worker > (session[:job_key]) > > > end > > > end > > > > > > > > > --------------------lib/workers/get_passwds_worker.rb-------------------- > > > # Put your code that runs your task inside > the > > do_work method > > > it will be > > > # run automatically in a thread. You have > access to > > all of > > > your rails > > > # models. You also get logger and results > method > > inside of > > > this class > > > # by default. > > > class Passwd::GetPasswdsWorker < > > > BackgrounDRb::Worker::RailsBase > > > attr_reader :progress > > > def do_work(args) > > > @progress = 0 > > > @progressinterval = 100/System.count > > > System.find_all.each do |sys| > > > getAndParsePasswdFile( sys.name ) > > > @progress += @progressinterval > > > end > > > end > > > > > > def getAndParsePasswdFile(sys) > > > output = %x[some > command-line-magic ;)] > > > output.each do |outputline| > > > outputline = outputline.split (":") > > > thisline = Passwdentry.new > > > thisline.setvalues(outputline[0], > sys, > > outputline[2]); > > > thisline.save > > > end > > > end > > > end > > > Passwd::GetPasswdsWorker.register > > > > > > > > > --------------------app/views/passwd/index/createReport.rhtml-------------------- > > > <h2>De database wordt voorzien van de > laatste > > data.</h2> > > > <div id=''progressbar'' > class="progress"></div> > > > <%= periodically_call_remote(:url => > {:action => > > > ''get_progress''}, :frequency => 1) %> > > > > > > > > > --------------------conf/backgroundrb.yml-------------------- > > > --- > > > :host: localhost > > > :port: 2000 > > > :rails_env: development > > > :load_rails: true > > > :worker_dir: lib/workers > > > > > > Your help is *very much* appreciated! > > > > > > Thanks, > > > Ger Apeldoorn > > > > > > > _______________________________________________ > > > Backgroundrb-devel mailing list > > > Backgroundrb-devel at rubyforge.org > > > > > > http://rubyforge.org/mailman/listinfo/backgroundrb-devel > > > > > >