Skip to content
Snippets Groups Projects
Commit 07dc438f authored by jaapjansma's avatar jaapjansma
Browse files

#58: added lock for cron triggers

parent f89064c0
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,11 @@
abstract class CRM_Civirules_Trigger_Cron extends CRM_Civirules_Trigger {
/**
* @var \CRM_Core_Lock
*/
private $lock;
/**
* This function returns a CRM_Civirules_TriggerData_TriggerData this entity is used for triggering the rule
*
......@@ -12,11 +17,19 @@ abstract class CRM_Civirules_Trigger_Cron extends CRM_Civirules_Trigger {
abstract protected function getNextEntityTriggerData();
/**
* @return int
* @return array
*/
public function process() {
$count = 0;
$isValidCount = 0;
if (!$this->acquireLock()) {
return array(
'count' => $count,
'is_valid_count' => $isValidCount,
);
}
while($triggerData = $this->getNextEntityTriggerData()) {
$this->alterTriggerData($triggerData);
$isValid = CRM_Civirules_Engine::triggerRule($this, $triggerData);
......@@ -25,11 +38,57 @@ abstract class CRM_Civirules_Trigger_Cron extends CRM_Civirules_Trigger {
}
$count ++;
}
$this->acquireLock();
return array(
'count' => $count,
'is_valid_count' => $isValidCount,
);
}
/**
* Acquires a lock. Returns true when the lock was free and is acquired
* Returns false when the lock was not free or could not be acquired.
*
* @return bool
*/
protected function acquireLock() {
try {
$name = 'civirules_cron_rule_' . $this->getRuleId();
if ($this->lock == NULL) {
$this->lock = new CRM_Core_Lock($name, $this->getLockTimeout());
if ($this->lock->isFree()) {
$this->lock->acquire();
if ($this->lock->isAcquired()) {
return TRUE;
}
}
}
} catch (\CRM_Core_Exception $e) {
// Do nothing.
}
return FALSE;
}
/**
* Releases the lock
*
*/
protected function releaseLock() {
if ($this->lock) {
$this->lock->release();
}
}
/**
* Returns the lock timeout for this trigger in seconds
*
* @return int
*/
protected function getLockTimeout() {
return 900; //900 seconds = 15 minutes
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment