OMP 3.1.0.0 Scheduled Tasks

  1. In ScheduledTaskHelper class, in _isInRange function we check whether a scheduled tasks should be executed or not.

$hour = $frequency->getAttribute(‘hour’);
if (isset($hour)) {
$isValid = ScheduledTaskHelper::_isInRange($hour, (int)date(‘G’), $lastRunTime, ‘hour’, strtotime(’-1 day’));
}


In the case we set hour as frequency,

The task will be executed on 2 cases, if a day has passed:
if ($cutoffTimestamp > $lastTimestamp) {
// Execute immediately if the cutoff time period has past since the task was last run
$isValid = true;
}
and second if is the exact hour we have set on frequency tag:

for ($i = 0, $count = count($rangeArray); !$isValid && ($i < $count); $i++) {
if ($rangeArray[$i] == ‘*’) {
// Is wildcard
$isValid = true;

  	} if (is_numeric($rangeArray[$i])) {
  		// Is just a value
  		$isValid = ($currentValue == (int)$rangeArray[$i]);

  	} else if (preg_match('/^(\d*)\-(\d*)$/', $rangeArray[$i], $matches)) {
  		// Is a range
  		$isValid = ScheduledTaskHelper::_isInNumericRange($currentValue, (int)$matches[1], (int)$matches[2]);

  	} else if (preg_match('/^(.+)\/(\d+)$/', $rangeArray[$i], $matches)) {
  		// Is a range with a skip factor
  		$skipRangeStr = $matches[1];
  		$skipFactor = (int)$matches[2];

  		if ($skipRangeStr == '*') {
  			$isValid = true;

  		} else if (preg_match('/^(\d*)\-(\d*)$/', $skipRangeStr, $matches)) {
  			$isValid = ScheduledTaskHelper::_isInNumericRange($currentValue, (int)$matches[1], (int)$matches[2]);
  		}

  		if ($isValid) {
  			// Check against skip factor
  			$isValid = (strtotime("-$skipFactor $timeCompareStr") > $lastTimestamp);
  		}
  	}
  }

This will lead the task to execute many times if we call it on that hour.
How can I solve this issue so the task will be executed only once per day.
Thank you in advance.

Hi @Dimitris_Sioulas ,

I think you can set <frequency hour="0"/> or <frequency hour="24"/>. I think both would mean that the task is executed after a day passed and next time you call a web page (using the Acron i.e. triggering the task call with an HTTP access on your web page).
Note that using Acron, the frequencies are saved in the DB table plugin_settings with the setting_name = crontab. See also how the frequency is treated for that: https://github.com/pkp/pkp-lib/blob/master/plugins/generic/acron/PKPAcronPlugin.inc.php#L270

Best,
Bozana

Thank @bozana, and appreciate your help.
In my understanding and if you check the code I posted above, you will see that hour=0 or hour=24 means that every time if its 24:00 o’clock the task will be executed. So if I make unlimited http calls and the current time equals to the time is set on frequency I will have the task being executed its time. This is something we do not want simply because we need the task to be executed strictly once per day.

HI @Dimitris_Sioulas

Ah, sorry, I looked at the code in the current release. The issue you are mentioning here has been fixed with this GitHub Issue Monthly scheduled task can be fired multiple times in a day · Issue #5362 · pkp/pkp-lib · GitHub.

Best,
Bozana

1 Like