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