|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Task; |
|
4
|
|
|
|
|
5
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
|
6
|
|
|
use SilverStripe\Dev\BuildTask; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\CronTask\Interfaces\CronTask; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Remove old LogEntry records from the database |
|
12
|
|
|
* |
|
13
|
|
|
* @package silverstripe-logviewer |
|
14
|
|
|
* @author Robbie Averill <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class RemoveOldLogEntriesTask extends BuildTask implements CronTask |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritDoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $segment = 'RemoveOldLogEntriesTask'; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritDoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $title = 'Remove LogEntry records older than a "n" days'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $description = 'Removes LogEntry records that are older than the configured ' |
|
32
|
|
|
. '`LogViewer.max_log_age` setting. Will run as a cron task unless disabled via configuration.'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* BuildTask implementation |
|
36
|
|
|
* |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool Whether anything was removed |
|
40
|
|
|
*/ |
|
41
|
|
|
public function run($request) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->removeOldLogs(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* CronTask implementation - can be disabled with YAML configuration |
|
48
|
|
|
* |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool Whether anything was removed |
|
52
|
|
|
*/ |
|
53
|
|
|
public function process() |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$this->getCronEnabled()) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
return $this->removeOldLogs(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritDoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getSchedule() |
|
65
|
|
|
{ |
|
66
|
|
|
return Config::inst()->get('LogViewer', 'cron_schedule'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the maximum age allowed for a LogEntry from configuration |
|
71
|
|
|
* |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getMaxAge() |
|
75
|
|
|
{ |
|
76
|
|
|
return (int) Config::inst()->get('LogViewer', 'max_log_age'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return whether the cron functionality is enabled from configuration |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getCronEnabled() |
|
85
|
|
|
{ |
|
86
|
|
|
return (bool) Config::inst()->get('LogViewer', 'cron_enabled'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Remove LogEntry records older than the LogViewer.max_log_age days |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool Whether anything was deleted or not |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function removeOldLogs() |
|
95
|
|
|
{ |
|
96
|
|
|
$tableName = LogEntry::singleton()->baseTable(); |
|
97
|
|
|
$maxAge = $this->getMaxAge(); |
|
98
|
|
|
|
|
99
|
|
|
$logs = LogEntry::get() |
|
100
|
|
|
->where(sprintf('"%s"."Created" <= NOW() - INTERVAL \'%d\' DAY', $tableName, $maxAge)); |
|
101
|
|
|
|
|
102
|
|
|
$count = $logs->count(); |
|
103
|
|
|
if ($count > 0) { |
|
104
|
|
|
$logs->removeAll(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
echo sprintf('Finished. Removed %d logs older than %d days.', $count, $maxAge) . PHP_EOL; |
|
108
|
|
|
|
|
109
|
|
|
return $count > 0; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|