2009-02-27 16:28:20 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2013-01-21 01:22:01 -05:00
|
|
|
* Copyright (C) 2000-2013 Bharat Mediratta
|
2009-02-27 16:28:20 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
class task_Core {
|
|
|
|
|
/**
|
|
|
|
|
* Get all available tasks
|
|
|
|
|
*/
|
2009-03-09 07:02:09 +00:00
|
|
|
static function get_definitions() {
|
2009-02-27 16:28:20 +00:00
|
|
|
$tasks = array();
|
2009-05-26 05:28:59 +00:00
|
|
|
foreach (module::active() as $module) {
|
|
|
|
|
$class_name = "{$module->name}_task";
|
2009-02-27 16:28:20 +00:00
|
|
|
if (method_exists($class_name, "available_tasks")) {
|
|
|
|
|
foreach (call_user_func(array($class_name, "available_tasks")) as $task) {
|
2009-03-09 07:02:09 +00:00
|
|
|
$tasks[$task->callback] = $task;
|
2009-02-27 16:28:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $tasks;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-01 16:31:24 -08:00
|
|
|
static function start($task_callback, $context=array()) {
|
|
|
|
|
$tasks = task::get_definitions();
|
|
|
|
|
$task = task::create($tasks[$task_callback], array());
|
|
|
|
|
|
|
|
|
|
$task->log(t("Task %task_name started (task id %task_id)",
|
|
|
|
|
array("task_name" => $task->name, "task_id" => $task->id)));
|
|
|
|
|
return $task;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-11 03:29:12 +00:00
|
|
|
static function create($task_def, $context) {
|
2009-02-27 16:28:20 +00:00
|
|
|
$task = ORM::factory("task");
|
2009-03-11 03:29:12 +00:00
|
|
|
$task->callback = $task_def->callback;
|
|
|
|
|
$task->name = $task_def->name;
|
2009-02-27 16:28:20 +00:00
|
|
|
$task->percent_complete = 0;
|
|
|
|
|
$task->status = "";
|
|
|
|
|
$task->state = "started";
|
2009-10-22 13:09:20 -07:00
|
|
|
$task->owner_id = identity::active_user()->id;
|
2009-03-10 13:53:59 +00:00
|
|
|
$task->context = serialize($context);
|
2009-02-27 16:28:20 +00:00
|
|
|
$task->save();
|
|
|
|
|
|
|
|
|
|
return $task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function cancel($task_id) {
|
|
|
|
|
$task = ORM::factory("task", $task_id);
|
2009-11-25 13:22:24 -08:00
|
|
|
if (!$task->loaded()) {
|
2009-02-27 16:28:20 +00:00
|
|
|
throw new Exception("@todo MISSING_TASK");
|
|
|
|
|
}
|
|
|
|
|
$task->done = 1;
|
|
|
|
|
$task->state = "cancelled";
|
2009-07-05 17:38:49 -07:00
|
|
|
$task->log(t("Task %task_name cancelled (task id %task_id)",
|
|
|
|
|
array("task_name" => $task->name, "task_id" => $task->id)));
|
2009-02-27 16:28:20 +00:00
|
|
|
$task->save();
|
|
|
|
|
|
|
|
|
|
return $task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function remove($task_id) {
|
|
|
|
|
$task = ORM::factory("task", $task_id);
|
2009-11-25 13:22:24 -08:00
|
|
|
if ($task->loaded()) {
|
2009-02-27 16:28:20 +00:00
|
|
|
$task->delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function run($task_id) {
|
|
|
|
|
$task = ORM::factory("task", $task_id);
|
2009-11-25 13:22:24 -08:00
|
|
|
if (!$task->loaded()) {
|
2009-02-27 16:28:20 +00:00
|
|
|
throw new Exception("@todo MISSING_TASK");
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-06 07:39:36 -07:00
|
|
|
try {
|
|
|
|
|
$task->state = "running";
|
|
|
|
|
call_user_func_array($task->callback, array(&$task));
|
2009-07-06 10:29:55 -07:00
|
|
|
if ($task->done) {
|
|
|
|
|
$task->log($task->status);
|
|
|
|
|
}
|
2009-07-06 07:39:36 -07:00
|
|
|
$task->save();
|
|
|
|
|
} catch (Exception $e) {
|
2010-01-27 21:58:06 -08:00
|
|
|
Kohana_Log::add("error", (string)$e);
|
2010-01-24 11:40:01 -08:00
|
|
|
|
|
|
|
|
// Ugh. I hate to use instanceof, But this beats catching the exception separately since
|
|
|
|
|
// we mostly want to treat it the same way as all other exceptions
|
|
|
|
|
if ($e instanceof ORM_Validation_Exception) {
|
|
|
|
|
Kohana_Log::add("error", "Validation errors: " . print_r($e->validation->errors(), 1));
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-27 21:58:06 -08:00
|
|
|
$task->log((string)$e);
|
2009-07-06 07:39:36 -07:00
|
|
|
$task->state = "error";
|
|
|
|
|
$task->done = true;
|
2009-07-20 20:47:47 -07:00
|
|
|
$task->status = substr($e->getMessage(), 0, 255);
|
2009-07-06 07:39:36 -07:00
|
|
|
$task->save();
|
|
|
|
|
}
|
2009-02-27 16:28:20 +00:00
|
|
|
|
|
|
|
|
return $task;
|
|
|
|
|
}
|
|
|
|
|
}
|