From 04f02b49c5d524345bc916aad6e247c714177662 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 5 Jul 2009 17:38:49 -0700 Subject: 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. --- modules/gallery/controllers/admin_maintenance.php | 38 ++++++++++++++++++++++ modules/gallery/helpers/task.php | 2 ++ modules/gallery/models/task.php | 24 ++++++++++++++ modules/gallery/views/admin_maintenance.html.php | 5 +++ .../views/admin_maintenance_show_log.html.php | 19 +++++++++++ 5 files changed, 88 insertions(+) create mode 100644 modules/gallery/views/admin_maintenance_show_log.html.php (limited to 'modules') 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,12 +81,48 @@ 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"))); print $view; } + /** + * 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 @@ id?csrf=$csrf") ?>"> + get_task_log()): ?> + id?csrf=$csrf") ?>"> + + + id?csrf=$csrf") ?>"> 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 @@ + + +
+

name ?>

+
+
get_task_log()) ?>
+
+ + +
-- cgit v1.2.3 From f381927ec6bca75e9c588725318f6682bf74d103 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 6 Jul 2009 07:39:36 -0700 Subject: Catch exceptions that are thrown within the task and log them to the task log and then set the task to done, the state to error and the status message set to the Exception Message. --- modules/gallery/helpers/task.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'modules') diff --git a/modules/gallery/helpers/task.php b/modules/gallery/helpers/task.php index 79a7c93b..06568887 100644 --- a/modules/gallery/helpers/task.php +++ b/modules/gallery/helpers/task.php @@ -76,9 +76,17 @@ class task_Core { throw new Exception("@todo MISSING_TASK"); } - $task->state = "running"; - call_user_func_array($task->callback, array(&$task)); - $task->save(); + try { + $task->state = "running"; + call_user_func_array($task->callback, array(&$task)); + $task->save(); + } catch (Exception $e) { + $task->log($e->__toString()); + $task->state = "error"; + $task->done = true; + $task->status = $e->getMessage(); + $task->save(); + } return $task; } -- cgit v1.2.3 From 6dbd36d1ccc482ff542884da0cdc60612436c002 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 6 Jul 2009 10:29:55 -0700 Subject: Always log the task completion status message --- modules/gallery/helpers/task.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'modules') diff --git a/modules/gallery/helpers/task.php b/modules/gallery/helpers/task.php index 06568887..6a9f63c2 100644 --- a/modules/gallery/helpers/task.php +++ b/modules/gallery/helpers/task.php @@ -79,6 +79,9 @@ class task_Core { try { $task->state = "running"; call_user_func_array($task->callback, array(&$task)); + if ($task->done) { + $task->log($task->status); + } $task->save(); } catch (Exception $e) { $task->log($e->__toString()); -- cgit v1.2.3 From 30aa065013507c18ed64cc189e60e0959b7c4d7d Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 6 Jul 2009 12:41:54 -0700 Subject: Don't use jQuery in the error page, it interferes with the jquery lib in the outer page when we load it in a dialog context. --- modules/gallery/views/kohana_error_page.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'modules') diff --git a/modules/gallery/views/kohana_error_page.php b/modules/gallery/views/kohana_error_page.php index 6bf48549..9361514d 100644 --- a/modules/gallery/views/kohana_error_page.php +++ b/modules/gallery/views/kohana_error_page.php @@ -53,7 +53,6 @@ margin: 0px; } - <?= t("Something went wrong!") ?> @@ -78,8 +77,13 @@

-
+ +