iainH iainH - 1 month ago 5x
Python Question

How to run a python app repeatedly using Bluemix?

The Bluemix python build pack works just fine - very easy to

cf push myapp --no-route
. So far, so good.

A question, though:

I want to run it periodically on Bluemix as I would using
on my local system.


The app is not written as a long-running task. I just run it periodically to collect data from several websites that I have written for my clients and to email me results when appropriate.

When I run it on IBM Bluemix though, the Bluemix runtime currently thinks the app is failing when it exits and needs to be restarted immediately. This is not what I want, of course.


This turned out to be incredibly easy after having got my head around Openwhisk's simple trigger; action; rule model.

So I migrated my Python app to Openwhisk entirely rather than use the Bluemix Python buildpack or Bluemix Docker containers neither of which were appropriate to this task's simple needs.

I include a summary below. I wrote a more verbose version here

As an added bonus I was able to remove a good deal of my own code and instead use Slack for notifications. As it was so simple to do, I include it in this answer.

The program

Openwhisk Python actions are python 2.7.
File sitemonitor.py:

def main(inDict):
  inTimeout = inDict.get('timeout', '4')
  // A few function calls omitted for brevity
  if state.errorCount == 0:
    return {'text': "All sites OK"}
    return { 'text' : state.slackMessage }

main takes a dictionary and returns a dictionary

Setting up the job

Create the sitemonitor action

$ wsk action create sitemonitor sitemonitor.py # default timout of 4 seconds

or give the action a different timeout in a default parameter

$ wsk action update sitemonitor sitemonitor.py --param timout 2 # seconds

Create the Slack package action monitorSlack

wsk package create  monitorSlack \
  --param url 'https://hooks.slack.com/services/...' \
  --param username 'monitor-bot' \
  --param channel '#general'

Find the Incoming Webhook URL from your Slack Team account

Create the composed monitor2slack action

$ wsk action create monitor2slack --sequence sitemonitor,monitorSlack/post

Create the trigger

It will fire automatically every other hour

$ wsk trigger create monitor2hrs --feed /whisk.system/alarms/alarm --param cron '0 0 */2 * * *' 

$ wsk trigger fire monitor2hrs # fire manually to test

Create the rule

$ wsk rule create monitorsites monitor2hrs monitor2slack

... and Openwhisk will do the rest.


Openwhisk documentation : very good

People at the #openwhisk channel in the the Slack dW OpenTeam were very helpful.