From dc4f784558db725eb555ce9668231b89aabb8954 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 27 Feb 2009 16:28:20 +0000 Subject: * Refactor task management methods from admin_maintenance.php to task.php * Added a owner_id field to the task database * Modified the admin maintenace to show the owner of the task <<**** Requires a reinstallation of core ****>> --- core/helpers/task.php | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 core/helpers/task.php (limited to 'core/helpers/task.php') diff --git a/core/helpers/task.php b/core/helpers/task.php new file mode 100644 index 00000000..816a5288 --- /dev/null +++ b/core/helpers/task.php @@ -0,0 +1,101 @@ + $module_info) { + $class_name = "{$module_name}_task"; + if (method_exists($class_name, "available_tasks")) { + foreach (call_user_func(array($class_name, "available_tasks")) as $task) { + if (in_array($task->type, $type)) { + $tasks[$task->callback] = $task; + } + } + } + } + + return $tasks; + } + + static function create($task_callback) { + $task_definitions = self::get_task_definitions(array("admin", "general", "both")); + + $task = ORM::factory("task"); + $task->callback = $task_callback; + $task->name = $task_definitions[$task_callback]->name; + $task->percent_complete = 0; + $task->status = ""; + $task->state = "started"; + $task->owner_id = user::active()->id; + $task->context = serialize(array()); + $task->save(); + + return $task; + } + + static function cancel($task_id) { + $task = ORM::factory("task", $task_id); + if (!$task->loaded) { + throw new Exception("@todo MISSING_TASK"); + } + $task->done = 1; + $task->state = "cancelled"; + $task->save(); + + return $task; + } + + static function remove($task_id) { + $task = ORM::factory("task", $task_id); + if ($task->loaded) { + $task->delete(); + } + } + + static function run($task_id) { + $task = ORM::factory("task", $task_id); + if (!$task->loaded) { + throw new Exception("@todo MISSING_TASK"); + } + + $task->state = "running"; + call_user_func_array($task->callback, array(&$task)); + $task->save(); + + return $task; + } + + static function success($task, $location=null) { + $result = array("result" => "success", "task" => $task->as_array()); + if (!empty($location)) { + $result["location"] = $location; + } + return json_encode($result); + } + + static function in_progress($task) { + return json_encode( + array("result" => "in_progress", + "task" => $task->as_array())); + } +} \ No newline at end of file -- cgit v1.2.3