From 8ffa6ba4e193b4b66a399e6d01050665ffc248cf Mon Sep 17 00:00:00 2001
From: Thomas Steur <tsteur@users.noreply.github.com>
Date: Wed, 17 May 2017 17:18:04 +0000
Subject: [PATCH] added new events for scheduled tasks

---
 CHANGELOG.md                 |  5 +++++
 core/Scheduler/Scheduler.php | 29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ce9011186d..9f2231edd1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,11 @@ This is the Developer Changelog for Piwik platform developers. All changes in ou
 
 The Product Changelog at **[piwik.org/changelog](http://piwik.org/changelog)** lets you see more details about any Piwik release, such as the list of new guides and FAQs, security fixes, and links to all closed issues. 
 
+## Piwik 3.0.5
+
+### New APIs
+* The events `ScheduledTasks.shouldExecuteTask`, `ScheduledTasks.execute`, `ScheduledTasks.execute.end` have been added to customize the behaviour of scheduled tasks.
+
 ## Piwik 3.0.4
 
 ### New APIs
diff --git a/core/Scheduler/Scheduler.php b/core/Scheduler/Scheduler.php
index 86b8f44614..44971ab4a0 100644
--- a/core/Scheduler/Scheduler.php
+++ b/core/Scheduler/Scheduler.php
@@ -9,6 +9,7 @@
 namespace Piwik\Scheduler;
 
 use Exception;
+use Piwik\Piwik;
 use Piwik\Timer;
 use Psr\Log\LoggerInterface;
 
@@ -120,6 +121,17 @@ class Scheduler
                     $this->logger->debug("Task {task} is scheduled to run again for {date}.", array('task' => $taskName, 'date' => $rescheduledDate));
                 }
 
+                /**
+                 * Triggered before a task is executed.
+                 *
+                 * A plugin can listen to it and modify whether a specific task should be executed or not. This way
+                 * you can force certain tasks to be executed more often or for example to be never executed.
+                 *
+                 * @param bool &$shouldExecuteTask Decides whether the task will be executed.
+                 * @param Task $task The task that is about to be executed.
+                 */
+                Piwik::postEvent('ScheduledTasks.shouldExecuteTask', array(&$shouldExecuteTask, $task));
+
                 if ($shouldExecuteTask) {
                     $message = $this->executeTask($task);
 
@@ -222,6 +234,13 @@ class Scheduler
 
         $timer = new Timer();
 
+        /**
+         * Triggered directly before a scheduled task is executed
+         *
+         * @param Task $task  The task that is about to be executed
+         */
+        Piwik::postEvent('ScheduledTasks.execute', array(&$task));
+
         try {
             $callable = array($task->getObjectInstance(), $task->getMethodName());
             call_user_func($callable, $task->getMethodParameter());
@@ -232,6 +251,16 @@ class Scheduler
 
         $this->isRunningTask = false;
 
+        /**
+         * Triggered after a scheduled task is successfully executed.
+         *
+         * You can use the event to execute for example another task whenever a specific task is executed or to clean up
+         * certain resources.
+         *
+         * @param Task $task The task that was just executed
+         */
+        Piwik::postEvent('ScheduledTasks.execute.end', array(&$task));
+
         $this->logger->info("Scheduler: finished. {timeElapsed}", array(
             'timeElapsed' => $timer,
         ));
-- 
GitLab