forked from matomo-org/plugin-QueuedTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasks.php
108 lines (90 loc) · 3.47 KB
/
Tasks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\QueuedTracking;
use Piwik\Common;
use Piwik\Db;
use Piwik\Mail;
use Piwik\Plugins\QueuedTracking\Queue\Backend\MySQL;
use Piwik\SettingsPiwik;
class Tasks extends \Piwik\Plugin\Tasks
{
/**
* @var Configuration
*/
private $config;
public function __construct(Configuration $configuration)
{
$this->config = $configuration;
}
public function schedule()
{
$this->daily('optimizeQueueTable', null, self::LOWEST_PRIORITY);
$this->hourly('notifyQueueSize', null, self::LOWEST_PRIORITY);
}
/**
* run eg using ./console core:run-scheduled-tasks "Piwik\Plugins\QueuedTracking\Tasks.notifyQueueSize"
*/
public function notifyQueueSize()
{
$settings = Queue\Factory::getSettings();
if (!$settings->queueEnabled->getValue()) {
// not needed to check anything
return;
}
$emailsToNotify = $this->config->getNotifyEmails();
$threshold = $this->config->getNotifyThreshold();
if (empty($emailsToNotify) || empty($threshold) || $threshold <= 0) {
// nobody to notify or no threshold defined
return;
}
$backend = Queue\Factory::makeBackend();
$queueManager = Queue\Factory::makeQueueManager($backend);
$larger = "";
$smaller = "";
foreach ($queueManager->getAllQueues() as $queue) {
$size = $queue->getNumberOfRequestSetsInQueue();
$entriesMessage = sprintf("Queue ID %s has %s entries.<br />", $queue->getId(), $size);
if ($size >= $threshold) {
$larger .= $entriesMessage;
} else {
$smaller .= $entriesMessage;
}
}
if (!empty($larger)) {
$message = sprintf("This is a notification that the threshold %s for a single queue has been reached.<br /><br />The following queue sizes are greater than the threshold: <br />%s", $threshold, $larger);
if (!empty($smaller)) {
$message .= sprintf("<br /><br />The remaining queue sizes, which are below the threshold, are listed below: <br />%s", $smaller);
}
$message = $message . "<br /><br />Sent from " . SettingsPiwik::getPiwikUrl();
$mail = new Mail();
$mail->setDefaultFromPiwik();
foreach ($emailsToNotify as $emailToNotify) {
$mail->addTo($emailToNotify);
}
$mail->setSubject('Queued Tracking - queue size has reached your threshold');
$mail->setWrappedHtmlBody($message);
$mail->send();
}
}
/**
* run eg using ./console core:run-scheduled-tasks "Piwik\Plugins\QueuedTracking\Tasks.optimizeQueueTable"
*/
public function optimizeQueueTable()
{
$settings = Queue\Factory::getSettings();
if ($settings->isMysqlBackend() && $settings->queueEnabled->getValue()) {
$db = Db::get();
$prefix = Common::prefixTable(MySQL::QUEUED_TRACKING_TABLE_PREFIX);
$tables = $db->fetchCol("SHOW TABLES LIKE '" . $prefix . "%'");
$force = Db::isOptimizeInnoDBSupported();
// if supported, then we want to force it, as it is quite important to execute this
Db::optimizeTables($tables, $force);
}
}
}