Go Dynamic with Google App Engine Cron Jobs

Using shared hosting services still is the most affordable solution for small and medium business projects. However, one of the features you often have to let go using shared hosting is server side autonomous processing.

Imagine you want to automatically send an email to all users with expired account or you want to clean a server cache once a day to save storage space, you will hit a wall using standard web hosting services. To get this kind of service you usually need to buy a dedicated hosting plan for several hundreds of dollars a month.

An other solution would be to buy a Linux Slice hosting plan but, then, you are on your own to secure and manage your server. This is a speciality not many programmers and web developers are familiar with. It takes a lot of your precious time just to keep the server up.

So, what would you think of a free and limitless solution to your server side processing needs? This is what Google App Engine offers through their Cron jobs implementation. You have full control over the scheduling of the cron jobs using the cron.yaml configuration file.

A Google App Engine cron job can call any url of your appstore application. From there, you can write your Python code to query your website using the URL fetch service. You cal also send emails using the Mail service.

You can do all that while keeping your actual website where is is. If you ever consider moving your website to an other hosting provider, your Google App Engine cron job will continue to work with no modifications.

Following is an example of a simple cron job definition.

Your comments are welcome,

Philippe Chrétien

app.yaml
application: yourappname
version: 1
runtime: python
api_version: 1

handlers:

- url: /.*
  script: main.py

cron.yaml
cron:
	- description: daily mailing job
	url: /mailjob
	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()

Tags: , , , ,

2 Replies

Trackback  •  Comments RSS

  1. Ryan Bavetta says:

    I think you mean “url: /mailjob” on line 15

  2. pchretien says:

    @Ryan Bavetta Thanks for the comment. You are right. Guilty of excessive cut and paste! :) I’ll fix that.

Post a Reply

Your email address will not be published. Required fields are marked *

Top