Skip to content

IronWorker in 5 Minutes

treeder edited this page Mar 21, 2012 · 2 revisions

Step 1: Sign Up for a Free IronWorker Account

  • Sign up at www.iron.io. You get 5 hours of compute time per month free!

Step 2: Create a Project

  • Go to the Project List and create a project.
  • Name it "Test Project" or "Project MyWorker".
  • Keep this Project tab open because you'll want to check the status of the test job in a later step.

Step 3: Install the IronWorker Gem

Install the IronWorker Ruby gem in your dev environment. You'll be creating worker jobs and sending them to IronWorker from your application. (Note that IronWorker works with Ruby >v 1.9. It will not work with earlier versions.)

gem install iron_worker

Step 4: Create a Worker

Create a file called my_worker.rb and enter the following:

require 'iron_worker'

class MyWorker < IronWorker::Base

  attr_accessor :x

  # The run method is what IronWorker calls to run your worker
  def run
    x.times do |n|
      log "I'm doing something #{n.to_s}!"
      sleep 2
    end
  end

end

The run method is what gets called when the worker runs. Now that we have a worker that does something, so let's run it.

Step 5: Run it locally to make sure it works

Create another file called run_my_worker.rb in the same directory as my_worker.rb and put the following in it:

require 'iron_worker'
require_relative 'my_worker'

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

worker = MyWorker.new
worker.x = 10
worker.run_local

Tokens can be found on the API Tokens tab under the Account link in HUD. Your project IDs can be found on the projects page. You can create multiple tokens that can exist across projects. Each project has a unique project_ID so make sure you're using the right ID for each project.

Now run the worker with:

% ruby run_my_worker.rb

Does your script run ok locally? If so, then it's time to send it off to IronWorker to run it in the cloud.

Step 6: Send it to the Cloud!

With a tiny little change to run_my_worker.rb, you'll be running your workers in the cloud.

Comment out the worker.run_local line and add worker.queue instead so your script now looks like this (except it will have your own token/project_id).

require 'iron_worker'
require_relative 'my_worker'

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

worker = MyWorker.new
worker.x = 10
#worker.run_local
worker.queue

Now run it again with:

% ruby run_my_worker.rb  

It should initialize and upload your worker to IronWorker to run in the cloud.

Step 7: View the Status of Worker Job

Click the "Jobs" tab in the IronWorker dashboard and you'll see the status of your worker! It should either be running or completed.

That's all there is to it!

Clone this wiki locally