App Engine Dev Tips #8 : Scheduled Tasks (Cron Jobs)

Romin Irani
Romin Irani’s Blog
3 min readSep 29, 2013

App Engine provides a great feature in the form of Scheduled Tasks (Cron Jobs) to run your code at scheduled interval. For e.g. if you wanted to consolidate a report of certain data once a day, you could schedule a job at say 1:00 AM and run that code.

Here are some other pointers:

  1. Scheduled Tasks also give us the additional advantage of running a long processing task. The current limit on a Scheduled Task is 10 minutes. So you could also look at Scheduled Task to help you execute code that cannot be run due to the 60 second request time out limit.
  2. Scheduled Task run only on the default version of the application and not separately on each version of the application. If you want to run the Scheduled Task on another version of the application, you will need to use the target parameter for the job.
  3. Master the nuances of a Cron Expression. For e.g. every 2 minutes, once a day at 9:00 PM, every Monday at 3:00 PM and so on.
  4. Remember that a Scheduled Task has 10 minutes. You must write your job with the fact that it could timeout. In case, you are processing hundreds or thousands of records, it is necessary to keep a count of where you are when the Scheduled Task exceeded the time deadline, so that next time the Scheduled Task is fired, it will take it off from there and you will not process it in a duplicate fashion. Alternately, think about making your code idempotent.
  5. If you are looking at running the code till completion and think that it is likely to take more than 10 minutes, you should be looking at Google Backends for that. Keep in mind that Backends is a paid service.
  6. It is not possible to test out your Scheduled Task at a given Cron Expression interval in the Local Development Server. The best way to test it out locally is to simply invoke the Servlet Endpoint for the Scheduled Task that you have configured in the web.xml file. Simply punch in that url in the browser and it will execute your code. This way you can at least test out the functionality. In case your local application requires authentication, be sure to check the Admin option before logging in, otherwise you will see an error that says “User Not in Role”, etc.
  7. It is a good idea to secure the URLs for your Scheduled Tasks. This is essential because it might open up your Scheduled Tasks URL for misuse by any user, should they come across the URL patterns.
  8. Keep your cron.xml updated at all times. If there are certain jobs that you would like to disable or remove, I suggest just commenting out the entries in the cron.xml file instead of removing them. Do remember to deploy your application for the updated entries to take effect. As a rule, after deployment, you should visit the Cron Jobs section in the Admin console to verify if all the entries have been correctly applied. It is also a good idea to see if there was no error reported while running it the last time.

If you are looking at learning how to write Scheduled Task, please check out the tutorial.

--

--

Romin Irani
Romin Irani’s Blog

My passion is to help developers succeed. ¯\_(ツ)_/¯