In this sample code the URL of the app seems to be determined by this line within the app:
application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)
but also by this line within the app handler of app.yaml:
- url: /.*
script: main.py
However, the URL of the cron task is set by this line:
url: /tasks/summary
So it seems the cron utility will call "/tasks/summary
" and because of the app handler, this will cause main.py
to be invoked. Does this mean that, as far as the cron is concerned, the line in the app that sets the URL is extraneous:
application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)
. . . since the only URL needed by the cron task is the one defined in app.yaml.
app.yaml
application: yourappname
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
cron.yaml
cron:
- description: daily mailing job
url: /tasks/summary
schedule: every 24 hours
main.py
#!/usr/bin/env python
import cgi
from google.appengine.ext import webapp
from google.appengine.api import mail
from google.appengine.api import urlfetch
class MailJob(webapp.RequestHandler):
def get(self):
# Call your website using URL Fetch service ...
url = "http://www.yoursite.com/page_or_service"
result = urlfetch.fetch(url)
if result.status_code == 200:
doSomethingWithResult(result.content)
# Send emails using Mail service ...
mail.send_mail(sender="[email protected]",
to="[email protected]",
subject="Your account on YourSite.com has expired",
body="Bla bla bla ...")
return
application = webapp.WSGIApplication([
('/mailjob', MailJob)], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()