2009-07-11 52 views
1

在此示例代碼的應用程序的URL似乎是由在應用內這條線來確定:在本例中,main.py或app.yaml是否確定App Engine cron任務使用的URL?

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

而且通過app.yaml中的應用處理程序中這一行:

- url: /.* 
    script: main.py 

url: /tasks/summary 

所以似乎cron實用程序將調用「/tasks/summary」而且由於應用H:然而,定期任務的URL被該行設定安德勒,這將導致main.py被調用。這是否意味着,只要cron是而言,在設置URL應用該行無關:

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

。 。 。因爲cron任務所需的唯一URL是在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() 

回答

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

看起來你正在閱讀this page(即使你不給我們的網址)。所呈現的配置和代碼將無法成功運行:cron任務將嘗試訪問URL路徑/任務/摘要,app.yaml將使該目錄執行main.py,但後者僅設置/ mailjob的處理程序,所以cron任務的嘗試將失敗,並顯示404狀態碼。

+0

足夠的上下文不添加URL提供了依據。你正在胡思亂想,不必要的大驚小怪。 – 2009-07-12 00:06:34

相關問題