Ruby‎ > ‎Getting Started‎ > ‎

SimpleWorker in 5 Minutes

Step 1: Sign Up for a Free SimpleWorker Account

  • Sign up at www.simpleworker.com. You get 5 hours of compute time a month free!
  • Follow the steps in the GetStarted tab.

  • Or follow the steps below to get you first worker running in no time.

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 SimpleWorker Gem

Install the SimpleWorker Ruby gem in your dev environment. You'll be creating worker jobs and sending them to SimpleWorker from your application.

(Note that SimpleWorker works with Ruby >v 1.9. It will not work with earlier versions.)

Install the gem:

% gem install simple_worker
Fetching: simple_worker-x.y.z.gem (100%)
Successfully installed simple_worker-2.0.1
1 gem installed
...

Step 4: Create a Worker

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

class MyWorker < SimpleWorker::Base

  attr_accessor :x

  # The run method is what SimpleWorker 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 script in it:

require 'simple_worker'
require_relative 'my_worker'

# Use this configure block for simple_worker gem v2.x (token/project_id)
SimpleWorker.configure do |config|     
  config.token = 'TOKEN'
  config.project_id = 'PROJECT_ID'
end

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

The v2 version of the SimpleWorker gem takes a token and a project_ID for authorization.

Tokens can be found on the API Tokens tab under the Account link. 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.
  • v1 of the SimpleWorker gem uses access_key/secret_key (see below).
# Use this config block for simple_worker gem v1.X (access_key/secret_key)
SimpleWorker.configure do |config|
  config.access_key = "ACCESS_KEY"
  config.secret_key = "SECRET_KEY"
end


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 SimpleWorker 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 a worker.queue instead so your script now looks like this (except it will have your own token/project_id).


require 'simple_worker' require_relative 'my_worker' # Use this configure block for simple_worker gem v2.x (token/project_id)
SimpleWorker.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 SimpleWorker to run in the cloud. 

Step 7: View the Status of Worker Job

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

That's all there is to it!

Extra Credit - Using Priorities

SimpleWorker is multi-tenant system processing thousands and hundreds of thousands of jobs across many users on many servers. SimpleWorker offers multiple priority levels to make sure you get the best combination of performance/value for your tasks.

Standard priority (p0) is often used for processing thousands of tasks -- crawling or indexing pages -- where some minutes in the queue does not have a material effect.

For tasks that are time-sensitive (such as offloading front-end tasks or when in dev/test mode), we recommend you run in p1 or p2 to run tasks faster. 

For example, specifying a higher priority will move tasks ahead of p0 tasks:

worker = MyWorker.new
worker.x = 30
# worker.run_local
# worker.queue
worker.queue(:priority=>1)
worker.queue(:priority=>2) 

Extra Credit - Using Schedules

Another key aspect of SimpleWorker is the ability to schedule jobs to run later. There are a number of options to use so that you can run them once or on a recurring schedule.

# Add this for the 'minutes_since' syntax
require 'active_support/core_ext'

worker = MyWorker.new
worker.x = 30
# worker.run_local
# worker.queue
worker.schedule(:start_at => 3.minutes.since, run_every:3, run_times:3)
worker.schedule(:start_at => 3.minutes.since, run_every:3, run_times:3, :priority=>1)




Comments