Tobias Laufkötter created OFBIZ-9374:
----------------------------------------

             Summary:  Fix TemporalExpressions.Frequency to avoid moving job 
start times away from given freqCount raster
                 Key: OFBIZ-9374
                 URL: https://issues.apache.org/jira/browse/OFBIZ-9374
             Project: OFBiz
          Issue Type: Bug
          Components: framework
    Affects Versions: Trunk
            Reporter: Tobias Laufkötter


If a job is scheduled using TemporalExpressions.Frequency the start time of the 
job will gradually move forward when the excecution of the job is delayed by 
one or more units of the frequency type. 

Example: Job is set up to start at 2017-01-01 00:00:00 and run every ten 
minutes. One month later due to some circumstances the job starts at 2017-02-01 
00:01:01 which results in the next execution to be scheduled at 2017-02-01 
01:11:00 in stead of 2017-02-01 01:10:00.

The reason behind this behaviour is the 
TemporalExpressions.Frequency#prepareCal function. It has the purpose to jump 
from the first starting time to the latest possible execution of the job. But 
instead it just sets it to the current time (with the precision of the chosen 
frequency type) and calculates the next execution time from this point.

{code:title=Frequencies.java}
protected Calendar prepareCal(Calendar cal) {
        protected Calendar prepareCal(Calendar cal) {
            // Performs a "sane" skip forward in time - avoids time consuming 
loops
            // like incrementing every second from Jan 1 2000 until today
            Calendar skip = (Calendar) cal.clone();
            skip.setTime(this.start);
            long deltaMillis = cal.getTimeInMillis() - this.start.getTime();
            if (deltaMillis < 1000) {
                return skip;
            }
            long divisor = deltaMillis;
            if (this.freqType == Calendar.DAY_OF_MONTH) {
                divisor = 86400000;
            } else if (this.freqType == Calendar.HOUR) {
                divisor = 3600000;
            } else if (this.freqType == Calendar.MINUTE) {
                divisor = 60000;
            } else if (this.freqType == Calendar.SECOND) {
                divisor = 1000;
            } else {
                return skip;
            }
            float units = deltaMillis / divisor;
            units = (units / this.freqCount) * this.freqCount;
            skip.add(this.freqType, (int)units);
            while (skip.after(cal)) {
                skip.add(this.freqType, -this.freqCount);
            }
            return skip;
        }
{code}

The error is at {{units = (units / this.freqCount) * this.freqCount;}}. This is 
no operation. What should have been done (and to me it looks like this was the 
intention), is a substraction of the remainder of an integer division of 
{{units}} and {{this.freqCount}} to get the number of units of the frequency 
type that have passed since the first start time.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to