Background jobs with Iron and Heroku in 10 minutes
Heroku allows for container based deploys, and [Iron’s Worker]((https://iron.io/worker?utm_source=coderwall) makes running and scaling background jobs extremely easy. They work great hand in hand. Heroku makes it extremely easy to get an application up and running and Iron shines at scaling workloads. We’ll deploy a simple Python application on Heroku, then write an Iron Worker to do some background work.
Objectives
- Run a dockerized web application on Heroku
- Create a new project on Iron that you'll use for this application
- Create a background worker using Iron Worker
- Fire off your worker via your web application
Clone a simple “Hello World” project, upload it to Heroku’s container registry, and deploy it:
> git clone https://github.com/heroku/alpinehelloworld.git
> cd alpinehelloworld
> heroku create
> heroku plugins:install heroku-container-registry
> heroku container:login
> heroku container:push web
> heroku open
You should see your browser open with the “hello world” text. This means your application is running correctly on Heroku.
Now let’s create our Iron worker
Create a file called “worker.py” (in reality, it would probably be named something like emailworker.py or imageprocessor.py or whatever it is that the background worker will actually do). We’ll print “Hello from IronWorker”.
> cd webapp
> echo "print 'Hello from IronWorker'" > worker.py
Now you need to grab your credentials from Iron
Log into your account at https://www.iron.io, create your project, and then download the corresponding iron.json file associated with your new project. Copy it to the root of your application.
> cp iron.json webapp/
You can test that your worker executes correctly by running it locally
> cd ..
> docker run --rm -v "$PWD":/worker -w /worker iron/python:2 python worker.py
Now you need to zip up your worker and your config file and send them to Iron
> cd webapp
> zip -r worker.zip worker.py iron.json
> iron worker upload --zip worker.zip --name worker iron/python:2 python worker.py
Once it’s uploaded to Iron, you can fire off workers on the command line
> iron worker queue --wait worker
… or via your application by using a client library. Let's modify our application so it fires off a background job on Iron immediately once someone visits your site. The first thing we need to do is add the iron-worker python client library to the requirements.txt file. This lets pip know that this is a required dependency.
> cd webapp
> echo "iron-worker" >> requirements.txt
Now let's modify the app.py file to include the code below and fire off our new worker on Iron
import os
from flask import Flask
from iron_worker import *
app = Flask(__name__)
@app.route('/')
def hello():
task = IronWorker()
task.queue(code_name="worker")
return 'Hello world!'
if __name__ == '__main__':
app.run(host='0.0.0.0')
That's it. Let's deploy the latest code to Heroku and see how things go
> heroku container:push web
> heroku open
You should see a hello world message in your browser, and if you visit your projects dashboard on Iron, you should see that your task has already run and outputted the hello message from the worker there as well.
Conclusion
Iron has client libraries in most languages and also a full REST API. It runs its own auto-scaling algorithm as well, so if you needed to handle 1 task on minute and 1000 the next, that's not a problem. It also supports installations on different cloud vendors, and on-premise installations, so it's able to move with you and your business.