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 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'modules/gallery/controllers') 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 -- cgit v1.2.3 From 493fb27825327e14168c3c0b133c338a53457e0e Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 6 Jul 2009 22:20:04 -0700 Subject: Cleanup remove task processing. Change Task_Model::delete to call parent::delete instead of parent::save. :-) Also change admin_maintenance_controller to delete via looping over all of the completed tasks and delete individually, so we can delete the associated cache entries at the same time. --- modules/gallery/controllers/admin_maintenance.php | 9 ++++++++- modules/gallery/models/task.php | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'modules/gallery/controllers') diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php index a65bd770..37cc5222 100644 --- a/modules/gallery/controllers/admin_maintenance.php +++ b/modules/gallery/controllers/admin_maintenance.php @@ -161,7 +161,14 @@ class Admin_Maintenance_Controller extends Admin_Controller { public function remove_finished_tasks() { access::verify_csrf(); - Database::instance()->delete("tasks", array("done" => 1)); + + // Do it the long way so we can call delete and remove the cache. + $finished = ORM::factory("task") + ->where(array("done" => 1)) + ->find_all(); + foreach ($finished as $task) { + task::remove($task->id); + } message::success(t("All finished tasks removed")); url::redirect("admin/maintenance"); } diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php index 2e77a7a6..012b88cf 100644 --- a/modules/gallery/models/task.php +++ b/modules/gallery/models/task.php @@ -42,7 +42,7 @@ class Task_Model extends ORM { public function delete() { Cache::instance()->delete($this->_cache_key()); - return parent::save(); + return parent::delete(); } public function owner() { -- cgit v1.2.3 From 5d9e334fe6d7c7c1cf6d39a12684e215e797b4b3 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 7 Jul 2009 12:49:21 -0700 Subject: Added a wee bit of phpDoc. Changed the name of the method get_task_log to get_log Changed the default name of the file when the log is downloaded to gallery3_task_log.txt --- modules/gallery/controllers/admin_maintenance.php | 4 ++-- modules/gallery/models/task.php | 14 +++++++++++++- modules/gallery/views/admin_maintenance.html.php | 2 +- modules/gallery/views/admin_maintenance_show_log.html.php | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) (limited to 'modules/gallery/controllers') diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php index 37cc5222..543961a1 100644 --- a/modules/gallery/controllers/admin_maintenance.php +++ b/modules/gallery/controllers/admin_maintenance.php @@ -119,8 +119,8 @@ class Admin_Maintenance_Controller extends Admin_Controller { } header("Content-Type: application/text"); - header("Content-Disposition: filename=g2_import.txt"); - print $task->get_task_log(); + header("Content-Disposition: filename=gallery3_task_log.txt"); + print $task->get_log(); } /** diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php index 55bb8e21..b7e255a2 100644 --- a/modules/gallery/models/task.php +++ b/modules/gallery/models/task.php @@ -49,6 +49,10 @@ class Task_Model extends ORM { return user::lookup($this->owner_id); } + /** + * Log a message to the task log. + * @params $msg mixed a string or array of strings + */ public function log($msg) { $key = $this->_cache_key(); $log = Cache::instance()->get($key); @@ -63,11 +67,19 @@ class Task_Model extends ORM { array("task", "log", "import"), 2592000); } - public function get_task_log() { + /** + * Retrieve the cached log information for this task. + * @returns the log data or null if there is no log data + */ + public function get_log() { $log_data = Cache::instance()->get($this->_cache_key()); return $log_data !== null ? $log_data : false; } + /** + * Build the task cache key + * @returns the key to use in access the cache + */ private function _cache_key() { return md5("$this->id; $this->name; $this->callback"); } diff --git a/modules/gallery/views/admin_maintenance.html.php b/modules/gallery/views/admin_maintenance.html.php index f50c95dd..eecc045c 100644 --- a/modules/gallery/views/admin_maintenance.html.php +++ b/modules/gallery/views/admin_maintenance.html.php @@ -164,7 +164,7 @@ id?csrf=$csrf") ?>"> - get_task_log()): ?> + get_log()): ?> 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 index afd988bb..9d850986 100644 --- a/modules/gallery/views/admin_maintenance_show_log.html.php +++ b/modules/gallery/views/admin_maintenance_show_log.html.php @@ -12,7 +12,7 @@ appendTo('body').submit().remove();

name ?>

-
get_task_log()) ?>
+
get_log()) ?>
-- cgit v1.2.3