Skip to content

Commit

Permalink
GeoIP first will update the next day, then weekly/monthly after that. F…
Browse files Browse the repository at this point in the history
…ixes matomo-org#11006 (matomo-org#11659)

* Feature: GeoIP first update will be the next day. Then Monthly or Weekly

* Get unit test to pass.
  • Loading branch information
diegobanos authored and diosmosis committed Mar 4, 2019
1 parent fd2a9c1 commit eb76c69
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
12 changes: 12 additions & 0 deletions core/Date.php
Expand Up @@ -130,6 +130,8 @@ public static function factory($dateString, $timezone = null)
$date = self::now();
} elseif ($dateString == 'today') {
$date = self::today();
} else if ($dateString == 'tomorrow') {
$date = self::tomorrow();
} elseif ($dateString == 'yesterday') {
$date = self::yesterday();
} elseif ($dateString == 'yesterdaySameTime') {
Expand Down Expand Up @@ -558,6 +560,16 @@ public static function today()
return new Date(strtotime(date("Y-m-d 00:00:00")));
}

/**
* Returns a date object set to tomorrow at midnight in UTC.
*
* @return \Piwik\Date
*/
public static function tomorrow()
{
return new Date(strtotime('tomorrow'));
}

/**
* Returns a date object set to yesterday at midnight in UTC.
*
Expand Down
18 changes: 18 additions & 0 deletions core/Scheduler/Scheduler.php
Expand Up @@ -180,6 +180,24 @@ public function rescheduleTask(Task $task)
$this->timetable->rescheduleTask($task);
}

/**
* Determines a task's scheduled time and persists it, overwriting the previous scheduled time.
*
* Call this method if your task's scheduled time has changed due to, for example, an option that
* was changed.
*
* The task will be run the first time tomorrow.
*
* @param Task $task Describes the scheduled task being rescheduled.
* @api
*/
public function rescheduleTaskAndRunTomorrow(Task $task)
{
$this->logger->debug('Rescheduling task and setting first run for tomorrow {task}', array('task' => $task->getName()));

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

/**
* Returns true if the scheduler is currently running a task.
*
Expand Down
11 changes: 11 additions & 0 deletions core/Scheduler/Timetable.php
Expand Up @@ -116,6 +116,17 @@ public function rescheduleTask(Task $task)
return Date::factory($rescheduledTime);
}

public function rescheduleTaskAndRunTomorrow(Task $task)
{
$tomorrow = Date::factory('tomorrow');

// update the scheduled time
$this->timetable[$task->getName()] = $tomorrow->getTimestamp();
$this->save();

return $tomorrow;
}

public function save()
{
Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable));
Expand Down
2 changes: 1 addition & 1 deletion plugins/UserCountry/GeoIPAutoUpdater.php
Expand Up @@ -369,7 +369,7 @@ public static function setUpdaterOptions($options)
/** @var Scheduler $scheduler */
$scheduler = StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler');

$scheduler->rescheduleTask(new GeoIPAutoUpdater());
$scheduler->rescheduleTaskAndRunTomorrow(new GeoIPAutoUpdater());
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/PHPUnit/Unit/DateTest.php
Expand Up @@ -50,6 +50,17 @@ public function testToday()
$this->assertEquals(date('Y-m-d') . ' 12:00:00', $date->getDatetime());
}

/**
* create tomorrow object check that timestamp is correct (midnight)
*
* @group Core
*/
public function testTomorrow()
{
$date = Date::tomorrow();
$this->assertEquals(strtotime(date("Y-m-d ", strtotime('+1day')) . " 00:00:00"), $date->getTimestamp());
}

/**
* create today object check that timestamp is correct (midnight)
*
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPUnit/Unit/Scheduler/SchedulerTest.php
Expand Up @@ -8,6 +8,7 @@

namespace Piwik\Tests\Unit\Scheduler;

use Piwik\Date;
use Piwik\Plugin;
use Piwik\Scheduler\Scheduler;
use Piwik\Scheduler\Task;
Expand Down Expand Up @@ -58,6 +59,24 @@ public function testGetScheduledTimeForMethod($expectedTime, $className, $method
self::resetPiwikOption();
}

public function testRescheduleTaskAndRunTomorrow()
{
$timetable = serialize(self::getTestTimetable());
self::stubPiwikOption($timetable);

$plugin = new Plugin();
$task = new Task($plugin, 'getVersion', null, null);

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

$scheduler->rescheduleTaskAndRunTomorrow($task);

$this->assertEquals(Date::factory('tomorrow')->getTimeStamp(), $scheduler->getScheduledTimeForMethod(Plugin::class, 'getVersion', null));
}

/**
* Dataprovider for testRun
*/
Expand Down
22 changes: 21 additions & 1 deletion tests/PHPUnit/Unit/Scheduler/TimetableTest.php
Expand Up @@ -8,7 +8,9 @@

namespace Piwik\Tests\Unit\Scheduler;

use Piwik\Date;
use Piwik\Plugin;
use Piwik\Scheduler\Task;
use Piwik\Scheduler\Timetable;
use Piwik\Tests\Framework\Mock\PiwikOption;
use ReflectionProperty;
Expand All @@ -23,6 +25,11 @@ class TimetableTest extends \PHPUnit_Framework_TestCase
'PrivacyManager.deleteReportData_1' => 1322229607,
);

public function tearDown()
{
self::resetPiwikOption();
}

/**
* Dataprovider for testGetTimetableFromOptionValue
*/
Expand Down Expand Up @@ -57,8 +64,21 @@ public function testGetTimetableFromOptionValue($expectedTimetable, $option)

$timetable = new Timetable();
$this->assertEquals($expectedTimetable, $timetable->getTimetable());
}

self::resetPiwikOption();
public function testRescheduleTaskAndRunTomorrow()
{
self::stubPiwikOption(serialize([]));

$timetable = new Timetable();
$task = $this->getMockBuilder(Task::class)
->disableOriginalConstructor()
->getMock();
$task->method('getName')->willReturn('taskName');

$timetable->rescheduleTaskAndRunTomorrow($task);

$this->assertEquals(Date::factory('tomorrow')->getTimeStamp(), $timetable->getTimetable()[$task->getName()]);
}

/**
Expand Down

0 comments on commit eb76c69

Please sign in to comment.