Note: You are viewing a non-styled version of this website. Click here to skip to the content.

Vermonster - Open Standards through Open Source

Vermonster Logo

Environment Variable Fix for Ruby Background Job

Posted on June 18th, 2008 under Code, Ruby on Rails.

We decided to use Background Job (aka. Bj) to handle some background jobs for a project. We based our decision to use this library after reading a blog entry and presentation (PDF) by AirBlog. And it is widely supported by the folks over at Engine Yard.

One of the things we wanted to do was pass environment variables to our child jobs, but found the de-serialization seemed to be broken. Here’s a quick patch.

The main goal is to launch our processes with the :env so that our ruby child processes could access the variables via ENV global.

  Bj.submit './script/runner ./lib/jobs/my_job.rb', :env => { :myvar => 'FOO' }

The issue we found is that while the :env has was getting set correctly in the database (serializing to YAML), it was not getting de-serialized previous to the actual launch of the job.

Here is a small patch to runner.rb you may find useful:

--- runner.rb.orig	2008-06-16 14:43:19.000000000 -0400
+++ runner.rb		2008-06-16 14:51:42.000000000 -0400
@@ -196,12 +196,22 @@
                 Bj.logger.info{ "#{ job.title } - started" }

                 command = job.command
-                env = job.env || {}
+
+                # env is serialized in the DB
+                if (job.env.blank?)
+                  env = {}
+                else
+                  env = YAML.load(job.env.to_s)
+                end
+
                 stdin = job.stdin || ''
                 stdout = job.stdout || ''
                 stderr = job.stderr || ''
                 started_at = Time.now

Leave a Reply