Skip to content

Commit

Permalink
Added extra context logs
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Jan 2, 2025
1 parent f2b36dd commit e5b7b7f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
39 changes: 39 additions & 0 deletions classes/clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,43 @@ protected static function execute_sql($sql) {
public static function get_settings_section_url($sectionname) {
return new \moodle_url('/admin/settings.php', array('section' => $sectionname));
}

/**
* Log some context of where and why this was run.
*/
public static function debug_info() {
global $CFG;

$context = "Time: " . \userdate(time()) . "\n";
$context .= "TZ: " . $CFG->timezone . "\n";
$context .= "Host: " . gethostname() . "\n";
$context .= "Moodle User: " . $USER->username . "\n";
$context .= "\$CFG->dbhost: " . $CFG->dbhost . "\n";
$context .= "\$CFG->dbuser: " . $CFG->dbuser . "\n";
$context .= "\$CFG->dataroot: " . $CFG->dataroot . "\n";
$context .= "\$CFG->wwwroot: " . $CFG->wwwroot . "\n";
$context .= "\$CFG->original_wwwroot: " . $CFG->original_wwwroot . "\n";

self::log($context);

// Also set this in the DB.
set_config('lastwash', $context, 'local_datacleaner');

}

/**
* Log details in a variety of places
*
* @param string
*/
public static function log($string) {
global $CFG;

// Send it to stderr.
error_log($string);

// Stash a copy into sitedir log file.
mkdir("$CFG->dataroot/datacleaner");
file_put_contents("$CFG->dataroot/datacleaner/clean.log", $string, FILE_APPEND);
}
}
18 changes: 11 additions & 7 deletions cli/clean.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
require_once($CFG->libdir.'/adminlib.php');
require_once(dirname(__FILE__) . '/lib.php');

use local_datacleaner\clean;

// Now get cli options.
list($options, $unrecognized) = cli_get_params(
array(
Expand Down Expand Up @@ -102,16 +104,18 @@
}

if ($options['dryrun']) {
echo "=== DRY RUN ===\n";
clean::log("=== DRY RUN ===\n");
}

$filter = $options['filter'];
if ($filter) {
echo "Filtering to ONLY run: $filter \n";
clean::log("Filtering to ONLY run: $filter \n");
}

$cascade = null;

local_datacleaner\clean::debug_info();

foreach ($plugins as $plugin) {
// Get the class that does the work.
$classname = 'cleaner_' . $plugin->name . '\clean';
Expand All @@ -127,7 +131,7 @@
// Skip subplugins that have a sort order that is greater or equal to 200.
if ($options['run-pre-wash']) {
if ($plugin->sortorder >= 200) {
echo "NOTICE: Pre washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n";
clean::log("NOTICE: Pre washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n");
continue;
}
}
Expand All @@ -136,18 +140,18 @@
// Skip subplugins that have a sort order that is less than 200.
if ($options['run-post-wash']) {
if ($plugin->sortorder < 200) {
echo "NOTICE: Post washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n";
clean::log("NOTICE: Post washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n");
continue;
}
}

echo "== Running {$plugin->name} cleaner ==\n";
if (!class_exists($classname)) {
echo "ERROR: Unable to locate local/datacleaner/cleaner/{$plugin->name}/classes/clean.php class. Skipping.\n";
clean::log("ERROR: Unable to locate local/datacleaner/cleaner/{$plugin->name}/classes/clean.php class. Skipping.\n");
continue;
}

$class = new $classname($options);
clean::log("== Running {$plugin->name} cleaner ==\n");

if (is_null($cascade) && $class->needs_cascade_delete()) {
$cascade = new \local_datacleaner\schema_add_cascade_delete($options);
Expand All @@ -160,4 +164,4 @@
$class->execute();
}

echo "Done.\n";
clean::log("Done.\n");
3 changes: 3 additions & 0 deletions cli/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

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

use local_datacleaner\clean;

/**
* Print a message to the terminal.
*
Expand All @@ -38,6 +40,7 @@ function print_message($text, $highlight = false) {
} else {
echo $text;
}
clean::log($text);
}

/**
Expand Down

0 comments on commit e5b7b7f

Please sign in to comment.