The New IronWorker Command Line Interface and .worker Files
We have created a shiny new command line interface (CLI) and a brand new way to define your workers to make uploading your worker code a lot easier and quicker. This should fit much better into the typical developer workflow so you can push to your git repo and upload your workers from the command line. This article will walk you through making a worker, uploading it to IronWorker and then queuing up a bunch of tasks for it.
First things first, let’s set up credentials for the machine we’re working from (you only have to do this once). All Iron.io services use OAuth2 tokens along with a project ID for authentication. You can get both of these from from the Iron.io HUD.
Create a file called .iron.json in your home directory (ie: ~/.iron.json) and enter your Iron.io credentials:
Alternatively, you can set the following environment variables:
As an added benefit, this configuration will work with ALL Iron.io client libaries for all Iron.io services, so you only need to do this once per machine.
Install the Next Generation Ruby Gem to Get the Command Line Tools
The command line tool is built in Ruby, so you’ll need to install Ruby if you don’t have it already. If you aren’t sure if you have Ruby installed, run ruby -v from the command line. If you don’t have it installed, you can get it here.
If you have Ruby installed, simply run:
sudo gem install iron_worker_ng
Now you’re good to go. Run iron_worker -v to ensure it’s installed properly.
Create a Simple Worker – HelloWorker
A worker can be any code you want to run and you can write your worker in almost any language you want, but this example is in Ruby. Save the following to a file called ‘hello_worker.rb’
Create a .worker File
Worker files are a simple way to define your worker and its dependencies. Save the following in a file called `hello.worker`
.worker files can also include dependencies such as gems for ruby, jars for java, and anything else your worker needs to run.
Upload Your Worker
iron_worker upload hello
That command will read your .worker file, create your worker code package and upload it to IronWorker. Head over to hud.iron.io, click the Worker link on your projects list, then click the Tasks tab. You should see your new worker listed there with zero runs. Click on it to show the task list which will be empty, but not for long.
Let’s quickly test it by running:
iron_worker queue hello
Now look at the task list in HUD and you should see your task show up and go from “queued” to “running” to “completed”. Now that we know it works, let’s queue up a bunch of tasks from code.
Queue up Tasks for your Worker
Now you can queue up as many tasks as you want, whenever you want, from whatever language you want. You will want to look at the docs for the client library for your language for how to queue (create a task). The following is an example in ruby, save the following into a file called ‘queue.rb‘:
And run it with:
Check out your task list in HUD to see the tasks running in real-time! And view the logs to see the results and the payload you passed in.
We’d love to hear your thoughts on this, please let us know what you think in the comments below.
Very nice. Worked with no problems.
I guess this means that another person with my token and project id could launch a worker from another machine without having the source code for the worker. They would need the CLI and a script or batch file that runs the iron_worker queue command. Does that make sense?
Hi Gareth. Exactly, you can upload workers from your dev machine for instance, then you can queue them from anywhere, such as your production application, very easily using the API. See the API docs:
You can use any of our libraries in any language to queue, like in the example above in Ruby, you would just call: client.tasks.create(“hello”, “foo”=>”bar”).