Configuring Cron Jobs – Developing Scalable Apps with Java

Configuring Cron Jobs – Developing Scalable Apps with Java


In this exercise, you’re going to set up a cron job to periodically update the announcement about the conferences that are almost sold out. You’ve already written the function to create the announcement, so pretty much all you’re going to need to do is to configure the cron job. So, you configure cron jobs in cron.XML, and this is an example for what cron.XML might look like; has a top level cron entries element and then each cron job is specified within a cron element. You must specify the path, and this is the URL that’s going to run the task invoked by the cron job. You can optionally specify a description, and you must specify a schedule. The schedule indicates how often to run the task. There’s a variety of ways to set the schedule, but you must strictly follow the syntax or the current job won’t run. I recommend checking the formats for the schedule. The developer documentation does a really nice job of describing the syntax for the schedule, and giving examples. For example, you can specify it to go every 12 hours. You can specify it to go once a year, first Monday of, let’s say December. And then there’s other ways of specifying it. Anyway, I really recommend you take a look at the develop documentation for the schedule. But do bear in mind, although there’s lots of flexibility in how you specify the schedule, you actually do have to get the syntax exactly right. So, you can’t say every one hour. You actually have to say every one hours. The admin console on appspot for your application has a cron jobs page that shows you what cron jobs you’ve got running. I’ve got my cron slash set announcement cron job running already, and it’s scheduled to go every one minute, and the last time it ran it had a success. If you recall, the set announcement function queries for all conferences that are almost sold out. So if we look at the quota details, you can see that my query ops number is going up. It’ll go up every minute. The other thing that’s going to go up every minute is the Datastore API calls. So let’s see. So that’s 162 and that’s 143. Let’s refresh, see what happens. If I refresh the browser, and you see that now my Datastore Query Ops has gone up to 144, and my Datastore API Calls have gone up to 163. So every minute these numbers are going to go up. It’s actually not a big deal because this cron job doesn’t do a whole lot, but if you have a cron job that does a lot of Reads and Writes to the Datastore, It is going to use up your quota. Another thing that running a cron job every minute does is that it means you’re, it means you’re app engine application is always in use, so it never has any idle time. Which, you know, if your application is in constant use by users anyway, it goes viral, you’re hugely successful, not a problem. But when your developing and testing, it does mean that if you keep your cron job running frequently, it’s going to keep your front end up, and you’re going to use up your instance hours. So you can see here, I’ve already used 4.64 of my 28 instance hours. Not a big deal, but it’s something to keep an eye on. Off shot load cron.xml two app engine. It stays there until you overwrite it with a new cron dot XML. So if you want to disable your cron job, it’s not good enough to just delete cron.XML completely from your app and re-upload your app. You actually have to send up a new cron.XML with no cron entries. So just delete the cron entries for the jobs that you want to delete and reupload your cron.xml. And another thing you could do is just specify the schedule so that it only runs like once a year then you’re not going to have to worry about Quotro or anything, but you know, why not just go ahead and delete it anyway

Leave a Reply

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