|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TDbCronCleanLogTask class file. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Brad Anderson <[email protected]> |
|
7
|
|
|
* @link https://github.com/pradosoft/prado |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Util\Cron; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Shell\TShellWriter; |
|
14
|
|
|
use Prado\TPropertyValue; |
|
15
|
|
|
use Prado\Exceptions\TInvalidDataValueException; |
|
16
|
|
|
use Prado\Util\Cron\TCronModule; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* TDbCronCleanLogTask class. |
|
20
|
|
|
* |
|
21
|
|
|
* TDbCronCleanLogTask Cleans the cron log of old entries older than the |
|
22
|
|
|
* age of {@link getTimePeriod} seconds. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Brad Anderson <[email protected]> |
|
25
|
|
|
* @package Prado\Util\Cron |
|
26
|
|
|
* @since 4.2.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
class TDbCronCleanLogTask extends TCronTask |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var int the time period in seconds of valid log items, default 28 days. |
|
32
|
|
|
*/ |
|
33
|
|
|
private $_timeperiod = 2419200; //86400 seconds/day * 28 days |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* This clears the log of the TDBCronModule specified by the ModuleId, |
|
37
|
|
|
* or if none specified, then the cron executing this task. |
|
38
|
|
|
* @param Prado\Util\Cron\TDbCronModule $cron |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
public function execute($cron) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($mid = $this->getModuleId()) { |
|
43
|
|
|
$cron = $this->getApplication()->getModule($mid); |
|
44
|
|
|
if ($cron === null) { |
|
45
|
|
|
throw new TInvalidDataValueException('dbcronclean_moduleid_is_null', $mid); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
if (is_object($cron) && $cron->isa('Prado\\Util\\Cron\\TDbCronModule')) { |
|
|
|
|
|
|
49
|
|
|
$count = $cron->clearCronLog($this->getTimePeriod()); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if ($cron->asa(TCronModule::SHELL_LOG_BEHAVIOR)) { |
|
|
|
|
|
|
52
|
|
|
$cron->getOutputWriter()->writeLine("Cleared {$count} Cron Task Logs", TShellWriter::GREEN); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} elseif (is_object($cron) && $cron->asa(TCronModule::SHELL_LOG_BEHAVIOR)) { |
|
55
|
|
|
$cron->getOutputWriter()->writeLine("No DB Cron Module to clean", TShellWriter::RED); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return int number of seconds, before which cron logs are to be deleted |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTimePeriod() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->_timeperiod; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @param int $timeperiod number of seconds, before which cron logs are to be deleted |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setTimePeriod($timeperiod) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->_timeperiod = TPropertyValue::ensureInteger($timeperiod); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|