Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
civirules
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ayduns
civirules
Commits
07dc438f
Commit
07dc438f
authored
4 years ago
by
jaapjansma
Browse files
Options
Downloads
Patches
Plain Diff
#58: added lock for cron triggers
parent
f89064c0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
CRM/Civirules/Trigger/Cron.php
+61
-2
61 additions, 2 deletions
CRM/Civirules/Trigger/Cron.php
with
61 additions
and
2 deletions
CRM/Civirules/Trigger/Cron.php
+
61
−
2
View file @
07dc438f
...
...
@@ -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
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment