Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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.

like image 929
James Avatar asked Nov 22 '25 07:11

James


1 Answers

Mix of weird things was happening here.

Firstly, app.yaml I had to place my /cron handler before the root was set:

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. 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="[email protected]",
            to="[email protected]",
            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.

like image 140
James Avatar answered Nov 23 '25 21:11

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!