-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTasks.php
235 lines (231 loc) · 10.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Piwik - 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\GoogleAnalyticsImporter;
use Piwik\Exception\DI\NotFoundException;
use Piwik\CliMulti\CliPhp;
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\SettingsPiwik;
use Piwik\SettingsServer;
use Piwik\Site;
use Piwik\Log\LoggerInterface;
use Piwik\Plugins\GoogleAnalyticsImporter\Diagnostic\RequiredExecutablesCheck;
class Tasks extends \Piwik\Plugin\Tasks
{
public const DATE_FINISHED_ENV_VAR = 'MATOMO_GOOGLE_IMPORT_END_DATE_TO_ARCHIVE';
public const SECONDS_IN_YEAR = 31557600;
// 60 * 60 * 24 * 365.25
public function schedule()
{
$this->hourly('resumeScheduledImports');
// we also run the archive command immediately after an import. the task is a safety net in case
// that doesn't work for some reason.
$this->daily('archiveImportedReports');
}
public function resumeScheduledImports()
{
$logger = StaticContainer::get(LoggerInterface::class);
$importStatus = StaticContainer::get(\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::class);
$statuses = $importStatus->getAllImportStatuses();
foreach ($statuses as $status) {
if (empty($status['idSite']) || !is_numeric($status['idSite']) || empty($status['status'])) {
$logger->info("Found broken import status entry.");
continue;
}
if ($status['status'] == \Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::STATUS_FINISHED) {
continue;
}
if (\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::isImportRunning($status)) {
continue;
}
if ($status['status'] == \Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::STATUS_ERRORED) {
$logger->info('Google Analytics import into site with ID = {idSite} encountered an unexpected error last time, attempting to resume.', ['idSite' => $status['idSite']]);
} else {
if ($status['status'] == \Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::STATUS_FUTURE_DATE_IMPORT_PENDING) {
$logger->info('Google Analytics import into site with ID = {idSite} importing future date', ['idSite' => $status['idSite']]);
} else {
$logger->info('Resuming import into site with ID = {idSite}.', ['idSite' => $status['idSite']]);
$importStatus->resumeImport($status['idSite']);
}
}
if (!empty($status['isGA4'])) {
self::startImportGA4($status);
}
}
$logger->info('Done scheduling imports.');
}
public function archiveImportedReports()
{
$logger = StaticContainer::get(LoggerInterface::class);
$importStatus = StaticContainer::get(\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::class);
$statuses = $importStatus->getAllImportStatuses();
foreach ($statuses as $status) {
$this->startArchive($status);
}
$logger->info('Done running archive commands.');
}
public static function startImportGA4($status)
{
if (\Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::isImportRunning($status)) {
return;
}
$logToSingleFile = StaticContainer::get('GoogleAnalyticsImporter.logToSingleFile');
$idSite = $status['idSite'];
$isVerboseLoggingEnabled = !empty($status['is_verbose_logging_enabled']);
$hostname = SettingsPiwik::getPiwikInstanceId();
$importLogFile = self::getImportLogFile($idSite, $hostname, $logToSingleFile);
if (!is_writable($importLogFile) && !is_writable(dirname($importLogFile))) {
$importLogFile = '/dev/null';
}
$cliPhp = new CliPhp();
$phpBinary = $cliPhp->findPhpBinary() ?: 'php';
$pathToConsole = '/console';
if (defined('PIWIK_TEST_MODE')) {
$pathToConsole = '/tests/PHPUnit/proxy/console';
}
$nohup = self::getNohupCommandIfPresent();
$command = "{$nohup} {$phpBinary} " . PIWIK_INCLUDE_PATH . $pathToConsole . ' ';
if (!empty($hostname)) {
$command .= '--matomo-domain=' . escapeshellarg($hostname) . ' ';
}
$command .= 'googleanalyticsimporter:import-ga4-reports --idsite=' . (int) $idSite;
if ($isVerboseLoggingEnabled) {
$command .= ' -vvv';
}
if ($logToSingleFile || !$isVerboseLoggingEnabled) {
$command .= ' >> ';
} else {
$command .= ' > ';
}
$command .= $importLogFile . ' 2>&1 &';
$logger = StaticContainer::get(LoggerInterface::class);
$logger->debug("Import command: {command}", ['command' => $command]);
static::exec($shouldUsePassthru = \false, $command);
}
public static function startArchive(array $status, $wait = \false, $lastDayArchived = null, $checkIsRunning = \true)
{
$logger = StaticContainer::get(LoggerInterface::class);
if (empty($status['idSite']) || !is_numeric($status['idSite']) || empty($status['status'])) {
$logger->info("Found broken import status entry.");
return;
}
if (empty($status['last_date_imported'])) {
$logger->info("Import for site ID = {$status['idSite']} has not imported any data yet, skipping archive job.");
return;
}
if ($checkIsRunning && \Piwik\Plugins\GoogleAnalyticsImporter\ImportStatus::isImportRunning($status)) {
$logger->info("Import is currently running for site ID = {$status['idSite']}, not starting archiving right now.");
return;
}
try {
$lastDateImported = Date::factory($status['last_date_imported']);
} catch (\Exception $ex) {
$logger->info("Found broken import status entry: invalid last imported date '{$status['last_date_imported']}' for site ID = {$status['idSite']}");
return;
}
if (empty($lastDayArchived)) {
try {
$lastDayArchived = empty($status['last_day_archived']) ? null : Date::factory($status['last_day_archived']);
} catch (\Exception $ex) {
$logger->info("Found broken import status entry: invalid last day archived date '{$status['last_day_archived']}' for site ID = {$status['idSite']}");
return;
}
}
if (!empty($lastDayArchived) && $lastDateImported->isEarlier($lastDayArchived)) {
$logger->info("Last archived date ({$lastDayArchived->toString()}) is earlier than last import date ({$lastDateImported->toString()}, no need to archive for site ID = {$status['idSite']}");
return;
}
$idSite = (int) $status['idSite'];
if (empty($lastDayArchived)) {
if (!empty($status['import_range_start'])) {
$lastDayArchived = Date::factory($status['import_range_start']);
} else {
$lastDayArchived = Date::factory(Site::getCreationDateFor($idSite));
}
}
$hostname = SettingsPiwik::getPiwikInstanceId();
$logToSingleFile = StaticContainer::get('GoogleAnalyticsImporter.logToSingleFile');
$archiveLogFile = self::getArchiveLogFile($idSite, $hostname, $logToSingleFile);
if (!is_writable($archiveLogFile) && !is_writable(dirname($archiveLogFile))) {
$archiveLogFile = '/dev/null';
}
$dateRange = $lastDayArchived->toString() . ',' . $lastDateImported->toString();
$pathToConsole = '/console';
if (defined('PIWIK_TEST_MODE')) {
$pathToConsole = '/tests/PHPUnit/proxy/console';
}
$cliPhp = new CliPhp();
$phpBinary = $cliPhp->findPhpBinary() ?: 'php';
$nohup = self::getNohupCommandIfPresent();
$command = self::DATE_FINISHED_ENV_VAR . '=' . $lastDateImported->toString();
if (StaticContainer::get('GoogleAnalyticsImporter.logToSingleFile')) {
$command .= ' MATOMO_GA_IMPORTER_LOG_TO_SINGLE_FILE=' . $idSite;
}
$command .= " {$nohup} {$phpBinary} " . PIWIK_INCLUDE_PATH . $pathToConsole . ' ';
if (!empty($hostname)) {
$command .= '--matomo-domain=' . escapeshellarg($hostname) . ' ';
}
$command .= 'core:archive --disable-scheduled-tasks --force-idsites=' . $idSite . ' --force-periods=week,month,year --force-date-range=' . $dateRange;
if (!$wait) {
if ($logToSingleFile) {
$command .= ' >> ';
} else {
$command .= ' > ';
}
$command .= $archiveLogFile . ' 2>&1 &';
}
$logger->debug("Archive command for imported site: {command}", ['command' => $command]);
static::exec($shouldUsePassthru = $wait, $command);
}
public static function exec($shouldUsePassthru, $command)
{
if ($shouldUsePassthru) {
passthru($command);
} else {
exec($command);
}
}
public static function getArchiveLogFile($idSite, $hostname, $logToSingleFile)
{
if ($logToSingleFile) {
return StaticContainer::get('path.tmp') . '/logs/gaimport.log';
}
return StaticContainer::get('path.tmp') . '/logs/gaimportlog.archive.' . $idSite . '.' . escapeshellcmd($hostname) . '.log';
}
public static function getImportLogFile($idSite, $hostname, $logToSingleFile)
{
if ($logToSingleFile) {
return StaticContainer::get('path.tmp') . '/logs/gaimport.log';
}
return StaticContainer::get('path.tmp') . '/logs/gaimportlog.' . $idSite . '.' . escapeshellcmd($hostname) . '.log';
}
private static function sanitizeArg($gaDimension)
{
return preg_replace('/[^a-zA-Z0-9:_-]]/', '', $gaDimension);
}
private static function getNohupCommandIfPresent()
{
try {
$useNohup = StaticContainer::get('GoogleAnalyticsImporter.useNohup');
if (!$useNohup) {
return '';
}
} catch (NotFoundException $ex) {
// ignore
}
if (SettingsServer::isWindows()) {
return '';
}
$requiredExecutablesCheck = StaticContainer::get(RequiredExecutablesCheck::class);
if ($requiredExecutablesCheck->isNohupPresent()) {
return 'nohup';
} else {
return '';
}
}
}