Google App Engine Python 定时任务
我这几天一直在尝试让Google App Engine运行一个定时任务的Python脚本,这个脚本只是简单地去执行我自己服务器上的一个脚本。
这个脚本不需要向页面发送任何数据,只需要打开一个连接,等它执行完毕后再给我发个邮件。
我之前写的代码显示为“成功”,但我从来没有收到过邮件,也没有看到我添加的logging.info
代码的任何输出,用来测试的。
有没有什么想法?
我最开始写的错误代码可以在这里找到 Google AppEngine Python Cron job urllib - 只是让你知道我之前尝试过这个。
1 个回答
4
这里发生了一些奇怪的事情。
首先,我在 app.yaml
文件中必须把我的 /cron
处理程序放在根目录设置之前:
handlers:
- url: /cron
script: assets/backup/main.py
- url: /
static_files: assets/index.html
upload: assets/index.html
否则我会遇到一些疯狂的错误,说找不到文件。这个要求其实是有道理的。
接下来是 Python 代码。我不太确定这里发生了什么,但最后我通过这样做让它正常工作了:
#!/usr/bin/env python
# import logging
from google.appengine.ext import webapp
from google.appengine.api import mail
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
import logging
class CronMailer(webapp.RequestHandler):
def get(self):
logging.info("Backups: Started!")
urlStr = "http://example.com/file.php"
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, urlStr)
mail.send_mail(sender="example@example.com",
to="email@example.co.uk",
subject="Backups complete!",
body="Daily backups have been completed!")
logging.info("Backups: Finished!")
application = webapp.WSGIApplication([('/cron', CronMailer)],debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
不管是什么导致了问题,现在都解决了。