views:

618

answers:

4

I am building an app for which I need to set up cron jobs. What I want to do is to set the specific minutes in a hour where specific crons should run. For instance:

  • Task1 at 1st minute of the hour
  • Task2 on every second minute of the hour
  • Task3 every 2 minute only in the second half of the hour

Building this in the standard Unix cron format is reasonably straightforward, but could not figure out how to do it in the Google-App-Engine.

The documentation does not list any non-trivial examples. Any suggestions on how to do it? Examples would be excellent.

+2  A: 

The documentation you linked to seems to indicate that it isn't possible to do what you want using only Cron for Java (unless they have an undocumented feature for it). In particular this doesn't appear to allow for multiple times.

time specifies the time of day, as HH:MM in 24 hour time.

The Python version says the exact same thing.

However, one solution (albeit somewhat more expensive in terms of CPU usage) would be to call a URL every minute, and from the handler for that URL, dispatch out to whatever other calls you need.

In other words, something like:

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
  <cron>
    <url>/run-scheduled-tasks</url>
    <description>Run all scheduled tasks</description>
    <schedule>every 1 minutes</schedule>
  </cron>
</cronentries>

Then in run-scheduled-tasks, check a database for when each task last run, and if your complex condition for triggering them has occurred since then.

a1kmm
Yes, thats what I am trying to do right now. One of my crons is to invoke a jsp page. Will response.sendRedirect(jsp) work in such a cron environment? Will the cron environment understand the redirects and call the second page?
Shreeni
Why not just execute the appropriate code, rather than redirecting to it?
Nick Johnson
Well, I was directing the cron to hit a servlet end point and wasn't sure how to do a server side include of a JSP page. But I could always reverse it - hit a JSP through the cron and do a server side include of everything I want to do - servlets and JSP pages. That seem to be working.BTW, do you know how to do a server side include of a JSP from within a servlet?
Shreeni
A: 

If the documentation is correct you can't get as granular as you are wanting. Doesn't look like they support picking a particular minute of the hour. Or a subset of an hour.

You might have to get creative. Why do you need such specific timing?

Jeremy Wall
A: 

Have a look at Quartz and see if that'll solve your problem.

NA
How will Quartz work with Google-App-Engine? AFAIK, the crons are initiated by the App-Engine, right? So, how will Quart help?
Shreeni
Quartz is a powerful library for scheduling things. That's why I suggested it incase you weren't aware of it. I don't know the restrictions on AppEngine if it'll let Quartz run or not hence I was intentionally vague in my original response. Also with Quartz you don't need to hit a webpage to triggers things.
NA
A: 

This may seem silly. Write three servlet. And schedule them from another UNIX machine on the other part of the world :D. Or even you can write a java app to do it. enjoy

iftee