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