forked from brianlmoon/GearmanManager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JobFailProtection.php
52 lines (45 loc) · 1.26 KB
/
JobFailProtection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Implements a job list that protects gearman from executing a job multiple times
*
* @author Luc Oth <[email protected]>
* @package GearmanManager
*
*/
class JobFailProtection {
const MaxTriesPerJobs = 3;
const CacheForSeconds = 600; // 5 minutes
const ApcPrefix = 'GearmanManager_';
public static $notifyEmail = null;
/**
* push a job int the list of executed jobs
*
* @param string $jobId the id of the job
*
* @return boolean false if the job and max. number of ties, else true
*/
public static function pushJob($jobId)
{
if (!apc_exists($jobId)) {
apc_add(self::ApcPrefix.$jobId , -1, self::CacheForSeconds);
}
$callCount = apc_inc(self::ApcPrefix.$jobId);
if ($callCount > JobFailProtection::MaxTriesPerJobs) {
// notify admins
if (!empty(self::$notifyEmail)) {
mail(self::$notifyEmail, 'GearmanManager Reoccuring Job Failure', 'Job with id: '.$jobId.' failed multiple times. Stopped processing.');
}
return false;
}
return true;
}
/**
* remove a job from the job list as it was successfully processed
*
* @param string $jobId the id of the job
*/
public static function clearJob($jobId)
{
apc_delete(self::ApcPrefix.$jobId);
}
}