MDL-61960 blocks: Standard block plugins use legacy cron

This commit is contained in:
Farhan Karmali 2018-05-29 01:29:12 +05:30
parent 0180369b27
commit fad9dc9290
11 changed files with 272 additions and 104 deletions

View file

@ -228,16 +228,6 @@ class block_recent_activity extends block_base {
return array('all' => true, 'my' => false, 'tag' => false);
}
/**
* Remove old entries from table block_recent_activity
*/
public function cron() {
global $DB;
// Those entries will never be displayed as RECENT anyway.
$DB->delete_records_select('block_recent_activity', 'timecreated < ?',
array(time() - COURSE_MAX_RECENT_PERIOD));
}
/**
* Migrates entries from table {log} into {block_recent_activity}
*

View file

@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Task for updating RSS feeds for rss client block
*
* @package block_recent_activity
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_recent_activity\task;
defined('MOODLE_INTERNAL') || die();
/**
* Task for updating RSS feeds for rss client block
*
* @package block_recent_activity
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cleanup extends \core\task\scheduled_task {
/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('cleanuptask', 'block_recent_activity');
}
/**
* Remove old entries from table block_recent_activity
*/
public function execute() {
global $DB;
// Those entries will never be displayed as RECENT anyway.
$DB->delete_records_select('block_recent_activity', 'timecreated < ?',
array(time() - COURSE_MAX_RECENT_PERIOD));
}
}

View file

@ -0,0 +1,39 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Task definition for block_recent_activity.
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @package block_recent_activity
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => '\block_recent_activity\task\cleanup',
'blocking' => 0,
'minute' => 'R',
'hour' => 'R',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
'disabled' => 0
)
);

View file

@ -23,6 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['cleanuptask'] = 'Cleanup task for recent activity block';
$string['pluginname'] = 'Recent activity';
$string['privacy:metadata'] = 'The recent activity block contains a cache of data stored elsewhere in Moodle.';
$string['privacy:metadata:block_recent_activity'] = 'Temporary log of recent teacher activity. Removed after two days';

View file

@ -24,7 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2018051400; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2018052900; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2018050800; // Requires this Moodle version
$plugin->component = 'block_recent_activity'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 24*3600; // Cron interval 1 day.

View file

@ -283,95 +283,13 @@
}
}
/**
* cron - goes through all the feeds. If the feed has a skipuntil value
* that is less than the current time cron will attempt to retrieve it
* with the cache duration set to 0 in order to force the retrieval of
* the item and refresh the cache.
*
* If a feed fails then the skipuntil time of that feed is set to be
* later than the next expected cron time. The amount of time will
* increase each time the fetch fails until the maximum is reached.
*
* If a feed that has been failing is successfully retrieved it will
* go back to being handled as though it had never failed.
*
* CRON should therefor process requests for permanently broken RSS
* feeds infrequently, and temporarily unavailable feeds will be tried
* less often until they become available again.
*
* @return boolean Always returns true
*/
function cron() {
global $CFG, $DB;
require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
// Get the legacy cron time, strangely the cron property of block_base
// does not seem to get set. This means we must retrive it here.
$this->cron = $DB->get_field('block', 'cron', array('name' => 'rss_client'));
// We are going to measure execution times
$starttime = microtime();
$starttimesec = time();
// Fetch all site feeds.
$rs = $DB->get_recordset('block_rss_client');
$counter = 0;
mtrace('');
foreach ($rs as $rec) {
mtrace(' ' . $rec->url . ' ', '');
// Skip feed if it failed recently.
if ($starttimesec < $rec->skipuntil) {
mtrace('skipping until ' . userdate($rec->skipuntil));
continue;
}
// Fetch the rss feed, using standard simplepie caching
// so feeds will be renewed only if cache has expired
core_php_time_limit::raise(60);
$feed = new moodle_simplepie();
// set timeout for longer than normal to be agressive at
// fetching feeds if possible..
$feed->set_timeout(40);
$feed->set_cache_duration(0);
$feed->set_feed_url($rec->url);
$feed->init();
if ($feed->error()) {
// Skip this feed (for an ever-increasing time if it keeps failing).
$rec->skiptime = $this->calculate_skiptime($rec->skiptime);
$rec->skipuntil = time() + $rec->skiptime;
$DB->update_record('block_rss_client', $rec);
mtrace("Error: could not load/find the RSS feed - skipping for {$rec->skiptime} seconds.");
} else {
mtrace ('ok');
// It worked this time, so reset the skiptime.
if ($rec->skiptime > 0) {
$rec->skiptime = 0;
$rec->skipuntil = 0;
$DB->update_record('block_rss_client', $rec);
}
// Only increase the counter when a feed is sucesfully refreshed.
$counter ++;
}
}
$rs->close();
// Show times
mtrace($counter . ' feeds refreshed (took ' . microtime_diff($starttime, microtime()) . ' seconds)');
return true;
}
/**
* Calculates a new skip time for a record based on the current skip time.
*
* @param int $currentskip The curreent skip time of a record.
* @return int A new skip time that should be set.
*/
protected function calculate_skiptime($currentskip) {
public function calculate_skiptime($currentskip) {
// The default time to skiptime.
$newskiptime = $this->cron * 1.1;
if ($currentskip > 0) {

View file

@ -0,0 +1,124 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Task for updating RSS feeds for rss client block
*
* @package block_rss_client
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\task;
defined('MOODLE_INTERNAL') || die();
/**
* Task for updating RSS feeds for rss client block
*
* @package block_rss_client
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class refreshfeeds extends \core\task\scheduled_task {
/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('refreshfeedstask', 'block_rss_client');
}
/**
* This task goes through all the feeds. If the feed has a skipuntil value
* that is less than the current time cron will attempt to retrieve it
* with the cache duration set to 0 in order to force the retrieval of
* the item and refresh the cache.
*
* If a feed fails then the skipuntil time of that feed is set to be
* later than the next expected task time. The amount of time will
* increase each time the fetch fails until the maximum is reached.
*
* If a feed that has been failing is successfully retrieved it will
* go back to being handled as though it had never failed.
*
* Task should therefore process requests for permanently broken RSS
* feeds infrequently, and temporarily unavailable feeds will be tried
* less often until they become available again.
*/
public function execute() {
global $CFG, $DB;
require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
// We are going to measure execution times.
$starttime = microtime();
$starttimesec = time();
// Fetch all site feeds.
$rs = $DB->get_recordset('block_rss_client');
$counter = 0;
mtrace('');
foreach ($rs as $rec) {
mtrace(' ' . $rec->url . ' ', '');
// Skip feed if it failed recently.
if ($starttimesec < $rec->skipuntil) {
mtrace('skipping until ' . userdate($rec->skipuntil));
continue;
}
// Fetch the rss feed, using standard simplepie caching
// so feeds will be renewed only if cache has expired.
\core_php_time_limit::raise(60);
$feed = new \moodle_simplepie();
// Set timeout for longer than normal to be agressive at
// fetching feeds if possible..
$feed->set_timeout(40);
$feed->set_cache_duration(0);
$feed->set_feed_url($rec->url);
$feed->init();
if ($feed->error()) {
// Skip this feed (for an ever-increasing time if it keeps failing).
$block = new \block_rss_client();
$rec->skiptime = $block->calculate_skiptime($rec->skiptime);
$rec->skipuntil = time() + $rec->skiptime;
$DB->update_record('block_rss_client', $rec);
mtrace("Error: could not load/find the RSS feed - skipping for {$rec->skiptime} seconds.");
} else {
mtrace ('ok');
// It worked this time, so reset the skiptime.
if ($rec->skiptime > 0) {
$rec->skiptime = 0;
$rec->skipuntil = 0;
$DB->update_record('block_rss_client', $rec);
}
// Only increase the counter when a feed is sucesfully refreshed.
$counter ++;
}
}
$rs->close();
// Show times.
mtrace($counter . ' feeds refreshed (took ' . microtime_diff($starttime, microtime()) . ' seconds)');
}
}

View file

@ -0,0 +1,39 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Task definition for block_rss_client.
* @author Farhan Karmali <farhan6318@gmail.com>
* @copyright Farhan Karmali 2018
* @package block_rss_client
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => '\block_rss_client\task\refreshfeeds',
'blocking' => 0,
'minute' => '*/5',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
'disabled' => 0
)
);

View file

@ -72,6 +72,7 @@ $string['privacy:metadata:block_rss_client:title'] = 'The title of the RSS feed.
$string['privacy:metadata:block_rss_client:url'] = 'The URL of the RSS feed.';
$string['privacy:metadata:block_rss_client:userid'] = 'The ID of the user that added the RSS feed.';
$string['remotenewsfeed'] = 'Remote news feed';
$string['refreshfeedstask'] = 'Refresh RSS feeds task';
$string['rss_client:addinstance'] = 'Add a new remote RSS feeds block';
$string['rss_client:createprivatefeeds'] = 'Create private RSS feeds';
$string['rss_client:createsharedfeeds'] = 'Create shared RSS feeds';

View file

@ -55,12 +55,12 @@ class block_rss_client_cron_testcase extends advanced_testcase {
);
$DB->insert_record('block_rss_client', $record);
$block = new block_rss_client();
$task = new \block_rss_client\task\refreshfeeds();
ob_start();
// Silence SimplePie php notices.
$errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
$block->cron();
$task->execute();
error_reporting($errorlevel);
$cronoutput = ob_get_clean();
@ -69,7 +69,7 @@ class block_rss_client_cron_testcase extends advanced_testcase {
}
/**
* Test that when a feed has an error the skip time is increaed correctly.
* Test that when a feed has an error the skip time is increased correctly.
*/
public function test_error() {
global $DB, $CFG;
@ -114,20 +114,20 @@ class block_rss_client_cron_testcase extends advanced_testcase {
);
$record3->id = $DB->insert_record('block_rss_client', $record3);
// Run the cron.
$block = new block_rss_client();
// Run the scheduled task.
$task = new \block_rss_client\task\refreshfeeds();
ob_start();
// Silence SimplePie php notices.
$errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
$block->cron();
$task->execute();
error_reporting($errorlevel);
$cronoutput = ob_get_clean();
$skiptime1 = $record->skiptime * 2;
$message1 = 'http://example.com/rss Error: could not load/find the RSS feed - skipping for ' . $skiptime1 . ' seconds.';
$this->assertContains($message1, $cronoutput);
$skiptime2 = 330; // Assumes that the cron time in the version file is 300.
$skiptime2 = 0;
$message2 = 'http://example.com/rss2 Error: could not load/find the RSS feed - skipping for ' . $skiptime2 . ' seconds.';
$this->assertContains($message2, $cronoutput);
$skiptime3 = block_rss_client::CLIENT_MAX_SKIPTIME;

View file

@ -24,7 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2018051400; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2018052900; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2018050800; // Requires this Moodle version
$plugin->component = 'block_rss_client'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 300; // Set min time between cron executions to 300 secs (5 mins)