AppEngine + Grails + Cron
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.
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.
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.
@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!
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!
@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.
Hi Morten,
Thanks to point in out. Just realized that you mentioned it in another blog posts.
Thank you.
corn.xml file should be placed in web-inf directory.
@Anton Novopashin
sorry cron.xml
@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.