diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-07-05 17:38:49 -0700 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-07-05 17:38:49 -0700 |
commit | 04f02b49c5d524345bc916aad6e247c714177662 (patch) | |
tree | 2410e3ca63a67511c8dbdfd3a8f20a174492a9e5 | |
parent | fcb031c2b60ac9e1888592296cba25791e77b446 (diff) |
Add task logging functionality. When a task runs, it creates a log that is
stored in the persistant cache for 30 days. On the admin_maintenance page
there is a new link for completed tasks "browse log". Clicking this will
open a dialog box that has the the contents of the log displayed.
The user can then view the log and close the dialog, or press the save button
to download the log to their local machine.
-rw-r--r-- | modules/gallery/controllers/admin_maintenance.php | 38 | ||||
-rw-r--r-- | modules/gallery/helpers/task.php | 2 | ||||
-rw-r--r-- | modules/gallery/models/task.php | 24 | ||||
-rw-r--r-- | modules/gallery/views/admin_maintenance.html.php | 5 | ||||
-rw-r--r-- | modules/gallery/views/admin_maintenance_show_log.html.php | 19 | ||||
-rw-r--r-- | themes/admin_default/css/screen.css | 14 |
6 files changed, 102 insertions, 0 deletions
diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php index 7c5934a3..a65bd770 100644 --- a/modules/gallery/controllers/admin_maintenance.php +++ b/modules/gallery/controllers/admin_maintenance.php @@ -59,6 +59,8 @@ class Admin_Maintenance_Controller extends Admin_Controller { $view = new View("admin_maintenance_task.html"); $view->task = $task; + $task->log(t("Task %task_name started (task id %task_id)", + array("task_name" => $task->name, "task_id" => $task->id))); log::info("tasks", t("Task %task_name started (task id %task_id)", array("task_name" => $task->name, "task_id" => $task->id)), html::anchor("admin/maintenance", t("maintenance"))); @@ -79,6 +81,8 @@ class Admin_Maintenance_Controller extends Admin_Controller { $view = new View("admin_maintenance_task.html"); $view->task = $task; + $task->log(t("Task %task_name resumed (task id %task_id)", + array("task_name" => $task->name, "task_id" => $task->id))); log::info("tasks", t("Task %task_name resumed (task id %task_id)", array("task_name" => $task->name, "task_id" => $task->id)), html::anchor("admin/maintenance", t("maintenance"))); @@ -86,6 +90,40 @@ class Admin_Maintenance_Controller extends Admin_Controller { } /** + * Show the task log + * @param string $task_id + */ + public function show_log($task_id) { + access::verify_csrf(); + + $task = ORM::factory("task", $task_id); + if (!$task->loaded) { + throw new Exception("@todo MISSING_TASK"); + } + $view = new View("admin_maintenance_show_log.html"); + $view->task = $task; + + print $view; + } + + /** + * Save the task log + * @param string $task_id + */ + public function save_log($task_id) { + access::verify_csrf(); + + $task = ORM::factory("task", $task_id); + if (!$task->loaded) { + throw new Exception("@todo MISSING_TASK"); + } + + header("Content-Type: application/text"); + header("Content-Disposition: filename=g2_import.txt"); + print $task->get_task_log(); + } + + /** * Cancel a task. * @param string $task_id */ diff --git a/modules/gallery/helpers/task.php b/modules/gallery/helpers/task.php index a8a004ab..79a7c93b 100644 --- a/modules/gallery/helpers/task.php +++ b/modules/gallery/helpers/task.php @@ -56,6 +56,8 @@ class task_Core { } $task->done = 1; $task->state = "cancelled"; + $task->log(t("Task %task_name cancelled (task id %task_id)", + array("task_name" => $task->name, "task_id" => $task->id))); $task->save(); return $task; diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php index 9e3ae5c6..2e77a7a6 100644 --- a/modules/gallery/models/task.php +++ b/modules/gallery/models/task.php @@ -40,7 +40,31 @@ class Task_Model extends ORM { return parent::save(); } + public function delete() { + Cache::instance()->delete($this->_cache_key()); + return parent::save(); + } + public function owner() { return user::lookup($this->owner_id); } + + public function log($msg) { + $key = $this->_cache_key(); + $log = Cache::instance()->get($key); + + // Save for 30 days. + $log .= !empty($log) ? "\n" : ""; + Cache::instance()->set($key, "$log{$msg}", + array("task", "log", "import"), 2592000); + } + + public function get_task_log() { + $log_data = Cache::instance()->get($this->_cache_key()); + return $log_data !== null ? $log_data : false; + } + + private function _cache_key() { + return md5("$this->id; $this->name; $this->callback"); + } }
\ No newline at end of file diff --git a/modules/gallery/views/admin_maintenance.html.php b/modules/gallery/views/admin_maintenance.html.php index c47f77f8..f50c95dd 100644 --- a/modules/gallery/views/admin_maintenance.html.php +++ b/modules/gallery/views/admin_maintenance.html.php @@ -164,6 +164,11 @@ <a href="<?= url::site("admin/maintenance/remove/$task->id?csrf=$csrf") ?>"> <?= t("remove") ?> </a> + <? if ($task->get_task_log()): ?> + <a class="gDialogLink" href="<?= url::site("admin/maintenance/show_log/$task->id?csrf=$csrf") ?>"> + <?= t("browse log") ?> + </a> + <? endif ?> <? else: ?> <a class="gDialogLink" href="<?= url::site("admin/maintenance/resume/$task->id?csrf=$csrf") ?>"> <?= t("resume") ?> diff --git a/modules/gallery/views/admin_maintenance_show_log.html.php b/modules/gallery/views/admin_maintenance_show_log.html.php new file mode 100644 index 00000000..afd988bb --- /dev/null +++ b/modules/gallery/views/admin_maintenance_show_log.html.php @@ -0,0 +1,19 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<script type="text/javascript"> + dismiss = function() { + window.location.reload(); + } + download = function(){ + // send request + $('<form action="<?= url::site("admin/maintenance/save_log/$task->id?csrf=$csrf") ?>" method="post"></form>'). +appendTo('body').submit().remove(); + }; +</script> +<div id="gTaskLogDialog"> + <h1> <?= $task->name ?> </h1> + <div class="gTaskLog"> + <pre><?= p::purify($task->get_task_log()) ?></pre> + </div> + <button id="gCloseButton" class="ui-state-default ui-corner-all" onclick="dismiss()"><?= t("Close") ?></button> + <button id="gSaveButton" class="ui-state-default ui-corner-all" onclick="download()"><?= t("Save") ?></button> +</div> diff --git a/themes/admin_default/css/screen.css b/themes/admin_default/css/screen.css index e68620a5..d408acf0 100644 --- a/themes/admin_default/css/screen.css +++ b/themes/admin_default/css/screen.css @@ -400,6 +400,20 @@ li.gDefaultGroup h4, li.gDefaultGroup .gUser { float: left; } +#gTaskLogDialog h1 { + font-size: 1.1em; +} + +.gTaskLog { + border: 1pt solid; + font-size: .9em; + height: 400px; + margin: .5em 0; + overflow: auto; + padding: .5em +} + + /** ******************************************************************* * 7) Server Add *********************************************************************/ |