Config
4 months ago
Db
3 months ago
Handler
2 years ago
Visit
1 month ago
Action.php
3 months ago
ActionPageview.php
2 years ago
BotRequest.php
3 months ago
BotRequestProcessor.php
1 month ago
Cache.php
6 months ago
Db.php
1 year ago
Failures.php
6 months ago
FingerprintSalt.php
1 year ago
GoalManager.php
1 month ago
Handler.php
2 years ago
IgnoreCookie.php
1 year ago
LogTable.php
1 year ago
Model.php
6 months ago
PageUrl.php
3 weeks ago
Request.php
1 month ago
RequestHandlerTrait.php
4 months ago
RequestProcessor.php
1 month ago
RequestSet.php
6 months ago
Response.php
3 months ago
ScheduledTasksRunner.php
1 year ago
Settings.php
3 months ago
TableLogAction.php
6 months ago
TrackerCodeGenerator.php
1 year ago
TrackerConfig.php
1 month ago
Visit.php
3 months ago
VisitExcluded.php
3 months ago
VisitInterface.php
3 months ago
Visitor.php
1 month ago
VisitorNotFoundInDb.php
1 month ago
VisitorRecognizer.php
1 year ago
ScheduledTasksRunner.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Tracker; |
| 10 | |
| 11 | use Piwik\CliMulti; |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Option; |
| 14 | use Piwik\Tracker; |
| 15 | class ScheduledTasksRunner |
| 16 | { |
| 17 | public function shouldRun(Tracker $tracker) |
| 18 | { |
| 19 | if (Common::isPhpCliMode()) { |
| 20 | // don't run scheduled tasks in CLI mode from Tracker, this is the case |
| 21 | // where we bulk load logs & don't want to lose time with tasks |
| 22 | return \false; |
| 23 | } |
| 24 | return $tracker->shouldRecordStatistics(); |
| 25 | } |
| 26 | /** |
| 27 | * Tracker requests will automatically trigger the Scheduled tasks. |
| 28 | * This is useful for users who don't setup the cron, |
| 29 | * but still want daily/weekly/monthly PDF reports emailed automatically. |
| 30 | * |
| 31 | * This is similar to calling the API CoreAdminHome.runScheduledTasks |
| 32 | */ |
| 33 | public function runScheduledTasks() |
| 34 | { |
| 35 | $now = time(); |
| 36 | // Currently, there are no hourly tasks. When there are some, |
| 37 | // this could be too aggressive minimum interval (some hours would be skipped in case of low traffic) |
| 38 | $minimumInterval = \Piwik\Tracker\TrackerConfig::getConfigValue('scheduled_tasks_min_interval'); |
| 39 | // If the user disabled browser archiving, they have already setup a cron |
| 40 | // To avoid parallel requests triggering the Scheduled Tasks, |
| 41 | // Get last time tasks started executing |
| 42 | $cache = \Piwik\Tracker\Cache::getCacheGeneral(); |
| 43 | if ($minimumInterval <= 0 || empty($cache['isBrowserTriggerEnabled'])) { |
| 44 | Common::printDebug("-> Scheduled tasks not running in Tracker: Browser archiving is disabled."); |
| 45 | return; |
| 46 | } |
| 47 | $nextRunTime = $cache['lastTrackerCronRun'] + $minimumInterval; |
| 48 | if (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS || $cache['lastTrackerCronRun'] === \false || $nextRunTime < $now) { |
| 49 | $cache['lastTrackerCronRun'] = $now; |
| 50 | \Piwik\Tracker\Cache::setCacheGeneral($cache); |
| 51 | Option::set('lastTrackerCronRun', $cache['lastTrackerCronRun']); |
| 52 | Common::printDebug('-> Scheduled Tasks: Starting...'); |
| 53 | $invokeScheduledTasksUrl = "?module=API&format=csv&convertToUnicode=0&method=CoreAdminHome.runScheduledTasks&trigger=archivephp"; |
| 54 | $cliMulti = new CliMulti(); |
| 55 | $cliMulti->runAsSuperUser(); |
| 56 | $responses = $cliMulti->request(array($invokeScheduledTasksUrl)); |
| 57 | $resultTasks = reset($responses); |
| 58 | Common::printDebug($resultTasks); |
| 59 | Common::printDebug('Finished Scheduled Tasks.'); |
| 60 | } else { |
| 61 | Common::printDebug("-> Scheduled tasks not triggered."); |
| 62 | } |
| 63 | Common::printDebug("Next run will be from: " . date('Y-m-d H:i:s', $nextRunTime) . ' UTC'); |
| 64 | } |
| 65 | } |
| 66 |