1

このサンプル コードでは、アプリの URL はアプリ内の次の行によって決定されるようです。

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

また、app.yaml のアプリ ハンドラー内の次の行によって:

- 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()
4

2 に答える 2

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()
于 2009-07-11T21:15:13.280 に答える
1

あなたはこのページを読んでいるようです(URL を教えてくれなくても)。提示された構成とコードは正常に実行されません: cron タスクは URL パス /tasks/summary にアクセスしようとし、app.yaml は main.py を実行させますが、後者は /mailjob のハンドラーをセットアップするだけなので、 cron タスクの試行は 404 ステータス コードで失敗します。

于 2009-07-11T20:48:59.963 に答える