Skip to content

Commit

Permalink
INT-15296: webservice to retrieve proccesor status for course and con…
Browse files Browse the repository at this point in the history
…tent events on ally
  • Loading branch information
Diego Casas committed Nov 19, 2019
1 parent 4b2ffc5 commit 3875952
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 2 deletions.
1 change: 1 addition & 0 deletions classes/push_content_updates.php
Expand Up @@ -45,6 +45,7 @@ public function handle_send_error(\Exception $e) {
$climode = get_config('tool_ally', 'push_cli_only');
// Too many errors, ensure it only runs on cli.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');

if ($climode) {
$msg = 'logger:pushcontentliveskip';
Expand Down
1 change: 1 addition & 0 deletions classes/push_course_updates.php
Expand Up @@ -45,6 +45,7 @@ public function handle_send_error(\Exception $e) {
$climode = get_config('tool_ally', 'push_cli_only');
// Too many errors, ensure it only runs on cli.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');

if ($climode) {
$msg = 'logger:pushcourseliveskip';
Expand Down
1 change: 1 addition & 0 deletions classes/push_file_updates.php
Expand Up @@ -45,6 +45,7 @@ public function handle_send_error(\Exception $e) {
$climode = get_config('tool_ally', 'push_cli_only');
// Too many errors, ensure it only runs on cli.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');

if ($climode) {
$msg = 'logger:pushfileliveskip';
Expand Down
3 changes: 3 additions & 0 deletions classes/task/content_updates_task.php
Expand Up @@ -128,6 +128,7 @@ private function push_deletes(push_config $config) {
if (!$sendsuccess) {
// Send failures occurred, might as well switch on cli only mode to avoid slowness on front end.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');
$this->clionly = true;
// Reset arrays for next payload.
$ids = [];
Expand All @@ -138,6 +139,7 @@ private function push_deletes(push_config $config) {
if ($this->clionly) {
// Successful send, enable live push updates.
set_config('push_cli_only', 0, 'tool_ally');
set_config('push_cli_only_off', time(), 'tool_ally');
$this->clionly = false;
}

Expand Down Expand Up @@ -250,6 +252,7 @@ private function push_content_updates(push_config $config) {
if ($this->clionly) {
// Successful send, enable live push updates.
set_config('push_cli_only', 0, 'tool_ally');
set_config('push_cli_only_off', time(), 'tool_ally');
$this->clionly = false;
}

Expand Down
2 changes: 2 additions & 0 deletions classes/task/course_updates_task.php
Expand Up @@ -109,6 +109,7 @@ private function push_updates(push_config $config, push_course_updates $updates)
if (!$sendsuccess) {
// Failed to send, might as well switch on cli only mode to avoid slowness on front end.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');
$this->clionly = true;
// Reset arrays for next payload.
$ids = [];
Expand All @@ -119,6 +120,7 @@ private function push_updates(push_config $config, push_course_updates $updates)
if ($this->clionly) {
// Successful send, enable live push updates.
set_config('push_cli_only', 0, 'tool_ally');
set_config('push_cli_only_off', time(), 'tool_ally');
$this->clionly = false;
}

Expand Down
4 changes: 4 additions & 0 deletions classes/task/file_updates_task.php
Expand Up @@ -116,6 +116,7 @@ private function push_updates(push_config $config, push_file_updates $updates, $
if (!$sendsuccess) {
// Failed to send, might as well switch on cli only mode to avoid slowness on front end.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');
$this->clionly = true;
// Give up at this point.
// Time stamp is set to last successful batches final file time modified.
Expand All @@ -126,6 +127,7 @@ private function push_updates(push_config $config, push_file_updates $updates, $
if ($this->clionly) {
// Successful send, enable live push updates.
set_config('push_cli_only', 0, 'tool_ally');
set_config('push_cli_only_off', time(), 'tool_ally');
$this->clionly = false;
}

Expand Down Expand Up @@ -172,6 +174,7 @@ private function push_deletes(push_config $config, push_file_updates $updates) {
if (!$sendsuccess) {
// Failed to send, might as well switch on cli only mode to avoid slowness on front end.
set_config('push_cli_only', 1, 'tool_ally');
set_config('push_cli_only_on', time(), 'tool_ally');
$this->clionly = true;
// Give up at this point.
return false;
Expand All @@ -180,6 +183,7 @@ private function push_deletes(push_config $config, push_file_updates $updates) {
if ($this->clionly) {
// Successful send, enable live push updates.
set_config('push_cli_only', 0, 'tool_ally');
set_config('push_cli_only_off', time(), 'tool_ally');
$this->clionly = false;
}

Expand Down
100 changes: 100 additions & 0 deletions classes/webservice/processors_status.php
@@ -0,0 +1,100 @@
<?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/>.

/**
* Gets the queue status.
*
* @package tool_ally
* @copyright Copyright (c) 2019 Blackboard Inc. (http://www.blackboard.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_ally\webservice;

use tool_ally\push_config;

defined('MOODLE_INTERNAL') || die();

/**
* Gets the queue status.
*
* @package tool_ally
* @copyright Copyright (c) 2019 Blackboard Inc. (http://www.blackboard.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class processors_status extends loggable_external_api {
/**
* @return \external_function_parameters
*/
public static function service_parameters() {
return new \external_function_parameters([]);
}

/**
* @return \external_single_structure
*/
public static function service_returns() {
return new \external_single_structure([
'is_valid' => new \external_value(PARAM_BOOL, 'Is the configuration valid?'),
'is_cli_only' => new \external_value(PARAM_BOOL, 'is the processor set to push only?'),
'when_cli_only_on' => new \external_value(PARAM_INT, 'When the cli went On'),
'when_cli_only_off' => new \external_value(PARAM_INT, 'when the cli went Off'),
'content_events' => new \external_value(PARAM_INT, 'Amount of content events in the queue'),
'oldest_content_event' => new \external_value(PARAM_INT, 'timestamp for oldest content event queued'),
'course_events' => new \external_value(PARAM_INT, 'Amount of course events in the queue'),
'oldest_course_event' => new \external_value(PARAM_INT, 'timestamp for oldest course event queued')
]);
}

/**
* @param int $id Course id.
* @return array
*/
public static function execute_service() {
global $DB;

$config = new push_config();
$contentquery = $DB->get_record('tool_ally_content_queue', [], 'count(id) as amount, min(eventtime) as oldest');
$contentqueue = self::cast($contentquery);
$deletedquery = $DB->get_record('tool_ally_deleted_content', [], 'count(id) as amount, min(timedeleted) as oldest');
$deletedqueue = self::cast($deletedquery);
$coursequery = $DB->get_record('tool_ally_course_event', [], 'count(id) as amount, min(time) as oldest');
$coursequeue = self::cast($coursequery);
$oldestcontent = null;
if ($contentqueue->oldest != null && $deletedqueue->oldest != null) {
$oldestcontent = $contentqueue->oldest < $deletedqueue->oldest ? $contentqueue->oldest : $deletedqueue->oldest;
} else if ($contentqueue->oldest == null && $deletedqueue->oldest != null) {
$oldestcontent = $deletedqueue ->oldest;
} else if ($contentqueue->oldest != null && $deletedqueue->oldest == null) {
$oldestcontent = $contentqueue ->oldest;
}
return (object)[
'is_valid' => $config->is_valid(),
'is_cli_only' => $config->is_cli_only(),
'when_cli_only_on' => (int) get_config('tool_ally', 'push_cli_only_on'),
'when_cli_only_off' => (int) get_config('tool_ally', 'push_cli_only_off'),
'content_events' => $contentqueue->amount + $deletedqueue->amount,
'oldest_content_event' => $oldestcontent,
'course_events' => $coursequeue->amount,
'oldest_course_event' => $coursequeue->oldest,
];
}
public static function cast($queue) {
$queue->amount = (int) $queue->amount;
$queue->oldest = (int) $queue->oldest;
return $queue;
}
}
12 changes: 11 additions & 1 deletion db/services.php
Expand Up @@ -141,6 +141,15 @@
'capabilities' => 'tool/ally:viewlogs',
'ajax' => true,
'loginrequired' => true
],
'tool_ally_processors_status' => [
'classname' => 'tool_ally\\webservice\\processors_status',
'methodname' => 'service',
'description' => 'get processor status',
'type' => 'read',
'capabilities' => '',
'ajax' => true,
'loginrequired' => true
]
];

Expand All @@ -167,7 +176,8 @@
'tool_ally_get_course_user_count',
'tool_ally_get_invalid_files',
'tool_ally_get_course_invalid_files',
'tool_ally_get_logs'
'tool_ally_get_logs',
'tool_ally_processors_status'
],
'enabled' => 0,
'restrictedusers' => 0,
Expand Down
80 changes: 80 additions & 0 deletions tests/webservice_processors_status.php
@@ -0,0 +1,80 @@
<?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/>.

/**
* Test for Process status webservice.
*
* @package tool_ally
* @copyright Copyright (c) 2019 Blackboard Inc. (http://www.blackboard.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use tool_ally\webservice\processors_status;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__.'/abstract_testcase.php');

/**
* Test for content webservice.
*
* @package tool_ally
* @copyright Copyright (c) 2019 Blackboard Inc. (http://www.blackboard.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_ally_webservice_processor_status_testcase extends tool_ally_abstract_testcase {
public function test_service_return() {
$returns = processors_status::service_returns();
$this->assertTrue($returns instanceof external_single_structure);
}
public function test_service() {

// Save timestamp to compare to oldest event created.
$basetime = time();
$this->resetAfterTest();
$this->setAdminUser();

set_config('push_cli_only', 1, 'tool_ally');
// Trigger 3 course event.
$course = $this->getDataGenerator()->create_course();
$coursetime = time();
$this->getDataGenerator()->create_course();
$this->getDataGenerator()->create_course();
$section = $this->getDataGenerator()->create_course_section(
['section' => 1, 'course' => $course]);
// Trigger 3 content event.
course_update_section($course, $section, ['summary' => 'test string']);
$contenttime = time();
course_update_section($course, $section, ['summary' => 'test string1']);
course_update_section($course, $section, ['summary' => 'test string2']);

$returns = processors_status::execute_service();
$this->assertObjectHasAttribute('is_valid', $returns);
$this->assertTrue($returns->is_valid == false);
$this->assertObjectHasAttribute('is_cli_only', $returns);
$this->assertTrue($returns->is_cli_only == 1);
$this->assertObjectHasAttribute('when_cli_only_on', $returns);
$this->assertObjectHasAttribute('when_cli_only_off', $returns);
$this->assertObjectHasAttribute('content_events', $returns);
$this->assertEquals(3, $returns->content_events);
$this->assertObjectHasAttribute('oldest_content_event', $returns);
$this->assertTrue(($basetime <= $returns->oldest_content_event) && ($returns->oldest_content_event <= $contenttime));
$this->assertObjectHasAttribute('course_events', $returns);
$this->assertEquals(3, $returns->course_events);
$this->assertObjectHasAttribute('oldest_course_event', $returns);
$this->assertTrue(($basetime <= $returns->oldest_course_event) && ($returns->oldest_course_event <= $coursetime));
}
}
2 changes: 1 addition & 1 deletion version.php
Expand Up @@ -27,7 +27,7 @@
/** @var stdClass $plugin */
$plugin->component = 'tool_ally';
$plugin->release = '3.6.7';
$plugin->version = 2019061200;
$plugin->version = 2019061201;
$plugin->requires = 2018120300;
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = [
Expand Down

0 comments on commit 3875952

Please sign in to comment.