在这个例子中,main.py还是app.yaml决定App Engine定时任务使用的URL?
在这个示例代码中,应用的URL似乎是由应用内部的这一行决定的:
application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)
同时,也由app.yaml文件中的这一行决定:
- url: /.*
script: main.py
不过,定时任务的URL是由这一行设置的:
url: /tasks/summary
所以看起来,定时任务工具会调用"/tasks/summary
",而由于应用处理程序的关系,这会导致main.py
被执行。这是否意味着,对于定时任务来说,应用中设置URL的那一行是多余的:
application = webapp.WSGIApplication([('/mailjob', MailJob)], debug=True)
……因为定时任务只需要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 个回答
1
看起来你正在阅读这个页面(虽然你没有给我们网址)。根据你提供的配置和代码,运行起来可能会出问题:定时任务会尝试访问路径 /tasks/summary,而 app.yaml 会让它执行 main.py,但 main.py 只设置了一个处理器来处理 /mailjob,所以定时任务的访问会失败,返回404错误。
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()