Home > Uncategorized > AppEngine + Grails + Cron

AppEngine + Grails + Cron

December 16th, 2009 Leave a comment Go to comments

I just added a Cron job to “ping” my application every 1 minute. After a short discussion with Paul Cusch, whom commented on one of my previous posts regarding the load request issues Grails on AppEngine is seeing. I decided to give it a try. This solution is entirely based on Paul’s idea I’m just writing it up.

Grails AppEngine plugin is missing a feature to use cron out of the box. I hope to submit a patch for it. This is how it works. You need to have a cron.xml file next to your appengine-web.xml file. In the cron file you describe which url and how often a GET operation should be done on the specified url.

Details can be found here.

I simply added a request to a specified url, which I added to my URL mapping:

"/today"(controller: "user", action = "ping")

Then added a cron file to ping it every minute:

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
    <cron>
        <url>/today</url>
        <description>Keep the app alive by ping every 1 minutes</description>
        <schedule>every 1 minutes</schedule>
    </cron>
</cronentries>

The cron.xml file I keep in my grails-app/conf directory, following the conventions used by the AppEngine plugin, for similar files.

The user controller unfortunately got the honor for hosting the ping request.

def ping = {
  render "ping ${new Date().toString()}"
}

And there it is. Next time that application is uploaded to the AppEngine, the ping method on the user controller will be executed every 1 minute. Helping to keep the application loaded.

This method comes with a few drawbacks however. The application will still hit loading requests every now and then, and those requests will take from your quota. This setup can easily eat up all your free quota.

Also, if everyone sets up this cron job. Then a lot of quota is wasted in general, which might affect the service for other applications. This workaround tries to fix a problem that should be fixed in the inner workings of AppEngine it self (not excluding changes to Grails initialization).

There is however a query that is ongoing. Google is considering the possibility to allow people to pay for keeping their application in memory. If you would consider such an option then star the this issue. I myself starred it, I think it’s quite acceptable.

I turned the cron loading task off in my application, instead seeing it as an experiment with Cron on Google AppEngine. The payoff for this solution wasn’t that great and for me it comes with the expense of some of my code quality.

Bookmark and Share
  1. Cmh
    December 17th, 2009 at 08:37 | #1

    This was a problem for me as well. My app would timeout occasionally on the first request. I am not running it on appengine so I have not tried it, but I thought it would be worth a shot sometime to use the xmpp plugin and have a one minute timer to send yourself a “status” im. I don’t know if it would keep the app in memory though, just an idea. I don’t think it would use up your quota like the cron job.

  2. Morten
    December 17th, 2009 at 10:29 | #2

    @Cmh
    Hi Cmh, an interesting idea. While it would still consume the CPU quouta, using the xmpp service on AppEngine with Grails seems like a fun project. Right now however i won’t have time to look at it for a while. We’ll see if I have time sometime after new years.

    Happy Holidays! :)

  3. December 21st, 2009 at 16:16 | #3

    Hi,

    FYI, Google App Engine team had resolved this issue. Please refer to the blog post below:
    Request performance in Java
    http://googleappengine.blogspot.com/2009/12/request-performance-in-java.html

    Regards,
    Chee Kin

    http://grailsfuse.vobject.com/
    Jump Start Grails Application on Google App Engine!

  4. Morten
    December 21st, 2009 at 18:01 | #4

    @Lim Chee Kin
    FYI, the blog post you are referring too does not solve the issue. As the blog post on morkeleb was written, precompilation was already activated on my appengine project. It doesn’t solve the timeout during loading requests issue, nor does it keep the application in memory. To that I refer you to this blog post, and the link to the issue created by the AppEngine team.

    You can read my other blog posts about this subject:

    http://www.morkeleb.com/2009/12/15/added-precompilation-enabled-to-appengine-plugin/

    and

    http://www.morkeleb.com/2009/12/15/upgraded-to-1-3-0/

    Happy holidays.

  5. January 6th, 2010 at 14:05 | #5

    Hi Morten,

    Thanks to point in out. Just realized that you mentioned it in another blog posts.

    Thank you.

  6. Anton Novopashin
    March 22nd, 2010 at 11:45 | #6

    corn.xml file should be placed in web-inf directory.

  7. Anton Novopashin
    March 22nd, 2010 at 11:46 | #7

    @Anton Novopashin
    sorry cron.xml

  8. March 22nd, 2010 at 15:21 | #8

    @Anton Novopashin
    Yes, this is true. I made some changes where it would be copied in during deployment, just like the other files needed by appenigne.

    The patch hasn’t been added to the AppEngine plugin yet though.

  1. No trackbacks yet.