This example will show you how to include dependencies with your worker so it will work right when you run it remotely on IronWorker.
Note: Be sure you've followed the base getting started instructions on the top level README.
docker run --rm -v "$PWD":/worker -w /worker iron/node:dev npm install
Now test it locally:
docker run --rm -e "PAYLOAD_FILE=hello.payload.json" -e "YOUR_ENV_VAR=ANYTHING" -v "$PWD":/worker -w /worker iron/node node hello.js
The PAYLOAD_FILE environment variable is passed in to your worker automatically and tells you where the payload file is. Our client libraries help you load the special environment variables automatically.
The YOUR_ENV_VAR environment variable is your custom environment variable. There can be any number of custom environment variables and they can be anything.
It works! And now that it works, we know it will work on IronWorker.
Let's package it up inside a Docker image and upload it to a Docker Registry. Copy the Dockerfile from this repository and modify the ENTRYPOINT line to run your script, but for this example, it's all ready to go:
docker build -t USERNAME/hello:0.0.1 .
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.
Test your image, just to be sure you created it correctly:
docker run --rm -it -e "PAYLOAD_FILE=hello.payload.json" -e "YOUR_ENV_VAR=ANYTHING" USERNAME/hello:0.0.1
Push it to Docker Hub:
docker push USERNAME/hello:0.0.1
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. Also, you can optionally register environment variables here that will be passed into your container at runtime.
iron register -e "YOUR_ENV_VAR=ANYTHING" USERNAME/hello:0.0.1
Now you can start queuing jobs or schedule recurring jobs for your image. Let's quickly queue up a job to try it out.
iron worker queue --payload-file hello.payload.json --wait USERNAME/hello
Notice we don't 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.
Read the API docs to see how to queue jobs from your code or how to schedule them: https://dev.iron.io/worker/reference/api/
Of course, in practice you'll be queuing up jobs via the API, most likely using one of our client libraries. Here's a curl example to show how easy it is to do in any language:
curl -H "Content-Type: application/json" -H "Authorization: OAuth $IRON_TOKEN" \
-d '{"tasks":[{"code_name":"USERNAME/hello","payload":"{\"name\":\"Travis\"}"}]}' \
"https://worker-aws-us-east-1.iron.io/2/projects/$IRON_PROJECT_ID/tasks"
Just copy the above, change USERNAME
to your Docker Hub username and paste it into a terminal
to queue up a task.
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:
iron docker login -e YOUR_DOCKER_HUB_EMAIL -u YOUR_DOCKER_HUB_USERNAME -p YOUR_DOCKER_HUB_PASSWORD
Then just do everything the same as above.
You can package and send your code to Iron directly with the instructions below. Start with steps 1 and 2 above, then continue at step 3 here.
Let's package it up:
zip -r hello.zip .
Then upload it:
iron worker upload --name hellojs --zip hello.zip iron/node node hello.js
Now you can start queuing jobs or schedule recurring jobs for your worker. Let's quickly queue up a job to try it out.
iron worker queue --payload-file hello.payload.json --wait hellojs
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.