Google App Engine Python Cron

I have been trying for several days to get the Google App Engine to run a cron Python script that will just execute a script hosted on my server.

No need to publish any data on the page, just open the connection, wait for it to complete, and then write to me.

The code I wrote earlier is registered as “successful,” but I never had an email and I did not see any of the code logging.infothat I added for verification.

Ideas?

The original and incorrect code that I originally wrote can be found in the Google AppEngine Python Cron job urllib - so that you know that I tried to do this before.

+3
source share
1 answer

There was a mixture of strange things.

First, app.yamlI had to place a handler /cronbefore the root was set:

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

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

Otherwise, I will get crazy errors related to the inability to find the file. This bit really makes sense.

The next bit is Python code. Not sure what is going on here, but in the end I managed to get it to work 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 this causes problems, it is now fixed.

+4
source

All Articles