|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* An queued job to clean out the QueuedJobDescriptor Table |
|
5
|
|
|
* which often gets too full |
|
6
|
|
|
* |
|
7
|
|
|
* @author Andrew Aitken-Fincham <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
class CleanupJob extends AbstractQueuedJob implements QueuedJob { |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* How we will determine "stale" |
|
13
|
|
|
* Possible values: age, number |
|
14
|
|
|
* @config |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $cleanup_method = "age"; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Value associated with cleanupMethod |
|
21
|
|
|
* age => days, number => integer |
|
22
|
|
|
* @config |
|
23
|
|
|
* @var integer |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $cleanup_value = 30; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Which JobStatus values are OK to be deleted |
|
29
|
|
|
* @config |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $cleanup_statuses = array( |
|
|
|
|
|
|
33
|
|
|
"Complete", |
|
34
|
|
|
"Broken", |
|
35
|
|
|
// "Initialising", |
|
36
|
|
|
// "Running", |
|
37
|
|
|
// "New", |
|
38
|
|
|
// "Paused", |
|
39
|
|
|
// "Cancelled", |
|
40
|
|
|
// "Waiting", |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Check whether is enabled or not for BC |
|
45
|
|
|
* @config |
|
46
|
|
|
* @var boolean |
|
47
|
|
|
*/ |
|
48
|
|
|
private static $is_enabled = false; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Required because we aren't extending object |
|
52
|
|
|
* @return Config_ForClass |
|
53
|
|
|
*/ |
|
54
|
|
|
public function config() { |
|
55
|
|
|
return Config::inst()->forClass(get_called_class()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Defines the title of the job |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTitle() { |
|
63
|
|
|
return _t( |
|
64
|
|
|
'CleanupJob.Title', |
|
65
|
|
|
"Clean up old jobs from the database" |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set immediacy of job |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getJobType() { |
|
74
|
|
|
$this->totalSteps = '1'; |
|
|
|
|
|
|
75
|
|
|
return QueuedJob::IMMEDIATE; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Clear out stale jobs based on the cleanup values |
|
80
|
|
|
*/ |
|
81
|
|
|
public function process() { |
|
82
|
|
|
$statusList = implode('\', \'', $this->config()->cleanup_statuses); |
|
83
|
|
|
switch($this->config()->cleanup_method) { |
|
84
|
|
|
// If Age, we need to get jobs that are at least n days old |
|
85
|
|
|
case "age": |
|
86
|
|
|
$cutOff = date("Y-m-d H:i:s", |
|
87
|
|
|
strtotime(SS_Datetime::now() . |
|
88
|
|
|
" - " . |
|
89
|
|
|
$this->config()->cleanup_value . |
|
90
|
|
|
" days" |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
$stale = DB::query( |
|
94
|
|
|
'SELECT "ID" |
|
95
|
|
|
FROM "QueuedJobDescriptor" |
|
96
|
|
|
WHERE "JobStatus" |
|
97
|
|
|
IN (\'' . $statusList . '\') |
|
98
|
|
|
AND "LastEdited" < \'' . $cutOff .'\'' |
|
99
|
|
|
); |
|
100
|
|
|
$staleJobs = $stale->column("ID"); |
|
101
|
|
|
break; |
|
102
|
|
|
// If Number, we need to save n records, then delete from the rest |
|
103
|
|
|
case "number": |
|
104
|
|
|
$fresh = DB::query( |
|
105
|
|
|
'SELECT "ID" |
|
106
|
|
|
FROM "QueuedJobDescriptor" |
|
107
|
|
|
ORDER BY "LastEdited" |
|
108
|
|
|
ASC LIMIT ' . $this->config()->cleanup_value |
|
109
|
|
|
); |
|
110
|
|
|
$freshJobIDs = implode('\', \'', $fresh->column("ID")); |
|
111
|
|
|
|
|
112
|
|
|
$stale = DB::query( |
|
113
|
|
|
'SELECT "ID" |
|
114
|
|
|
FROM "QueuedJobDescriptor" |
|
115
|
|
|
WHERE "ID" |
|
116
|
|
|
NOT IN (\'' . $freshJobIDs . '\') |
|
117
|
|
|
AND "JobStatus" |
|
118
|
|
|
IN (\'' . $statusList . '\')' |
|
119
|
|
|
); |
|
120
|
|
|
$staleJobs = $stale->column("ID"); |
|
121
|
|
|
break; |
|
122
|
|
|
default: |
|
123
|
|
|
$this->addMessage("Incorrect configuration values set. Cleanup ignored"); |
|
124
|
|
|
$this->isComplete = true; |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
if (empty($staleJobs)) { |
|
128
|
|
|
$this->addMessage("No jobs to clean up."); |
|
129
|
|
|
$this->isComplete = true; |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
$numJobs = count($staleJobs); |
|
133
|
|
|
$staleJobs = implode('\', \'', $staleJobs); |
|
134
|
|
|
DB::query('DELETE FROM "QueuedJobDescriptor" |
|
135
|
|
|
WHERE "ID" |
|
136
|
|
|
IN (\'' . $staleJobs . '\')' |
|
137
|
|
|
); |
|
138
|
|
|
$this->addMessage($numJobs . " jobs cleaned up."); |
|
139
|
|
|
// let's make sure there is a cleanupJob in the queue |
|
140
|
|
|
if (Config::inst()->get('CleanupJob', 'is_enabled')) { |
|
141
|
|
|
$this->addMessage("Queueing the next Cleanup Job."); |
|
142
|
|
|
$cleanup = new CleanupJob(); |
|
143
|
|
|
singleton('QueuedJobService')->queueJob($cleanup, date('Y-m-d H:i:s', time() + 86400)); |
|
144
|
|
|
} |
|
145
|
|
|
$this->isComplete = true; |
|
146
|
|
|
return; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.