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