Skip to content

Commit

Permalink
Merge branch 'MDL-69615-main' of https://github.com/HuongNV13/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyatregubov committed Jan 16, 2024
2 parents d3b8694 + 028c038 commit 75c5854
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 19 deletions.
16 changes: 3 additions & 13 deletions admin/tool/dataprivacy/tests/task/task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace tool_dataprivacy\task;

use core\task\task_trait;
use tool_dataprivacy\api;

defined('MOODLE_INTERNAL') || die();
Expand All @@ -30,6 +31,8 @@
*/
class task_test extends \data_privacy_testcase {

use task_trait;

/**
* Test tearDown.
*/
Expand Down Expand Up @@ -215,17 +218,4 @@ public function test_delete_existing_deleted_users_task_existing_finished_delete
$this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id,
[api::DATAREQUEST_STATUS_CANCELLED], [api::DATAREQUEST_TYPE_DELETE]));
}

/**
* Helper to execute a particular task.
*
* @param string $task The task.
*/
private function execute_task($task) {
// Run the scheduled task.
ob_start();
$task = \core\task\manager::get_scheduled_task($task);
$task->execute();
ob_end_clean();
}
}
27 changes: 22 additions & 5 deletions backup/util/helper/backup_cron_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL
$rs->close();

// Send email to admin if necessary.
if ($emailpending) {
self::send_backup_status_to_admin($admin);
}
set_config(
'backup_auto_emailpending',
$emailpending ? 1 : 0,
'backup',
);
} finally {
// Everything is finished release lock.
$lock->release();
Expand Down Expand Up @@ -188,7 +190,7 @@ public static function get_backup_status_array() {
* @param stdClass $admin
* @return array
*/
private static function send_backup_status_to_admin($admin) {
public static function send_backup_status_to_admin($admin) {
global $DB, $CFG;

mtrace("Sending email to admin");
Expand Down Expand Up @@ -386,7 +388,22 @@ private static function push_course_backup_adhoc_task($backupcourse, $admin) {
'courseid' => $backupcourse->courseid,
'adminid' => $admin->id
));
\core\task\manager::queue_adhoc_task($asynctask);
$taskid = \core\task\manager::queue_adhoc_task($asynctask);

// Get the queued tasks.
$queuedtasks = [];
if ($value = get_config('backup', 'backup_auto_adhoctasks')) {
$queuedtasks = explode(',', $value);
}
if ($taskid) {
$queuedtasks[] = (int) $taskid;
}
// Save the queued tasks.
set_config(
'backup_auto_adhoctasks',
implode(',', $queuedtasks),
'backup',
);

$backupcourse->laststatus = self::BACKUP_STATUS_QUEUED;
$DB->update_record('backup_courses', $backupcourse);
Expand Down
1 change: 1 addition & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,7 @@
$string['taskadmintitle'] = 'Tasks';
$string['taskanalyticscleanup'] = 'Analytics cleanup';
$string['taskautomatedbackup'] = 'Automated backups';
$string['taskautomatedbackup_report'] = 'Automated backups report';
$string['taskbackupcleanup'] = 'Clean backup tables, logs and files';
$string['taskbadgesadhoc'] = 'Award badge';
$string['taskbadgescron'] = 'Add award badges adhoc tasks';
Expand Down
86 changes: 86 additions & 0 deletions lib/classes/task/automated_backup_report_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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/>.

namespace core\task;

/**
* Report task for core automation backup.
*
* @package core
* @copyright 2024 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class automated_backup_report_task extends scheduled_task {

/**
* Get a descriptive name for the task (shown to admins).
*
* @return string
*/
public function get_name(): string {
return get_string('taskautomatedbackup_report', 'admin');
}

/**
* Do the job.
*/
public function execute(): void {
global $DB, $CFG;
$queuedtasks = [];
if ($value = get_config('backup', 'backup_auto_adhoctasks')) {
$queuedtasks = explode(',', $value);
}
if (!empty($queuedtasks)) {
// Some automated backup tasks are still running.
// Check the status for each task.
foreach ($queuedtasks as $taskid) {
if (!$DB->record_exists('task_adhoc', ['id' => $taskid])) {
// The task has been completed. Remove it from the queue.
if (($key = array_search($taskid, $queuedtasks)) !== false) {
unset($queuedtasks[$key]);
}
}
}
// Update the queue.
set_config(
'backup_auto_adhoctasks',
implode(',', $queuedtasks),
'backup',
);
}
if (empty($queuedtasks) && get_config('backup', 'backup_auto_emailpending')) {
// All the automated backup tasks have been completed. Send the report.
$admin = get_admin();
if (!$admin) {
mtrace("Error: No admin account was found");
return;
}
// Send email to admin if necessary.
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php');
\backup_cron_automated_helper::send_backup_status_to_admin($admin);
// Remove the configs.
unset_config(
'backup_auto_adhoctasks',
'backup',
);
unset_config(
'backup_auto_emailpending',
'backup',
);
}
}
}
59 changes: 59 additions & 0 deletions lib/classes/task/task_trait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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/>.

namespace core\task;

/**
* This file defines a trait to assist with unit tests in tasks.
*
* @package core
* @copyright 2024 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait task_trait {

/**
* Helper to execute a particular task.
*
* @param string $taskclass The task class.
*/
protected function execute_task(string $taskclass): void {
// Run the scheduled task.
$this->start_output_buffering();
$task = manager::get_scheduled_task($taskclass);
$task->execute();
$this->stop_output_buffering();
}

/**
* Helper to start output buffering.
*/
protected function start_output_buffering(): void {
ob_start();
}

/**
* Helper to stop output buffering.
*
* @return string|null The output buffer contents or null if output buffering is not active.
*/
protected function stop_output_buffering(): ?string {
$output = ob_get_contents();
ob_end_clean();

return $output;
}
}
9 changes: 9 additions & 0 deletions lib/db/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,13 @@
'dayofweek' => 'R',
'month' => '*',
],
[
'classname' => 'core\task\automated_backup_report_task',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
],
);
Loading

0 comments on commit 75c5854

Please sign in to comment.