iainH
iainH

Reputation: 1084

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 cron on my local system.

Background

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.

Upvotes: 3

Views: 512

Answers (2)

iainH
iainH

Reputation: 1084

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"}
  else:
    return { 'text' : state.slackMessage }

main takes a dictionary and returns a dictionary

Setting up the job

Create the sitemonitor action

timeout is a parameter specific to my app only.

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

or give the action a different timeout in a default parameter

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

Create the Slack package action monitorSlack

wsk package bind  /whisk.system/slack 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.

References

Openwhisk documentation : very good

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

Upvotes: 2

data_henrik
data_henrik

Reputation: 17156

There are a couple options:

1) If you want to do it completely in Python, you could try something like I did in this example. The basic structure is like this:

import schedule  
 import time 

 def job():  
 #put the task to execute here  

 def anotherJob():  
 #another task can be defined here  

 schedule.every(10).minutes.do(job)  
 schedule.every().day.at("10:30").do(anotherJob)  

while True:  
   schedule.run_pending()  
   time.sleep(1)  

2) Bluemix has changed and today a better approach would be to use OpenWhisk. It is IBM's version of "serverless" computing and it allows to schedule execution of tasks or do it event-driven. You could move your app into a Docker container and invoke it based on a schedule or driven by external events.

Upvotes: 3

Related Questions