dinsdag 28 juli 2009

Cron jobs van appspot.com

Are you not Dutch? But English is no problem? Click here to go to the English version.



Deze post is eigenlijk een voortzetting van de vorige, het plaatsen van berichten op je website met Twitter. De berichten worden ieder kwartier bijgewerkt. Tenminste, bij de eerste bezoeker die op onze website van de drumband komt. Als niemand op de website zou komen en alleen het twitter-account zou bekijken, zouden de berichten nooit geupdate worden!

Toen kwam Google in beeld. Je kunt namelijk bij www.appspot.com een account aanmaken, en daarbij heb je ook CRON beschikbaar! Ik had de nodige moeite om het aan de praat te krijgen, maar het is gelukt. Ik geef hieronder het kant en klare overzicht waarmee het zou moeten werken;

STAP 1
Ga naar www.appspot.com, maak een account aan en download de benodigde spullen (Python, Google AppEngine). Dit laatste zorgt dat je als je Windows draait een map c:\program files\Google\Google_appengine krijgt.

STAP 2
Stel dat je testsite.appspot.com aangemaakt hebt. Maak dan in je c:\program files\google een map testsite aan en plaats hier de volgende bestanden met de volgende inhoud in:


cron.yaml
cron:
- description: every quarter of a hour update Twitter entries
url: /tasks/sync_twitter_site
schedule: every 15 minutes

index.yaml:
indexes:

# keep the autogenerated stuff. quite an empy document :)


main.py
#!/usr/bin/env python

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext import mail
from google.appengine.ext import urlfetch

class MainHandler(webapp.RequestHandler):

def get(self):
self.response.out.write("This is the main page.")

def main():
application = webapp.WSGIApplication([('/',MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if 1 == 1:
main()



app.yaml
application:testsite
version: 1
runtime: python
api_version: 1

handlers:
- url: /tasks/sync_twitter_site
script: sync_twittersite.py
login: admin
- url: /.*
script: main.py


sync_twittersite.py
#!/usr/bin/env python

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext import mail
from google.appengine.ext import urlfetch

class MainHandler(webapp.RequestHandler):

def get(self):
url = "http://mysite.com/page_to_call.asp"
result = urlfetch(url)
self.response.out.write(result.status_code)

def main():
application = webapp.WSGIApplication([('/.*',MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if 1 == 1:
main()



Zoals je ziet, de code voor het Python sync-script is bijna hetzelfde als main.py script. Maar het /.* is de clou, als je die .* vergeet werkt het script niet (omdat je binnenkomt met /tasks/...). En dan kreeg je weer een 'JOB FAILED' zondat dat je doorhad waar het nu door kwam.

STAP 3
Zet het spul online. Klik op je startknop, Uitvoeren, CMD en klik op OK.
C:, cd\progra~1, cd google, appcfg.py update ./testsite/

Via www.appspot.com en hier inloggen kun je je CRON acties in de gaten houden. Succes!


ENGLISH VERSION


This post is almost a continuation of the previous post, placing messages on your website with Twitter. The messages are updated each quarter of a hour. At least, when people visit our website, the first one in this quarter triggers the update. So if no one would visit our site and only look at our Twitter account, you would get no updates!

After a little search I found that 'Google is your friend!'. At www.appspot.com you can create a free account, appspot supports CRON jobs! I had quite some difficulties to get this stuff working, but finally I succeeded. I will give you my setup of files and code to help you work it out.

STEP 1
Go to www.appspot.com, create an account and download your stuff (Python, Google AppEngine). The Google AppEngine creates this folder on your PC if you are running Windows: c:\program files\Google\Google_appengine.

STEP 2
In this example we presume you created testsite.appspot.com. Create in your c:\program files\google a folder with the name 'testsite' and put these files (with this content) in it:


cron.yaml
cron:
- description: every quarter of a hour update Twitter entries
url: /tasks/sync_twitter_site
schedule: every 15 minutes

index.yaml:
indexes:

# keep the autogenerated stuff. quite an empy document :)


main.py
#!/usr/bin/env python

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext import mail
from google.appengine.ext import urlfetch

class MainHandler(webapp.RequestHandler):

def get(self):
self.response.out.write("This is the main page.")

def main():
application = webapp.WSGIApplication([('/',MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if 1 == 1:
main()



app.yaml
application:testsite
version: 1
runtime: python
api_version: 1

handlers:
- url: /tasks/sync_twitter_site
script: sync_twittersite.py
login: admin
- url: /.*
script: main.py


sync_twittersite.py
#!/usr/bin/env python

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext import mail
from google.appengine.ext import urlfetch

class MainHandler(webapp.RequestHandler):

def get(self):
url = "http://mysite.com/page_to_call.asp"
result = urlfetch(url)
self.response.out.write(result.status_code)

def main():
application = webapp.WSGIApplication([('/.*',MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

if 1 == 1:
main()



As you see, the code for the Python sync-script is almost the same as the main.py script. But the /.* is easily forgotten, which causes not to run (because you enter with /tasks/...).

STEP 3
Let's get it on the Internet. Click on the start-button, Run, CMD and click OK.
C:, cd\progra~1, cd google, appcfg.py update ./testsite/


By www.appspot.com, login here, you can check your CRON tasks. Good luck!

1 opmerking: