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 `LogViewer.max_log_age`.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* BuildTask implementation |
35
|
|
|
* |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
* |
38
|
|
|
* @return bool Whether anything was removed |
39
|
|
|
*/ |
40
|
|
|
public function run($request) |
41
|
|
|
{ |
42
|
|
|
return $this->removeOldLogs(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* CronTask implementation - can be disabled with YAML configuration |
47
|
|
|
* |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
* |
50
|
|
|
* @return bool Whether anything was removed |
51
|
|
|
*/ |
52
|
|
|
public function process() |
53
|
|
|
{ |
54
|
|
|
if (!$this->getCronEnabled()) { |
55
|
|
|
return false; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
return $this->removeOldLogs(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
public function getSchedule() |
64
|
|
|
{ |
65
|
|
|
return Config::inst()->get(LogEntry::class, 'cron_schedule'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the maximum age allowed for a LogEntry from configuration |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
public function getMaxAge() |
74
|
|
|
{ |
75
|
|
|
return (int) Config::inst()->get(LogEntry::class, 'max_log_age'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return whether the cron functionality is enabled from configuration |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function getCronEnabled() |
84
|
|
|
{ |
85
|
|
|
return (bool) Config::inst()->get(LogEntry::class, 'cron_enabled'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Remove LogEntry records older than the LogViewer.max_log_age days |
90
|
|
|
* |
91
|
|
|
* @return bool Whether anything was deleted or not |
92
|
|
|
*/ |
93
|
|
|
protected function removeOldLogs() |
94
|
|
|
{ |
95
|
|
|
$tableName = LogEntry::singleton()->baseTable(); |
96
|
|
|
$maxAge = $this->getMaxAge(); |
97
|
|
|
|
98
|
|
|
$logs = LogEntry::get() |
99
|
|
|
->where(sprintf('"%s"."Created" <= NOW() - INTERVAL \'%d\' DAY', $tableName, $maxAge)); |
100
|
|
|
|
101
|
|
|
$count = $logs->count(); |
102
|
|
|
if ($count > 0) { |
103
|
|
|
$logs->removeAll(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
echo sprintf('Finished. Removed %d log(s) older than %d day(s).', $count, $maxAge) . PHP_EOL; |
107
|
|
|
|
108
|
|
|
return $count > 0; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|