Skip to content
treeder edited this page Mar 22, 2012 · 1 revision

It's best to think of your workers as completely separate entities from Ruby code or your Rails app. In this sense, they need to be able to configure themselves to external services when they run. So if it has to connect to a database or use global attributes, you'll want to have the worker be able to access this configuration information as if it were running outside your application (which it is).

IronWorker Access Keys

IronWorker configuration is done in a configuration block and its simplest form looks like this:

IronWorker.configure do |config|
  config.token = 'TOKEN'
  config.project_id = 'PROJECT_ID'
end

Get your config token at (http://hud.iron.io/tokens)

If you are using IronWorker in a Ruby on Rails application, you would add the config block above to your development.rb and production.rb or as an initializer.

We recommend you use different IronWorker projects for development and production. Each project has unique keys so make sure you assign them appropriately in the config blocks.

Configuring Databases

You can set db access keys as part of an init() method of your worker class or you can include it within the config block like the keys above. See our knowledge base section on configuring databases to learn how to use your database with IronWorker.

Note that each worker/job is a standalone unit of work so if you run 20 concurrent workers, you'd create 20 connections to your database.

We recommend that you look to group a number of database tasks together into a single worker to better amortize the configuration time.

Setting Global Attributes

Attributes can be set in your configuration block so that they will automatically be set in all of your workers that have these attributes defined. This approach is great for taking care of attributes that all of your workers will use as well as for situations where the keys might change between environments.

As an example, if you were to include the following as part of your config block, then these values will be set in all your worker objects that have those attributes defined.

IronWorker.config do |config|
  ...
  config.global_attributes[:db_user] = "username"
  config.global_attributes[:db_pass] = "password"
end

In your worker, you would simply have the attributes defined as follows:

attr_accessor :db_user, :db_pass

Now when you use these attributes in your worker, they will be set to "username" and "password" automatically.

Changing Attributes By Environment

You can also set up global config attributes to read from a file. For example:

ACCOUNTS = YAML.load_file("#{Rails.root}/config/#{Rails.env}.yml")

IronWorker.config do |config|
    ...
    config.global_attributes[:twilio_key] = ACCOUNTS[:twilio_key]
    config.global_attributes[:twilio_secret] = ACCOUNTS[:twilio_secret]
end

Then in your worker, just add:

attr_accessor :twilio_key, :twilio_secret

When you call queue, the global attributes will automatically be set on those attributes so you don't need to set them every time you use the worker.

Avoid Rails References in your Workers

Because workers are running separate from your application, you want to avoid using Rails environment references in your worker. If you do, you'll likely get an uninitialized constant error as the worker tries to resolve the references in the IronWorker cloud. (Exceptions are Rails.env and Rails.version. See below.)

By way of example, do not do the following:

class IncorrectWorker < IronWorker::Base
  merge "#{Rails.root}/vendor/plugins/.../somefile.rb"

  def run
   
  end
end

It may work when you run_local, but when you queue or schedule the worker, you'll get something like the following:

uninitialized constant IncorrectWorker::Rails
Accessing Rails.env and Rails.version

Exceptions to the "not referencing Rails constants in your workers" rule are Rails.env and Rails.version. These are available in your worker provided you're running within a Rails app. (Note though that your worker is still running independent of a Rails environment.)

# This is OK to do in your worker.
if Rails.env == 'development'
  do_something
elsif Rails.env == 'test'
  do_something_else
elsif Rails.env == 'production'
  do_someother_thing
end