main.py或app.yaml是否确定此示例中App Engine cron任务使用的URL?

时间:2009-07-11 20:42:05

标签: python google-app-engine cron url-routing

在此示例代码中,应用程序的URL似乎由应用程序中的以下行确定:

application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)

也可以通过app.yaml的app处理程序中的这一行:

- url: /.*
  script: main.py

但是,cron任务的URL由以下行设置:

url: /tasks/summary

因此,cron实用程序似乎会调用“/tasks/summary”,并且由于应用程序处理程序,这将导致main.py被调用。这是否意味着,就cron而言,应用程序中设置URL的行是无关紧要的:

application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)

。 。 。因为cron任务所需的唯一URL是app.yaml中定义的URL。

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="admin@gmail.com",
                to="someone@gmail.com",
                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()

2 个答案:

答案 0 :(得分:3)

你可以这样做:

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="admin@gmail.com",
                        to="someone@gmail.com",
                        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()

答案 1 :(得分:1)

看起来你正在阅读this page(即使你没有给我们提供网址)。所提供的配置和代码将无法成功运行:cron任务将尝试访问URL路径/任务/摘要,app.yaml将使其执行main.py,但后者仅为/ mailjob设置处理程序,因此cron任务的尝试将失败,并带有404状态代码。