Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extra context logs #183

Open
wants to merge 1 commit into
base: MOODLE_311_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading