Getting Started With IronWorker
IronWorker is a container based distributed work-on-demand platform. We use Docker’s container format, and have container images for the major languages you can use to run your custom code. But because we use Docker you could use any available Docker image to run your custom code.
This getting started tutorial will walk you through the steps of writing custom code, testing it locally, deploying it to Iron, and queuing message to run tasks.
Offload your tasks to the parallel-processing power of the elastic cloud. Write your code, then queue tasks against it—no servers to manage, no scaling to worry about.
Before you begin
Before starting, you will need to setup a couple of things. You only need to do this once.
Hello World Worker
This is a very simple hello world example worker in Ruby. You do not even need Ruby installed to try this example so give it a go! All languages follow the same process so you will get an idea of how things work regardless.
1. Write your custom Worker code
Create a file called helloworld.rb
containing:
Now let’s run it in one of the Iron stack containers:
The fact that it runs means it’s all good to run on IronWorker, so lets upload it and queue up a task for it so it runs on the IronWorker platform.
Let’s package it up inside a Docker image. You should have account on Docker Hub for this. Copy the Dockerfile from our repository and modify the ENTRYPOINT line to run your script. Build your docker image:
Build a docker image:
The 0.0.1
is the version which you can update whenever you make changes to your code.
USERNAME
is your username on Docker Hub.
Test your image, just to be sure you created it correctly:
Push it to Docker Hub and register your image with Iron:
And finally queue up a job for it!
The --wait
parameter waits for the job to finish, then prints the output.
You will also see a link to HUD where you can see all the rest of the task details along with the log output.
That’s it, you have ran a worker on the IronWorker cloud platform!
Now let’s get into more detail.
2. Test your Worker
IronWorker’s environment is a Linux Docker container that your task is executed in. Anything you write that runs inside of our published Docker images should run just the same as on IronWorker. The key here is getting it to run with the Docker commands below and sample payloads.
The primary Docker command is:
- Replace IMAGE with the name of the image you want your code to be executed in. For example, if your worker is a Ruby script, you can replace IMAGE with
iron/ruby
. Also you may need to specify a TAG (version) of the image you want to use:iron/ruby:2.2
- Replace MY_COMMAND with what you would like to execute. For instance, if your worker was a Ruby script called
myworker.rb
, you would replace MY_COMMAND withruby myworker.rb
. If it was a Go program, you would change it to./myworker
. - Replace MY_PAYLOAD with the name of an example payload file to test with. This payload file is the format that you will use to queue up jobs/tasks for your worker after it’s uploaded.
This command may seem simple at first glance, but the main thing is that it will force you to vendor all your dependencies along with your worker. You’ll find links to an example repository showing how to do this for various languages.
3. Package your Worker
Let’s package it up inside a Docker image and upload it to a Docker Registry. Copy the Dockerfile from appropriate directory (depending on used programming language) of this repository and modify the ENTRYPOINT line to run your script. Build your docker image:
That’s just a standard docker build
command. The 0.0.1 is the version which you can update
whenever you make changes to your code. Change USERNAME
to your Docker Hub username and change IMAGENAME
to your docker image name.
Test your image, just to be sure you created it correctly:
4. Push it to Docker Hub
Push it to Docker Hub:
5. Register your image with Iron
Ok, we’re ready to run this on Iron now, but first we have to let Iron know about the image you just pushed to Docker Hub.
6. Queue / Schedule jobs for your image
Now you can start queuing jobs or schedule recurring jobs for your image.
Notice we do not use the image tag when queuing, this is so you can change versions without having to update all your code that’s queuing up jobs for the image.
The –wait parameter waits for the job to finish, then prints the output. You will also see a link to HUD where you can see all the rest of the task details along with the log output.
Of course, in practice you will be queuing up jobs via the API, most likely using one of our client libraries.
Private images
If you want to keep your code private and use a private Docker repository, you just need to let Iron know how to access your private images. From the same directory as your iron.json file, run:
Then just do everything the same as above.
Deploying your code directly to Iron.io without docker
1. Write and Test your Worker
Stays exactly the same, Click Here to read.
2. Package and Upload your Worker
Packing is pretty straightforward knowing that you got it working with the docker run
command above, just zip it up.
Then upload the zip you just created. You’ll notice this command needs you to specify a Docker Image even though you are not using Docker. This is because Iron will still execute your task in a container and needs this information to properly run:
3. Queue Tasks for your Worker
Now you get to queue up tasks/jobs for your Worker!
Typically you would use the IronWorker API to actually queue up tasks from other systems. The cli queue command above is primarily for testing purposes.