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
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
setup_database uri = URI.parse("https://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
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
cd workers
iron_worker upload pygments