简体   繁体   English

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

[英]Does main.py or app.yaml determine the URL used by the App Engine cron task in this example?

In this sample code the URL of the app seems to be determined by this line within the app: 在此示例代码中,应用程序的URL似乎由应用程序中的此行确定:

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

but also by this line within the app handler of app.yaml: 而且在app.yaml的app处理程序中的这一行:

- url: /.*
  script: main.py

However, the URL of the cron task is set by this line: 但是,cron任务的URL由以下行设置:

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. 因此,似乎cron实用程序将调用“ /tasks/summary ”,并且由于app处理程序,这将导致调用main.py Does this mean that, as far as the cron is concerned, the line in the app that sets the URL is extraneous: 这是否意味着,就cron而言,应用程序中设置URL的行是无关紧要的:

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

. . . since the only URL needed by the cron task is the one defined in app.yaml. 因为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()

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="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()

Looks like you're reading this page (even though you don't give us the URL). 看起来你正在阅读这个页面 (即使你没有给我们提供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. 所提供的配置和代码将无法成功运行:cron任务将尝试访问URL路径/任务/摘要,app.yaml将使其执行main.py,但后者仅为/ mailjob设置处理程序,因此cron任务的尝试将失败,并带有404状态代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM