简体   繁体   English

Google App Engine Python Cron

[英]Google App Engine Python Cron

I've been trying for a few days now to get Google App Engine to run a cron Python script which will simply execute a script hosted on a server of mine. 我已经尝试了几天让Google App Engine运行一个cron Python脚本,它只会执行我的服务器上托管的脚本。

It doesn't need to post any data to the page, simply open a connection, wait for it to finish then email me. 它不需要向页面发布任何数据,只需打开一个连接,等待它完成然后给我发电子邮件。

The code I've previously written has logged as "successful" but I never got an email, nor did I see any of the logging.info code I added to test things. 我之前编写的代码记录为“成功”,但我从未收到过电子邮件,也没有看到我添加的任何logging.info代码进行测试。

Ideas? 想法?

The original and wrong code that I originally wrote can be found at Google AppEngine Python Cron job urllib - just so you know I have attempted this before. 我最初编写的原始和错误代码可以在 Google AppEngine Python Cron作业urllib中找到 - 只是因为您知道我之前尝试过这个。

Mix of weird things was happening here. 混乱的奇怪事情发生在这里。

Firstly, app.yaml I had to place my /cron handler before the root was set: 首先, app.yaml我必须在设置root之前放置我的/cron处理程序:

handlers:
- url: /cron
  script: assets/backup/main.py

- url: /
  static_files: assets/index.html
  upload: assets/index.html

Otherwise I'd get crazy errors about not being able to find the file. 否则我会发现无法找到文件的疯狂错误。 That bit actually makes sense. 这一点实际上是有道理的。

The next bit was the Python code. 接下来是Python代码。 Not sure what was going on here, but in the end I managed to get it working by doing this: 不确定这里发生了什么,但最终我设法让它通过这样做:

#!/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()

Whatever it was causing the problems, it's now fixed. 无论是什么导致了这些问题,它现在已经解决了。

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

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