Hi,
Actually this is not true. The logger object is available in the Rails
environment practically everywhere. See Obie Fernandez's excellent text
"The Rails Way" page 18.
You are partially correct though. If I was
using pure Ruby or IRB then I will have to "require" the logger object.
My assumption is that Backgroundrb interfaces seamlessly with a Rails
application and therefore has access to its environment. If there are
any limitations then please point those out since I may be going down
the wrong way.
Well, put following code in root directory of your rails app and run it:
require File.join(File.dirname(__FILE__),"config","environment")
t_user = User.find_by_id(1)
logger.info("Hello World : user is : #{t_user.login}")
You will find that, even though AR is logging the query, next line throws error:
undefined local variable or method `logger' for main:Object (NameError)
zsh: exit 1 ruby lib/foo.rb
BackgrounDRb does exactly that for loading rails environment and
hence, you don't have access to logger object thats defined in
rails(although, you do have access to logger which is specifically for
backgroundrb).
I have been able to get the scheduler going both using the cron style
and using add_periodic_timer. I show the code below:
class HelloWorker < BackgrounDRb::MetaWorker
set_worker_name :hello_worker
def create(args = nil)
add_periodic_timer(10) do
Post.say_hello
end
end
# def yml_schedule
# logger.info "Hello from yml_schedule"
# Post.say_hello
# end
end
class Post < ActiveRecord::Base
class << self
def say_hello
logger.info 'Hello from Post model'
end
end
end
One thing that I learned the hard way is that the key to make sure that
the scheduled workers work correctly in Backgroundrb is that the
backgroundrb debug log file is empty! That is, if there are any errors
anywhere including other workers that are loaded then the scheduler
stops working! I was following the tutorial in the Advanced Rails
Recipes book that you wrote with Greg Pollock and the BillingWorker was
creating problems. I deleted it and everything started to work right.
I have one request. Since an end-to-end tutorial in Backgroundrb
current release is not available, it would be great if you can publish
one. I own the Advanced Rails Recipes book and was using your recipe
that I mention above, but since the Backgroundrb API has changed, it is
creating problems. Do you think that you can publish the updated recipe
code for the current version of Backgroundrb at the book's website so
that we can use it in its entirety and not get tripped up? I can help
you verify it and test it.
I am sorry about that and I am working with Mike for updating the
recipe. At least, pdf version should be released with updated text.
I am upgrading a Rails 1.2.6 application to Rails 2.1.0. It uses
Backgroundrb and therefore I have to upgrade to the latest release
right?
Not necessarily, but I _do_ recommend to upgrade anyways. Just keep
two things in mind:
1. Do not use inbuilt cache object, its a toy (i hate myself for
writing it). But thankfully, backgroundrb comes with mechanism to
replace that cache with memcache(refer the docs, its really trivial).
Stability should increase by leap and bounds.
2. Avoid spawning workers, unless you must.
I don't understand this. Can you please clarify.
I meant, if you run bdrb with "./script/backgroundrb start" bdrb
server gets daemonized and all the puts statements will be gobbled up.