<?xml version="1.0" encoding="UTF-8"?>
<snippet>
  <account-id type="integer">2</account-id>
  <body>Draft...

h2. Planning on using BackgroundDRB? Take a long look at the alternatives first

Ask yourself, do you really need a complex solution like BackgroundDRB? Most likely you don't, so use a simple daemonized process instead, see &quot;this snippet about the daemons gem&quot;:http://snippets.aktagon.com/snippets/212-How-to-create-a-daemon-process-using-Ruby-and-the-daemons-RubyGem for  more information.

Heck, even a simple Ruby script run by cron every 5 minutes will be more stable than BackgroundDRB and require less work.

Even if you really need to process a lot of data asynchronously in the background, I wouldn't recommend BackgroundDRB, it's riddled with bugs and unstable in production, so use the &quot;BJ plugin&quot;:http://agilewebdevelopment.com/plugins/bj instead.

Anyway, continue reading if you want to use BackgroundDRB...

h2. Installing the prerequisites:

&lt;code&gt;
$ sudo gem install chronic packet 
&lt;/code&gt;

h2. Installing backgroundrb 

&lt;code&gt;
$ cd rails_project
$ git clone git://gitorious.org/backgroundrb/mainline.git vendor/plugins/backgroundrb
&lt;/code&gt;

You can also get the latest stable version from the Subversion repository:

&lt;code&gt;
 svn co http://svn.devjavu.com/backgroundrb/trunk  vendor/plugins/backgroundrb
&lt;/code&gt;

h2. Setup backgroundrb

&lt;code&gt;
rake backgroundrb:setup
&lt;/code&gt;

h2. Create a worker

&lt;code&gt;
./script/generate worker feeds_worker
&lt;/code&gt;

&lt;code&gt;
class FeedsWorker &lt; BackgrounDRb::MetaWorker
  set_worker_name :feeds_worker
  
  def create(args = nil)
    # this method is called, when worker is loaded for the first time
    logger.info &quot;Created feeds worker&quot;
  end
  
  def update(data)
    logger.info &quot;Updating #{Feed.count} feeds.&quot;
    
    seconds = Benchmark.realtime do
      thread_pool.defer do
        Feed.update_all()
      end
    end

    logger.info &quot;Update took #{'%.5f' % seconds}.&quot;
  end
end
&lt;/code&gt;

h2. Starting backgroundrb

First configure backgroundrb by opening config/backgroundrb.yml in your editor:

&lt;code&gt;
:backgroundrb:
  :ip: 0.0.0.0

:development:
  :backgroundrb:
    :port: 11111     # use port 11111
    :log: foreground # foreground mode,print log messages on console

:production:
  :backgroundrb:
    :port: 22222      # use port 22222
&lt;/code&gt;

Next, start backgroundrb in development mode:

&lt;code&gt;
./script/backgroundrb -e development &amp;
&lt;/code&gt;

h2. Call your worker

From the command line:

&lt;code&gt;
$ script/console
Loading development environment (Rails 2.0.2)
&gt;&gt; MiddleMan.worker(:feeds_worker).update() 
&lt;/code&gt;

h2. When things go wrong

Asynchronous programming is complex, so expect bugs...

h3. Rule #1 know who you're calling. 

If you give your MiddleMan the wrong name of your worker, he'll just spit this crap at you:

&lt;code&gt;
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.send_request
/usr/local/lib/ruby/gems/1.8/gems/packet-0.1.5/lib/packet/packet_master.rb:44:in `ask_worker'
/Users/christian/Documents/Projects/xxx/vendor/plugins/backgroundrb/server/lib/master_worker.rb:104:in `process_work'
/Users/christian/Documents/Projects/xxx/vendor/plugins/backgroundrb/server/lib/master_worker.rb:35:in `receive_data'
/usr/local/lib/ruby/gems/1.8/gems/packet-0.1.5/lib/packet/packet_parser.rb:29:in `call'
/usr/local/lib/ruby/gems/1.8/gems/packet-0.1.5/lib/packet/packet_parser.rb:29:in `extract'
/Users/christian/Documents/Projects/xxx/vendor/plugins/backgroundrb/server/lib/master_worker.rb:31:in `receive_data'
&lt;/code&gt;

So for example this command would generate the above mentioned error:

&lt;code&gt;
MiddleMan.worker(:illegal_worker).update() 
&lt;/code&gt;

It's always nice to see a cryptic error messages such as this, it really deserves an award.

h3. Check for bugs and bug fixes

&quot;git mainline commits&quot;:http://gitorious.org/projects/backgroundrb/repos/mainline/logs/master

h3. Going to production

Starting the daemon:

&lt;code&gt;
./script/backgroundrb -e production start
&lt;/code&gt;

h2. Configuring your task to run periodically

The following example makes backgroundrb call the FeedsWorker's update method once every 15 minutes:

&lt;code&gt;
:production:
  :backgroundrb:
    :port: 22222      # use port 22222
    :lazy_load: true  # do not load models eagerly
    :debug_log: false # disable log workers and other logging
# Cron based scheduling
:schedules:
  :feeds_worker:
    :update:
      :trigger_args: * */15 * * * *
      :data: &quot;Hello world&quot;
&lt;/code&gt;


At the time of writing, the cron scheduler seems to be broken, so I prefer hard-coding the interval in the worker's create method:

&lt;code&gt;
         def create
           add_periodic_timer(15.minutes) { update }
         end
&lt;/code&gt;

If using Vlad or Capistrano, it's also a good idea to fix script/backgroundrb by changing these lines: 
&lt;code&gt;
pid_file = &quot;#{RAILS_HOME}/../../shared/pids/backgroundrb_#{CONFIG_FILE[:backgroundrb][:port]}.pid&quot;
SERVER_LOGGER = &quot;#{RAILS_HOME}/../../shared/log/backgroundrb_server_#{CONFIG_FILE[:backgroundrb][:port]}.log&quot;
&lt;/code&gt;

h3. Resources

&quot;Backgroundrb homepage&quot;:http://backgroundrb.rubyforge.org/

&quot;Backgroundrb best practices&quot;:http://gnufied.org/2008/02/12/backgroundrb-best-practises/

&quot;Backgroundrb scheduling&quot;:http://backgroundrb.rubyforge.org/scheduling/index.html

&quot;Debugging backgroundrb&quot;:http://www.johnyerhot.com/2008/02/11/debugging-backgroundrb/

&quot;Backroundrb's README&quot;:http://backgroundrb.rubyforge.org/files/README.html

&quot;topfunky's messaging article&quot;:http://nubyonrails.com/articles/about-this-blog-beanstalk-messaging-queue



</body>
  <comments-count type="integer">0</comments-count>
  <created-at type="datetime">2008-03-19T00:35:52+02:00</created-at>
  <id type="integer">161</id>
  <language-id type="integer">124</language-id>
  <rendered-body>&lt;p&gt;Draft&amp;#8230;&lt;/p&gt;


	&lt;h2&gt;Planning on using BackgroundDRB? Take a long look at the alternatives first&lt;/h2&gt;


	&lt;p&gt;Ask yourself, do you really need a complex solution like BackgroundDRB? Most likely you don&amp;#8217;t, so use a simple daemonized process instead, see &lt;a href=&quot;http://snippets.aktagon.com/snippets/212-How-to-create-a-daemon-process-using-Ruby-and-the-daemons-RubyGem&quot;&gt;this snippet about the daemons gem&lt;/a&gt; for  more information.&lt;/p&gt;


	&lt;p&gt;Heck, even a simple Ruby script run by cron every 5 minutes will be more stable than BackgroundDRB and require less work.&lt;/p&gt;


	&lt;p&gt;Even if you really need to process a lot of data asynchronously in the background, I wouldn&amp;#8217;t recommend BackgroundDRB, it&amp;#8217;s riddled with bugs and unstable in production, so use the &lt;a href=&quot;http://agilewebdevelopment.com/plugins/bj&quot;&gt;BJ plugin&lt;/a&gt; instead.&lt;/p&gt;


	&lt;p&gt;Anyway, continue reading if you want to use BackgroundDRB&amp;#8230;&lt;/p&gt;


	&lt;h2&gt;Installing the prerequisites:&lt;/h2&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; $ sudo gem install chronic packet 
&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;Installing backgroundrb&lt;/h2&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; $ cd rails_project
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; $ git clone git&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;/&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;gitorious.&lt;span class=&quot;FunctionName&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;backgroundrb&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;mainline.&lt;span class=&quot;FunctionName&quot;&gt;git&lt;/span&gt; vendor&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;plugins&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;backgroundrb
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;You can also get the latest stable version from the Subversion repository:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; svn co http&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;/&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;svn.&lt;span class=&quot;FunctionName&quot;&gt;devjavu&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;com&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;backgroundrb&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;trunk  vendor&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;plugins&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;backgroundrb
&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;Setup backgroundrb&lt;/h2&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; rake backgroundrb&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;setup&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;Create a worker&lt;/h2&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; .&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;script&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;generate worker feeds_worker
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;TypeName&quot;&gt;FeedsWorker&lt;span class=&quot;InheritedClass&quot;&gt; &lt;span class=&quot;InheritedClass&quot;&gt;&amp;lt;&lt;/span&gt; BackgrounDRb::MetaWorker&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt;   set_worker_name &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;feeds_worker&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt;   
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt;   &lt;span class=&quot;Keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;FunctionName&quot;&gt;create&lt;/span&gt;(&lt;span class=&quot;FunctionArgument&quot;&gt;args &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;BuiltInConstant&quot;&gt;nil&lt;/span&gt;&lt;/span&gt;)
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;    &lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; this method is called, when worker is loaded for the first time&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt;     logger.&lt;span class=&quot;FunctionName&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;Created feeds worker&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt;   &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt;   
&lt;span class=&quot;line-numbers&quot;&gt;   9 &lt;/span&gt;   &lt;span class=&quot;Keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;FunctionName&quot;&gt;update&lt;/span&gt;(&lt;span class=&quot;FunctionArgument&quot;&gt;data&lt;/span&gt;)
&lt;span class=&quot;line-numbers&quot;&gt;  10 &lt;/span&gt;     logger.&lt;span class=&quot;FunctionName&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;Updating &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;LibraryClassType&quot;&gt;Feed&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;FunctionName&quot;&gt;count&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;}&lt;/span&gt;&lt;/span&gt; feeds.&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  11 &lt;/span&gt;     
&lt;span class=&quot;line-numbers&quot;&gt;  12 &lt;/span&gt;     seconds &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;LibraryClassType&quot;&gt;Benchmark&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;realtime&lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  13 &lt;/span&gt;       thread_pool.&lt;span class=&quot;FunctionName&quot;&gt;defer&lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  14 &lt;/span&gt;         &lt;span class=&quot;LibraryClassType&quot;&gt;Feed&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;update_all&lt;/span&gt;()
&lt;span class=&quot;line-numbers&quot;&gt;  15 &lt;/span&gt;       &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  16 &lt;/span&gt;     &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  17 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  18 &lt;/span&gt;     logger.&lt;span class=&quot;FunctionName&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;Update took &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;%.5f&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;%&lt;/span&gt; seconds&lt;span class=&quot;String&quot;&gt;}&lt;/span&gt;&lt;/span&gt;.&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  19 &lt;/span&gt;   &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  20 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;Starting backgroundrb&lt;/h2&gt;


	&lt;p&gt;First configure backgroundrb by opening config/backgroundrb.yml in your editor:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;backgroundrb&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt;   &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;ip&lt;/span&gt;: &lt;span class=&quot;Number&quot;&gt;0.0&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;0.0&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt; &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;development&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt;   &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;backgroundrb&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt;     &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;port&lt;/span&gt;: &lt;span class=&quot;Number&quot;&gt;11111&lt;/span&gt;     &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; use port 11111&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt;     &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;log&lt;/span&gt;: foreground &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; foreground mode,print log messages on console&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   9 &lt;/span&gt; &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;production&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;  10 &lt;/span&gt;   &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;backgroundrb&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;  11 &lt;/span&gt;     &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;port&lt;/span&gt;: &lt;span class=&quot;Number&quot;&gt;22222&lt;/span&gt;      &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; use port 22222&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;Next, start backgroundrb in development mode:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; .&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;script&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;backgroundrb &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;e development &lt;span class=&quot;Operator&quot;&gt;&amp;amp;&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;Call your worker&lt;/h2&gt;


	&lt;p&gt;From the command line:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; $ script&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;console
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;Loading&lt;/span&gt; development environment (&lt;span class=&quot;Variable&quot;&gt;Rails&lt;/span&gt; &lt;span class=&quot;Number&quot;&gt;2.0&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;)
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;LibraryClassType&quot;&gt;MiddleMan&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;worker&lt;/span&gt;(&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;feeds_worker&lt;/span&gt;).&lt;span class=&quot;FunctionName&quot;&gt;update&lt;/span&gt;() 
&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;When things go wrong&lt;/h2&gt;


	&lt;p&gt;Asynchronous programming is complex, so expect bugs&amp;#8230;&lt;/p&gt;


	&lt;h3&gt;Rule #1 know who you&amp;#8217;re calling.&lt;/h3&gt;


	&lt;p&gt;If you give your MiddleMan the wrong name of your worker, he&amp;#8217;ll just spit this crap at you:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;You&lt;/span&gt; have a &lt;span class=&quot;BuiltInConstant&quot;&gt;nil&lt;/span&gt; object &lt;span class=&quot;Keyword&quot;&gt;when&lt;/span&gt; you didn&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;t expect it!&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;The error occurred while evaluating nil.send_request&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;/usr/local/lib/ruby/gems/1.8/gems/packet-0.1.5/lib/packet/packet_master.rb:44:in `ask_worker&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;christian&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Variable&quot;&gt;Documents&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Variable&quot;&gt;Projects&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;xxx&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;vendor&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;plugins&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;backgroundrb&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;server&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;master_worker.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt;:&lt;span class=&quot;Number&quot;&gt;104&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;in&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;process_work'&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;/Users/christian/Documents/Projects/xxx/vendor/plugins/backgroundrb/server/lib/master_worker.rb:35:in &lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;&lt;/span&gt;receive_data&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;/usr/local/lib/ruby/gems/1.8/gems/packet-0.1.5/lib/packet/packet_parser.rb:29:in `call&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;usr&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;local&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;ruby&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;gems&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;1.8&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;gems&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;packet&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;0.1&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;packet&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;packet_parser.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt;:&lt;span class=&quot;Number&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;in&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;extract'&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;/Users/christian/Documents/Projects/xxx/vendor/plugins/backgroundrb/server/lib/master_worker.rb:31:in &lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;&lt;/span&gt;receive_data&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;So for example this command would generate the above mentioned error:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;LibraryClassType&quot;&gt;MiddleMan&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;worker&lt;/span&gt;(&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;illegal_worker&lt;/span&gt;).&lt;span class=&quot;FunctionName&quot;&gt;update&lt;/span&gt;() 
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s always nice to see a cryptic error messages such as this, it really deserves an award.&lt;/p&gt;


	&lt;h3&gt;Check for bugs and bug fixes&lt;/h3&gt;


	&lt;p&gt;&lt;a href=&quot;http://gitorious.org/projects/backgroundrb/repos/mainline/logs/master&quot;&gt;git mainline commits&lt;/a&gt;&lt;/p&gt;


	&lt;h3&gt;Going to production&lt;/h3&gt;


	&lt;p&gt;Starting the daemon:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; .&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;script&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;backgroundrb &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;e production start
&lt;/pre&gt;&lt;/p&gt;


	&lt;h2&gt;Configuring your task to run periodically&lt;/h2&gt;


	&lt;p&gt;The following example makes backgroundrb call the FeedsWorker&amp;#8217;s update method once every 15 minutes:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;production&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt;   &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;backgroundrb&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt;     &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;port&lt;/span&gt;: &lt;span class=&quot;Number&quot;&gt;22222&lt;/span&gt;      &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; use port 22222&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt;     &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;lazy_load&lt;/span&gt;: &lt;span class=&quot;BuiltInConstant&quot;&gt;true&lt;/span&gt;  &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; do not load models eagerly&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt;     &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;debug_log&lt;/span&gt;: &lt;span class=&quot;BuiltInConstant&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; disable log workers and other logging&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; Cron based scheduling&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt; &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;schedules&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt;   &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;feeds_worker&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;   9 &lt;/span&gt;     &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;update&lt;/span&gt;:
&lt;span class=&quot;line-numbers&quot;&gt;  10 &lt;/span&gt;       &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;trigger_args&lt;/span&gt;: &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  11 &lt;/span&gt;       &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;data&lt;/span&gt;: &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;Hello world&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;At the time of writing, the cron scheduler seems to be broken, so I prefer hard-coding the interval in the worker&amp;#8217;s create method:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;FunctionName&quot;&gt;create&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt;            &lt;span class=&quot;FunctionName&quot;&gt;add_periodic_timer&lt;/span&gt;(&lt;span class=&quot;Number&quot;&gt;15&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;minutes&lt;/span&gt;) { update }
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt;          &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;If using Vlad or Capistrano, it&amp;#8217;s also a good idea to fix script/backgroundrb by changing these lines: 
&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; pid_file &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;Variable&quot;&gt;RAILS_HOME&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;}&lt;/span&gt;&lt;/span&gt;/../../shared/pids/backgroundrb_&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;Variable&quot;&gt;CONFIG_FILE&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;backgroundrb&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;port&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;}&lt;/span&gt;&lt;/span&gt;.pid&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;SERVER_LOGGER&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;Variable&quot;&gt;RAILS_HOME&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;}&lt;/span&gt;&lt;/span&gt;/../../shared/log/backgroundrb_server_&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;Variable&quot;&gt;CONFIG_FILE&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;backgroundrb&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;port&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;}&lt;/span&gt;&lt;/span&gt;.log&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;h3&gt;Resources&lt;/h3&gt;


	&lt;p&gt;&lt;a href=&quot;http://backgroundrb.rubyforge.org/&quot;&gt;Backgroundrb homepage&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://gnufied.org/2008/02/12/backgroundrb-best-practises/&quot;&gt;Backgroundrb best practices&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://backgroundrb.rubyforge.org/scheduling/index.html&quot;&gt;Backgroundrb scheduling&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://www.johnyerhot.com/2008/02/11/debugging-backgroundrb/&quot;&gt;Debugging backgroundrb&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://backgroundrb.rubyforge.org/files/README.html&quot;&gt;Backroundrb&amp;#8217;s  README &lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://nubyonrails.com/articles/about-this-blog-beanstalk-messaging-queue&quot;&gt;topfunky&amp;#8217;s messaging article&lt;/a&gt;&lt;/p&gt;</rendered-body>
  <title>Using backgroundrb to execute tasks asynchronously in Rails</title>
  <updated-at type="datetime">2008-08-26T09:38:34+03:00</updated-at>
</snippet>
