views:

849

answers:

2

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()
A: 

Looks like you're reading this page (even though you don't give us the URL). The configuration and code as presented won't run successfully: the cron task will try to visit URL path /tasks/summary, app.yaml will make that execute main.py, but the latter only sets up a handler for /mailjob, so the cron task's attempt will fail with a 404 status code.

Alex Martelli
Enough context was provided without adding the URL. You're being cranky and making an unnecessary fuss.
David M.
Alex Martelli
+3  A: 

You could do it like this:

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

handlers:

- url: /tasks/.*
  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([
        ('/tasks/summary', MailJob)], debug=True)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()
flo