Skip to content

Commit

Permalink
Refs #4263, display next scheduled run time for geoip updater task, m…
Browse files Browse the repository at this point in the history
…ake sure updater is run in next cron run if a file is out of date, & forgot to commit new file in last commit.
  • Loading branch information
diosmosis committed Jan 7, 2014
1 parent 6a60bcd commit d1b6596
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 13 deletions.
125 changes: 125 additions & 0 deletions core/ScheduledTaskTimetable.php
@@ -0,0 +1,125 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*/

namespace Piwik;

/**
* This data structure holds the scheduled times for each active scheduled task.
*/
class ScheduledTaskTimetable
{
const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable";

private $timetable;

public function __construct()
{
$optionData = Option::get(self::TIMETABLE_OPTION_STRING);
$unserializedTimetable = @unserialize($optionData);

$this->timetable = $unserializedTimetable === false ? array() : $unserializedTimetable;
}

public function getTimetable()
{
return $this->timetable;
}

public function setTimetable($timetable)
{
$this->timetable = $timetable;
}

public function removeInactiveTasks($activeTasks)
{
$activeTaskNames = array();
foreach ($activeTasks as $task) {
$activeTaskNames[] = $task->getName();
}
foreach (array_keys($this->timetable) as $taskName) {
if (!in_array($taskName, $activeTaskNames)) {
unset($this->timetable[$taskName]);
}
}
}

public function getScheduledTaskNames()
{
return array_keys($this->timetable);
}

public function getScheduledTaskTime($taskName)
{
return isset($this->timetable[$taskName]) ? Date::factory($this->timetable[$taskName]) : false;
}

/**
* Checks if the task should be executed
*
* Task has to be executed if :
* - the task has already been scheduled once and the current system time is greater than the scheduled time.
* - execution is forced, see $forceTaskExecution
*
* @param string $taskName
*
* @return boolean
*/
public function shouldExecuteTask($taskName)
{
$forceTaskExecution =
(isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS'])
|| DEBUG_FORCE_SCHEDULED_TASKS;

return $forceTaskExecution || ($this->taskHasBeenScheduledOnce($taskName) && time() >= $this->timetable[$taskName]);
}

/**
* Checks if a task should be rescheduled
*
* Task has to be rescheduled if :
* - the task has to be executed
* - the task has never been scheduled before
*
* @param ScheduledTask $task
*
* @return boolean
*/
public function taskShouldBeRescheduled($task)
{
$taskName = $task->getName();

return !$this->taskHasBeenScheduledOnce($taskName) || $this->shouldExecuteTask($taskName);
}

public function rescheduleTask($task)
{
// update the scheduled time
$this->timetable[$task->getName()] = $task->getRescheduledTime();
$this->save();
}

public function save()
{
Option::set(self::TIMETABLE_OPTION_STRING, serialize($this->timetable));
}

public function getScheduledTimeForMethod($className, $methodName, $methodParameter = null)
{
$taskName = ScheduledTask::getTaskName($className, $methodName, $methodParameter);

return $this->taskHasBeenScheduledOnce($taskName) ? $this->timetable[$taskName] : false;
}

public function taskHasBeenScheduledOnce($taskName)
{
return isset($this->timetable[$taskName]);
}
}
3 changes: 2 additions & 1 deletion lang/en.json
Expand Up @@ -1847,7 +1847,8 @@
"GeoIPIncorrectDatabaseFormat": "Your GeoIP database does not seem to have the correct format. It may be corrupt. Make sure you are using the binary version and try replacing it with another copy.",
"DefaultLocationProviderExplanation": "You are using the default location provider, which means Piwik will guess the visitor's location based on the language they use. %1$sRead this%2$s to learn how to setup more accurate geolocation.",
"UpdaterWillRunNext": "It is next scheduled to run on %s.",
"UpdaterIsNotScheduledToRun": "It is not scheduled to run in the future."
"UpdaterIsNotScheduledToRun": "It is not scheduled to run in the future.",
"UpdaterScheduledForNextRun": "It is scheduled to run during the next archive.php cron execution."
},
"UserCountryMap": {
"map": "map",
Expand Down
10 changes: 5 additions & 5 deletions plugins/UserCountry/Controller.php
Expand Up @@ -176,10 +176,7 @@ private function setUpdaterManageVars($view)
$view->lastTimeUpdaterRun = '<strong><em>' . $lastRunTime->toString() . '</em></strong>';
}

$nextRunTime = GeoIPAutoUpdater::getNextRunTime();
if ($nextRunTime !== false) {
$view->nextTimeUpdaterRun = '<strong><em><span id="geoip-updater-next-run-time">' . $nextRunTime->toString() . '</span></em></strong>';
}
$view->nextRunTime = GeoIPAutoUpdater::getNextRunTime();
}

/**
Expand Down Expand Up @@ -212,7 +209,10 @@ public function updateGeoIPLinks()
if ($info !== false) {
return Common::json_encode($info);
} else {
return Common::json_encode(array('nextRunTime' => GeoIPAutoUpdater::getNextRunTime()->toString()));
$view = new View("@UserCountry/_updaterNextRunTime");
$view->nextRunTime = GeoIPAutoUpdater::getNextRunTime();
$nextRunTimeHtml = $view->render();
return Common::json_encode(array('nextRunTime' => $nextRunTimeHtml));
}
} catch (Exception $ex) {
return Common::json_encode(array('error' => $ex->getMessage()));
Expand Down
49 changes: 49 additions & 0 deletions plugins/UserCountry/GeoIPAutoUpdater.php
Expand Up @@ -632,4 +632,53 @@ public static function getNextRunTime()
$timetable = new ScheduledTaskTimetable();
return $timetable->getScheduledTaskTime($task->getName());
}

/**
* See {@link Piwik\ScheduledTime::getRescheduledTime()}.
*/
public function getRescheduledTime()
{
$nextScheduledTime = parent::getRescheduledTime();

// if a geoip database is out of date, run the updater as soon as possible
if ($this->isAtLeastOneGeoIpDbOutOfDate($nextScheduledTime)) {
return time();
}

return $nextScheduledTime;
}

private function isAtLeastOneGeoIpDbOutOfDate($rescheduledTime)
{
$previousScheduledRuntime = $this->getPreviousScheduledTime($rescheduledTime);

foreach (GeoIp::$dbNames as $type => $dbNames) {
$dbUrl = Option::get(self::$urlOptions[$type]);
$dbPath = GeoIp::getPathToGeoIpDatabase($dbNames);

// if there is a URL for this DB type and the GeoIP DB file's last modified time is before
// the time the updater should have been previously run, then **the file is out of date**
if ($dbUrl !== false
&& filemtime($dbPath) < $previousScheduledRuntime
) {
return true;
}
}

return false;
}

private function getPreviousScheduledTime($rescheduledTime)
{
$updaterPeriod = Option::get(self::SCHEDULE_PERIOD_OPTION_NAME);

if ($updaterPeriod == 'week') {
return Date::factory($rescheduledTime)->subWeek(1)->getTimestamp();
} else if ($updaterPeriod == 'month') {
return Date::factory($rescheduledTime)->subMonth(1)->getTimestamp();
} else {
Log::warning("Unknown GeoIP updater period found in database: %s", $updaterPeriod);
return 0;
}
}
}
3 changes: 1 addition & 2 deletions plugins/UserCountry/javascripts/userCountry.js
Expand Up @@ -186,14 +186,13 @@ $(document).ready(function () {
id: 'userCountryGeoIpUpdate'
});

$('#geoip-updater-next-run-time').text(response.nextRunTime);
$('#geoip-updater-next-run-time').html(response.nextRunTime).parent().effect('highlight', {color: '#FFFFCB'}, 2000);
}
};

// setup the auto-updater
var ajaxRequest = new ajaxHelper();
var periodSelected = $('#geoip-update-period-cell').find('input:checked').val();
console.log(periodSelected);
ajaxRequest.addParams({
period: periodSelected
}, 'get');
Expand Down
8 changes: 3 additions & 5 deletions plugins/UserCountry/templates/_updaterManage.twig
Expand Up @@ -49,11 +49,9 @@
{{ 'UserCountry_UpdaterHasNotBeenRun'|translate }}
{% endif %}
<br/><br/>
{% if nextTimeUpdaterRun|default is not empty %}
{{ 'UserCountry_UpdaterWillRunNext'|translate(nextTimeUpdaterRun)|raw }}
{% else %}
{{ 'UserCountry_UpdaterIsNotScheduledToRun'|translate }}
{% endif %}
<div id="geoip-updater-next-run-time">
{% include "@UserCountry/_updaterNextRunTime.twig" %}
</div>
{% endset %}
{{ piwik.inlineHelp(lastTimeRunNote) }}
</td>
Expand Down
9 changes: 9 additions & 0 deletions plugins/UserCountry/templates/_updaterNextRunTime.twig
@@ -0,0 +1,9 @@
{% if nextRunTime|default is not empty %}
{% if date(nextRunTime.getTimestamp()) <= date() %}
{{ 'UserCountry_UpdaterScheduledForNextRun'|translate }}
{% else %}
{{ 'UserCountry_UpdaterWillRunNext'|translate('<strong><em>' ~ nextRunTime.toString() ~ '</em></strong>')|raw }}
{% endif %}
{% else %}
{{ 'UserCountry_UpdaterIsNotScheduledToRun'|translate }}
{% endif %}

0 comments on commit d1b6596

Please sign in to comment.