Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz Job in a Grails Application being triggered three times

I have the next Quartz Job in a Grails Application. The job calculates some stats and sends and email with these stats. I want the job to execute everyday at 7 o'clock.

My problem is the job istriggered three times every day instead of just once.

class DailyEmailJob  {
    def eventService 

    static triggers = {
        cron name: 'emailTrigger', cronExpression: "0 0 7 * * ?"
    }

    def execute() {
        eventService.send24StatsEmail()
    }
}

I am hosting the Grails application in Apache Tomcat/7.0.35, running Grails 2.2.1 and quartz-1.0-RC6

like image 607
Sergio del Amo Avatar asked Apr 08 '13 05:04

Sergio del Amo


3 Answers

Your cron expression looks OK. Tomcat's auto-deployment features may be the culprit. For some suggestions, see: https://stackoverflow.com/a/11990221

like image 60
Andrew Avatar answered Nov 17 '22 08:11

Andrew


EDIT

The below analysis does not hold good in this scenario. This was only a hypothesis based on raw misunderstood facts. If advised, I can remove the answer.

END

I hope I can answer to my own question I asked in the comment.

"What are the times it triggers other than 7 AM?"

Is it 7 AM, 2 PM and 9 PM ?

Reason:

  • Refer to the version of quartz plugin [quartz-1.0-RC6] which uses org.quartz-scheduler:quartz:1.8.4. Refer BuildConfig.groovy from Github.
  • Follow CronExpression API from the corressponding Javadoc.

Highlighting the important verbiage:-

"The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety."

In your case the cron expression: "0 0 7 * * ?" results in running the job every 7 hours in 24 hours (A day) resulting in running at 7 am , (7 + 7) 2 PM, (7 + 7 + 7) 9 PM.

This latest docs from quartz-scheduler.org say otherwise. Have a look at the examples here. Also study the "Special Character /".

Based on my tests on seconds and minutes [I did not want to wait for 24 hours to test :)], I strongly think the below cronExpression will work and trigger the execute() at exactly 7:00 Hrs every day:

Answer:

  1. "0 0 7/23 * * ?" [Run the job every 24 hours starting at the 7th hour 0th minute and 0th Second of the day]
  2. "0 0 0/23,7 * * ?" [Synonymous to answer 1 but less verbose.]

Let know your test results. I strongly believe this will work. I am also going to set the scheduler up for the day/night in order to test results after 24 hours (I do not want to tamper the JVM clock in order to achieve a clear result, although I successfully tested doing the same tamper.). Will post results of my actual test.

like image 41
dmahapatro Avatar answered Nov 17 '22 07:11

dmahapatro


This is what the docs say

Either Day-of-Week or Day-of-Month must be "?", or you will get an error since support by the underlying library is not complete. So you can't specify both fields, nor leave both as the all values wildcard "*"; this is a departure from the unix crontab specification.

Hope that helps

like image 1
allthenutsandbolts Avatar answered Nov 17 '22 07:11

allthenutsandbolts