Dimension
6 years ago
API.php
6 years ago
AggregatedMetric.php
6 years ago
ArchivedMetric.php
6 years ago
Archiver.php
6 years ago
Categories.php
6 years ago
ComponentFactory.php
6 years ago
ComputedMetric.php
6 years ago
ConsoleCommand.php
6 years ago
Controller.php
6 years ago
ControllerAdmin.php
6 years ago
Dependency.php
6 years ago
LogTablesProvider.php
6 years ago
Manager.php
6 years ago
Menu.php
6 years ago
MetadataLoader.php
6 years ago
Metric.php
6 years ago
PluginException.php
6 years ago
ProcessedMetric.php
6 years ago
ReleaseChannels.php
6 years ago
Report.php
6 years ago
ReportsProvider.php
6 years ago
RequestProcessors.php
6 years ago
Segment.php
6 years ago
SettingsProvider.php
6 years ago
Tasks.php
6 years ago
ThemeStyles.php
6 years ago
ViewDataTable.php
6 years ago
Visualization.php
6 years ago
WidgetsProvider.php
6 years ago
Tasks.php
155 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - 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 | namespace Piwik\Plugin; |
| 10 | |
| 11 | use Piwik\Development; |
| 12 | use Piwik\Scheduler\Schedule\Schedule; |
| 13 | use Piwik\Scheduler\Task; |
| 14 | |
| 15 | /** |
| 16 | * Base class for all Tasks declarations. |
| 17 | * Tasks are usually meant as scheduled tasks that are executed regularly by Piwik in the background. For instance |
| 18 | * once every hour or every day. This could be for instance checking for updates, sending email reports, etc. |
| 19 | * Please don't mix up tasks with console commands which can be executed on the CLI. |
| 20 | */ |
| 21 | class Tasks |
| 22 | { |
| 23 | /** |
| 24 | * @var Task[] |
| 25 | */ |
| 26 | private $tasks = array(); |
| 27 | |
| 28 | const LOWEST_PRIORITY = Task::LOWEST_PRIORITY; |
| 29 | const LOW_PRIORITY = Task::LOW_PRIORITY; |
| 30 | const NORMAL_PRIORITY = Task::NORMAL_PRIORITY; |
| 31 | const HIGH_PRIORITY = Task::HIGH_PRIORITY; |
| 32 | const HIGHEST_PRIORITY = Task::HIGHEST_PRIORITY; |
| 33 | |
| 34 | /** |
| 35 | * This method is called to collect all schedule tasks. Register all your tasks here that should be executed |
| 36 | * regularly such as daily or monthly. |
| 37 | */ |
| 38 | public function schedule() |
| 39 | { |
| 40 | // eg $this->daily('myMethodName') |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return Task[] $tasks |
| 45 | */ |
| 46 | public function getScheduledTasks() |
| 47 | { |
| 48 | return $this->tasks; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Schedule the given tasks/method to run once every hour. |
| 53 | * |
| 54 | * @param string $methodName The name of the method that will be called when the task is being |
| 55 | * exectuted. To make it work you need to create a public method having the |
| 56 | * given method name in your Tasks class. |
| 57 | * @param null|string $methodParameter Can be null if the task does not need any parameter or a string. It is not |
| 58 | * possible to specify multiple parameters as an array etc. If you need to |
| 59 | * pass multiple parameters separate them via any characters such as '###'. |
| 60 | * For instance '$param1###$param2###$param3' |
| 61 | * @param int $priority Can be any constant such as self::LOW_PRIORITY |
| 62 | * |
| 63 | * @return Schedule |
| 64 | * @api |
| 65 | */ |
| 66 | protected function hourly($methodName, $methodParameter = null, $priority = self::NORMAL_PRIORITY) |
| 67 | { |
| 68 | return $this->custom($this, $methodName, $methodParameter, 'hourly', $priority); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Schedule the given tasks/method to run once every day. |
| 73 | * |
| 74 | * See {@link hourly()} |
| 75 | * @api |
| 76 | */ |
| 77 | protected function daily($methodName, $methodParameter = null, $priority = self::NORMAL_PRIORITY) |
| 78 | { |
| 79 | return $this->custom($this, $methodName, $methodParameter, 'daily', $priority); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Schedule the given tasks/method to run once every week. |
| 84 | * |
| 85 | * See {@link hourly()} |
| 86 | * @api |
| 87 | */ |
| 88 | protected function weekly($methodName, $methodParameter = null, $priority = self::NORMAL_PRIORITY) |
| 89 | { |
| 90 | return $this->custom($this, $methodName, $methodParameter, 'weekly', $priority); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Schedule the given tasks/method to run once every month. |
| 95 | * |
| 96 | * See {@link hourly()} |
| 97 | * @api |
| 98 | */ |
| 99 | protected function monthly($methodName, $methodParameter = null, $priority = self::NORMAL_PRIORITY) |
| 100 | { |
| 101 | return $this->custom($this, $methodName, $methodParameter, 'monthly', $priority); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Schedules the given tasks/method to run depending at the given scheduled time. Unlike the convenient methods |
| 106 | * such as {@link hourly()} you need to specify the object on which the given method should be called. This can be |
| 107 | * either an instance of a class or a class name. For more information about these parameters see {@link hourly()} |
| 108 | * |
| 109 | * @param string|object $objectOrClassName |
| 110 | * @param string $methodName |
| 111 | * @param null|string $methodParameter |
| 112 | * @param string|Schedule $time |
| 113 | * @param int $priority |
| 114 | * |
| 115 | * @return \Piwik\Scheduler\Schedule\Schedule |
| 116 | * |
| 117 | * @throws \Exception If a wrong time format is given. Needs to be either a string such as 'daily', 'weekly', ... |
| 118 | * or an instance of {@link Piwik\Scheduler\Schedule\Schedule} |
| 119 | * |
| 120 | * @api |
| 121 | */ |
| 122 | protected function custom($objectOrClassName, $methodName, $methodParameter, $time, $priority = self::NORMAL_PRIORITY) |
| 123 | { |
| 124 | $this->checkIsValidTask($objectOrClassName, $methodName); |
| 125 | |
| 126 | if (is_string($time)) { |
| 127 | $time = Schedule::factory($time); |
| 128 | } |
| 129 | |
| 130 | if (!($time instanceof Schedule)) { |
| 131 | throw new \Exception('$time should be an instance of Schedule'); |
| 132 | } |
| 133 | |
| 134 | $this->scheduleTask(new Task($objectOrClassName, $methodName, $methodParameter, $time, $priority)); |
| 135 | |
| 136 | return $time; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * In case you need very high flexibility and none of the other convenient methods such as {@link hourly()} or |
| 141 | * {@link custom()} suit you, you can use this method to add a custom scheduled task. |
| 142 | * |
| 143 | * @param Task $task |
| 144 | */ |
| 145 | protected function scheduleTask(Task $task) |
| 146 | { |
| 147 | $this->tasks[] = $task; |
| 148 | } |
| 149 | |
| 150 | private function checkIsValidTask($objectOrClassName, $methodName) |
| 151 | { |
| 152 | Development::checkMethodIsCallable($objectOrClassName, $methodName, 'The registered task is not valid as the method'); |
| 153 | } |
| 154 | } |
| 155 |