Blog

Search This Blog

Loading...

Monday, September 9, 2013

IronCast 1: Introduction to IronWorker - IronWorker 101 Part 1/4


In a series of four IronCasts, we will provide a high-level introduction to using IronWorker. IronWorker is an easy-to-use scalable task queue that gives cloud developers a simple way to offload front-end tasks, run scheduled jobs, and process tasks in the background and at scale.

These videocasts will cover core concepts including:
  • Deploying a worker
  • Writing worker files to declare dependencies
  • Test and prototype workers rapidly locally
  • Connecting to a cloud development database

We will be using an example application which is written in Rails. However, the same concept applies to every language or framework. IronWorker can handle almost every language including binary files and so if you program in PHP,Python, Node.js, or other languages, don't worry, we have client libraries and examples to show you the way. It should also be possible to convert this example to the language of your choice without much effort. Please refer to further documentation here.

In this video, we will show you how to upload and run a worker in the IronWorker environment. We will deploy a worker that makes external API calls in the background in four simple steps:



Step 1: Worker controller code
This code appears within your application logic and queues up the worker task to run in IronWorker.
class SnippetsController < ApplicationController

  def create
    @snippet = Snippet.new(snippet_params)
    if @snippet.save
      @client ||= IronWorkerNG::Client.new(:token => ENV["TOKEN"], :project_id => ENV["PROJECT_ID"])
      @client.tasks.create("pygments",
                           "database" => Rails.configuration.database_configuration[Rails.env], # This sends in database credentials
                           "request" => {"lang" => @snippet.language,
                                         "code" => @snippet.plain_code},
                           "snippet_id" => @snippet.id)
      redirect_to @snippet
    else
      render :new
    end
  end
Step 2: Worker code
This worker will get uploaded to IronWorker and will run in the background asynchronously when invoked. This worker makes an external API request and then saves the results into the database.
setup_database

uri = URI.parse("http://pygments.appspot.com/")
request = Net::HTTP.post_form(uri, lang: params["request"]["lang"], code: params["request"]["code"])

snippet = Snippet.where(:id => params["snippet_id"]).first
snippet.update_attribute(:highlighted_code, request.body)
def setup_database
  puts "Database connection details:#{params['database'].inspect}"
  return unless params['database']
  # estabilsh database connection
  ActiveRecord::Base.establish_connection(params['database'])
end
Step 3: Worker file (.worker)
This file declares your IronWorker’s dependencies so that we can package up the dependencies and make them available to your worker.
runtime "ruby"

# include postgresql and activerecord
gem "pg"
gem "activerecord"


exec "pygments_worker.rb"

# Merging models
dir '../app/models/'


full_remote_build true # Or remote
Step 4: Uploading to IronWorker
After you install the IronWorker CLI. The CLI instruction, iron_worker upload [WORKER NAME] looks for an iron.json where you Iron.io credentials should be stored. Therefore, if you stored your iron.json in your workers folder, you should first cd into that folder. [WORKER NAME] is the file name of the .worker file.
cd workers
iron_worker upload pygments 
And that’s it! Four simple steps and you have deployed your first IronWorker. Once the worker has been uploaded, you can queue up tasks from the application or you can queue up tasks from the CLI. In the following three episodes of IronCast, we will dive into the details how to construct your own worker file, how to prototype with IronWorker locally and how to have your workers connect to your cloud hosted database.

But for now, you should have enough to get up and running on IronWorker. Sign up for a free account and run this example. Or you can check out other examples on Github as well as dive into more details on the service within our Dev Center.