Skip to content

Commit

Permalink
Merge pull request #224 from ArloSoftware/ARLO-50
Browse files Browse the repository at this point in the history
ARLO-50
  • Loading branch information
ojnadjarm authored Jul 8, 2024
2 parents 4ce14b6 + ad3b711 commit dda54e4
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 128 deletions.
42 changes: 33 additions & 9 deletions admin/apiretries.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,53 @@

use enrol_arlo\api;
use enrol_arlo\local\tablesql\apiretries;
use enrol_arlo\adminsettings\configarlostatus;

require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/tablelib.php');
require_once($CFG->dirroot . '/enrol/arlo/locallib.php');

admin_externalpage_setup('enrolsettingsarloapiretries');
$action = optional_param('action', null, PARAM_ALPHA);
$course = optional_param('course', null, PARAM_INT);
$regid = optional_param('regid', null, PARAM_INT);

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('apiretries', 'enrol_arlo'));

[$arlostatus, $desc] = configarlostatus::api_status_render();
$plugin = api::get_enrolment_plugin();
$pluginconfig = $plugin->get_plugin_config();
echo '<div class="communication-buttons d-flex align-items-center mb-3">';
echo get_string('connectionstatus', 'enrol_arlo') . $arlostatus;

if (empty($pluginconfig->get('enablecommunication'))) {
echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'enablecommunication']), get_string('enablecommunication', 'enrol_arlo'));
}

if (!empty($pluginconfig->get('redirectcount'))) {
echo get_string('apifails', 'enrol_arlo') . $pluginconfig->get('redirectcount');
echo $OUTPUT->single_button(new moodle_url($PAGE->url, ['action' => 'resetredirects']), get_string('resetredirects', 'enrol_arlo'));
}
echo '</div>';

if ($action === 'enablecommunication') {
$plugin = api::get_enrolment_plugin();
$pluginconfig = $plugin->get_plugin_config();
set_config('enablecommunication', 1, 'enrol_arlo');
$pluginconfig->set('enablecommunication', get_config('enrol_arlo','enablecommunication'));
set_config('redirectcount', 0, 'enrol_arlo');
$pluginconfig->set('redirectcount', get_config('enrol_arlo','redirectcount'));
enrol_arlo_enablecommunication();
echo $OUTPUT->notification(get_string('communication_enabled_message', 'enrol_arlo'),\core\output\notification::NOTIFY_SUCCESS);
}

if ($action === 'resetredirects') {
enrol_arlo_reset_redirects($regid);
echo $OUTPUT->notification(get_string('resetretries_message', 'enrol_arlo'),\core\output\notification::NOTIFY_SUCCESS);
}

// This will be hidden from the user interface, will only work if the params are set manually through the URL.
if ($action === 'updateall' && !empty($course)) {
enrol_arlo_update_all_course_registrations($course);
}

$report = new apiretries('enrolsettingsarloapiretries');
$report->out(apiretries::PAGINATION_MAX_LIMIT, false);
$url = new moodle_url($PAGE->url, ['action'=>'resubmit']);

$url = new moodle_url($PAGE->url, ['action' => 'enablecommunication']);
echo $OUTPUT->single_button($url, "Enable communication", 'get');
echo $OUTPUT->footer();
18 changes: 13 additions & 5 deletions classes/adminsettings/configarlostatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public function write_setting($data) {
* @throws moodle_exception
*/
public function output_html($data, $query = '') {
[$element, $description] = self::api_status_render();
return format_admin_setting($this, '', $element, $description, false, '', null, $query);
}

/**
* Render the API status.
*
* @return array An array containing the status element and description.
*/
public static function api_status_render() {
global $OUTPUT;
$apistatus = get_config('enrol_arlo', 'apistatus');
$apilastrequested = (int) get_config('enrol_arlo', 'apitimelastrequest');
Expand All @@ -94,7 +104,6 @@ public function output_html($data, $query = '') {
$statusicon = $OUTPUT->pix_icon('t/go', get_string('ok', 'enrol_arlo'));
}
$reason = get_string('apistatusok', 'enrol_arlo', userdate($apilastrequested));
$description = '';
} else if (0 == $apistatus || ($apistatus >= 400 && $apistatus < 499)) {
if ($useimageiconclass) {
$statusicon = $OUTPUT->image_icon('t/stop', get_string('notok', 'enrol_arlo'));
Expand All @@ -111,12 +120,11 @@ public function output_html($data, $query = '') {
$statusicon = $OUTPUT->pix_icon('t/stop', get_string('notok', 'enrol_arlo'));
}
$reason = get_string('apistatusservererror', 'enrol_arlo');
$url = new moodle_url('/enrol/arlo/admin/apirequests.php');
$url = new \moodle_url('/enrol/arlo/admin/apirequests.php');
$description = get_string('pleasecheckrequestlog', 'enrol_arlo', $url->out());
} else {
return '';
return ['', ''];
}
$element = '<div class="form-text">'.$statusicon.'&nbsp;'.$reason.'</div>';
return format_admin_setting($this, '', $element, $description, false, '', null, $query);
return ['<div class="form-text">'.$statusicon.'&nbsp;'.$reason.'</div>', $description];
}
}
9 changes: 4 additions & 5 deletions classes/local/job/memberships_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,16 @@ public function run() {
$collection = response_processor::process($response);
if ($collection->count() > 0) {
foreach ($collection as $resource) {
$lockfactory = static::get_lock_factory();
$lock = $lockfactory->get_lock('Registration: ' .
$reglock = $lockfactory->get_lock('Registration: ' .
$resource->RegistrationID, self::TIME_LOCK_TIMEOUT);
if ($lock) {
if ($reglock) {
try{
$this->sync_resource($resource, $trace);
} catch (moodle_exception $exception) {
debugging($exception->getMessage(), DEBUG_DEVELOPER);
} finally {
$lock->release();
}
$reglock->release();
}
} else {
$trace->output('Lock timeout');
}
Expand Down
87 changes: 37 additions & 50 deletions classes/local/job/outcomes_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,38 +119,11 @@ public function can_run() {
$this->add_reasons(get_string('onlineactivityresultpushingdisabled', 'enrol_arlo'));
return false;
}
$maxpluginredirects = get_config('enrol_arlo','maxpluginredirects');
$this->trace->output('redirects are '.$pluginconfig->get('redirectcount'));

$this->trace->output('enabled is '.$pluginconfig->get('enablecommunication'));
if (get_config('enrol_arlo', 'redirectcount')>=$maxpluginredirects){
// Notify about failure
global $CFG, $SITE;
require_once($CFG->dirroot . '/enrol/arlo/locallib.php');
$redirectcount = $pluginconfig->get('redirectcount');
$this->trace->output('redirects are '. $redirectcount);
$this->trace->output('enabled is '. $pluginconfig->get('enablecommunication'));


$admins = get_admins();
$allVariables = get_defined_vars();

// Display the list
//$this->trace->output(json_encode($allVariables));
$manager = new \enrol_arlo\manager();
$manager->add_max_redirect_notification_to_queue();
foreach ($admins as $admin) {
//$this->trace->output(json_encode($admin));

$this->trace->output(sendfailurenotification($admin));
}

}
if (get_config('enrol_arlo', 'redirectcount')>=$maxpluginredirects && $pluginconfig->get('enablecommunication') == 1 ) {
$this->add_reasons(get_string('redirectcountmaxlimit', 'enrol_arlo'));
//sychronize the plugin config persistent settings with the current database values.
$pluginconfig->set('redirectcount', get_config('enrol_arlo','redirectcount'));
set_config('enablecommunication', 0, 'enrol_arlo');
$pluginconfig->set('enablecommunication', get_config('enrol_arlo','enablecommunication'));
return false;
}
if($pluginconfig->get('enablecommunication') == 0) {
$this->add_reasons(get_string('communication_disabled_message', 'enrol_arlo'));
return false;
Expand Down Expand Up @@ -201,19 +174,11 @@ public function run() {
$user = core_user::get_user($registrationpersistent->get('userid'));
$apiretryerrorpt1 = get_string('apiretryerrorpt1', 'enrol_arlo');
$apiretryerrorpt2 = get_string('apiretryerrorpt2', 'enrol_arlo');
$redirectcounter = $registrationpersistent->get('redirectcounter');
$maxredirects = $pluginconfig->get('retriesperrecord');
$retrylog = new retry_log_persistent();
$retrylog->set('timelogged', time());
$retrylog->set('userid', $user->id);
$retrylog->set('participantname', "$user->lastname, $user->firstname");
$retrylog->set('courseid', $course->id);
$retrylog->set('coursename', $course->fullname);
if ( $redirectcounter >= $maxredirects) {
$recordcounter = $registrationpersistent->get('redirectcounter');
$maxrecordretries = $pluginconfig->get('retriesperrecord');
if ($recordcounter >= $maxrecordretries) {
// Display retry error to admin on job page
$this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2");
// Create and save a log of the failure
$retrylog->save();
} else {
try {
if (!$user) {
Expand All @@ -226,19 +191,41 @@ public function run() {
if (!empty($data)) {
$this->trace->output(implode(',', $data));
external::patch_registration_resource($sourceregistration, $data);
// Check API status code. If it's a 3xx, increment the redirectcounter by 1
// Check API status code. If it's a 3xx, increment the registration and global retry counter.
// Now we count the errors 4xx and 5xx globally, but we don't increase the registration retry counter.
$apistatus = $pluginconfig->get('apistatus');
if ($apistatus >= 300 && $apistatus <= 399) {
$registrationpersistent->set('redirectcounter', ++$redirectcounter);
$pluginredirectcount = $pluginconfig->get('redirectcount');
$pluginconfig->set('redirectcount', ++$pluginredirectcount);
if ($apistatus >= 300 && $apistatus <= 599) {
$pluginredirectcount = $pluginconfig->get('redirectcount') + 1;
$pluginconfig->set('redirectcount', $pluginredirectcount);
$pluginmaxredirects = $pluginconfig->get('maxretries');
if ($apistatus <= 399) {
$recordcounter++;
$registrationpersistent->set('redirectcounter', $recordcounter);
if (!empty($maxrecordretries) && $recordcounter >= $maxrecordretries) {
// Display retry error to admin on job page
$this->trace->output("$apiretryerrorpt1 $user->id $apiretryerrorpt2");
// Create and save a log of the failure
$retrylog = new retry_log_persistent();
$retrylog->set('timelogged', time());
$retrylog->set('userid', $user->id);
$retrylog->set('participantname', "$user->lastname, $user->firstname");
$retrylog->set('courseid', $course->id);
$retrylog->set('coursename', $course->fullname);
$retrylog->save();
}
}
// We have reached the maximum number of errors allowed. Disable communication.
if (!empty($pluginmaxredirects) && $pluginredirectcount >= $pluginmaxredirects) {
$pluginconfig->set('enablecommunication', 0);
}
} else {
$registrationpersistent->set('redirectcounter', 0);
if ($recordcounter > 0) {
$registrationpersistent->set('redirectcounter', 0);
}
$pluginconfig->set('redirectcount', 0);
}
$apistatus == 200 ? $registrationpersistent->set('updatesource', 0) : null;
$registrationpersistent->set('timelastrequest', time());
// Reset update flag.
$registrationpersistent->set('updatesource', 0);
$retrylog->save();
$registrationpersistent->save();
}
} catch (Exception $exception) {
Expand Down
20 changes: 20 additions & 0 deletions classes/local/tablesql/apiretries.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function __construct($uniqueid) {
$headers[] = get_string('courseid', 'enrol_arlo');
$columns[] = 'coursename';
$headers[] = get_string('coursename', 'enrol_arlo');
$columns[] = 'action';
$headers[] = get_string('action');

$this->define_columns($columns);
$this->define_headers($headers);
$this->define_baseurl("/enrol/arlo/admin/apiretries.php");
Expand All @@ -58,4 +61,21 @@ public function __construct($uniqueid) {
public function col_timelogged($values) {
return userdate($values->timelogged);
}

public function col_action($values) {
global $DB, $OUTPUT;
$sql = "SELECT r.id, r.redirectcounter
FROM {enrol_arlo_registration} r
JOIN {enrol} e ON e.id = r.enrolid
WHERE r.userid = :userid
AND e.courseid = :courseid";
$params = ['userid' => $values->userid, 'courseid' => $values->courseid];
$record = $DB->get_record_sql($sql, $params);
$retry = get_string('retry_sync', 'enrol_arlo');
$moodeurl = new \moodle_url('/enrol/arlo/admin/apiretries.php', ['action' => 'resetredirects', 'regid' => $record->id]);
$maxretries = get_config('enrol_arlo', 'retriesperrecord');
$renderlink = !empty($record->redirectcounter) && ($record->redirectcounter >= $maxretries);
$output = $renderlink ? '<a href="' . $moodeurl . '">' . $retry . '</a>' : '';
return $output;
}
}
38 changes: 21 additions & 17 deletions classes/task/api_retry_notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace enrol_arlo\task;

use core\message\message;

use enrol_arlo\api;

/**
* Arlo Retry Log Monitor Task
Expand Down Expand Up @@ -50,25 +50,29 @@ public function execute() {
global $CFG;
require_once($CFG->dirroot . '/enrol/arlo/locallib.php');
$newentries = check_arlo_api_retry_log();

$admins = get_admins();
if (!empty($newentries)) {
// Notify all Moodle administrators about the new entries.
$admins = get_admins();
$apiretrylogurl = new \moodle_url('/enrol/arlo/admin/apiretries.php');
$manager = new \enrol_arlo\manager();
$manager->add_max_redirect_notification_to_queue();
$message = new message();
$message->component = 'enrol_arlo';
$message->name = 'arlo_retry_log_notification';
$message->userfrom = \core_user::get_noreply_user();
$message->subject = get_string('arlo_retry_log_subject', 'enrol_arlo');
$message->fullmessage = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out());
$message->fullmessageformat = FORMAT_PLAIN;
$message->fullmessagehtml = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out());
foreach ($admins as $admin) {
$message->userto = $admin;
message_send($message);
sendfailurenotification($admin);
$plugin = api::get_enrolment_plugin();
$pluginconfig = $plugin->get_plugin_config();
if ($pluginconfig->get('enablecommunication') == 0) {
sendfailurenotification($admin);
} else {
$apiretrylogurl = new \moodle_url('/enrol/arlo/admin/apiretries.php');
$manager = new \enrol_arlo\manager();
$manager->add_max_redirect_notification_to_queue();
$message = new message();
$message->component = 'enrol_arlo';
$message->name = 'arlo_retry_log_notification';
$message->userfrom = \core_user::get_noreply_user();
$message->subject = get_string('arlo_retry_log_subject', 'enrol_arlo');
$message->fullmessage = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out());
$message->fullmessageformat = FORMAT_PLAIN;
$message->fullmessagehtml = get_string('arlo_retry_log_message', 'enrol_arlo', $apiretrylogurl->out());
$message->userto = $admin;
message_send($message);
}
}
}
}
Expand Down
Loading

0 comments on commit dda54e4

Please sign in to comment.