Skip to content

Commit

Permalink
Added log statements in the task scheduler to debug future problems and
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Feb 15, 2015
1 parent 2c1d6f9 commit a004412
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 14 additions & 1 deletion core/Scheduler/Scheduler.php
Expand Up @@ -10,6 +10,7 @@

use Exception;
use Piwik\Timer;
use Psr\Log\LoggerInterface;

/**
* Schedules task execution.
Expand Down Expand Up @@ -62,10 +63,16 @@ class Scheduler
*/
private $loader;

public function __construct(TaskLoader $loader)
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(TaskLoader $loader, LoggerInterface $logger)
{
$this->timetable = new Timetable();
$this->loader = $loader;
$this->logger = $logger;
}

/**
Expand All @@ -85,6 +92,8 @@ public function run()
{
$tasks = $this->loader->loadTasks();

$this->logger->debug('{count} scheduled tasks loaded', array('count' => count($tasks)));

// remove from timetable tasks that are not active anymore
$this->timetable->removeInactiveTasks($tasks);

Expand Down Expand Up @@ -131,6 +140,8 @@ public function run()
*/
public function rescheduleTask(Task $task)
{
$this->logger->debug('Rescheduling task {task}', array('task' => $task->getName()));

$this->timetable->rescheduleTask($task);
}

Expand Down Expand Up @@ -166,6 +177,8 @@ public function getScheduledTimeForMethod($className, $methodName, $methodParame
*/
private function executeTask($task)
{
$this->logger->debug('Running task {task}', array('task' => $task->getName()));

try {
$timer = new Timer();
call_user_func(array($task->getObjectInstance(), $task->getMethodName()), $task->getMethodParameter());
Expand Down
5 changes: 3 additions & 2 deletions tests/PHPUnit/Unit/Scheduler/SchedulerTest.php
Expand Up @@ -13,6 +13,7 @@
use Piwik\Scheduler\Task;
use Piwik\Scheduler\Timetable;
use Piwik\Tests\Framework\Mock\PiwikOption;
use Psr\Log\NullLogger;
use ReflectionProperty;

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ public function testGetScheduledTimeForMethod($expectedTime, $className, $method
self::stubPiwikOption($timetable);

$taskLoader = $this->getMock('Piwik\Scheduler\TaskLoader');
$scheduler = new Scheduler($taskLoader);
$scheduler = new Scheduler($taskLoader, new NullLogger());

$this->assertEquals($expectedTime, $scheduler->getScheduledTimeForMethod($className, $methodName, $methodParameter));

Expand Down Expand Up @@ -159,7 +160,7 @@ public function testRun($expectedTimetable, $expectedExecutedTasks, $timetableBe
// stub the piwik option object to control the returned option value
self::stubPiwikOption(serialize($timetableBeforeTaskExecution));

$scheduler = new Scheduler($taskLoader);
$scheduler = new Scheduler($taskLoader, new NullLogger());

// execute tasks
$executionResults = $scheduler->run();
Expand Down

0 comments on commit a004412

Please sign in to comment.