2008-12-28 10:12:41 +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
|
2008-12-28 10:12:41 +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 Admin_Maintenance_Controller extends Admin_Controller {
|
2008-12-28 23:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Show a list of all available, running and finished tasks.
|
|
|
|
|
*/
|
|
|
|
|
public function index() {
|
2009-12-02 00:34:34 -08:00
|
|
|
$query = db::build()
|
|
|
|
|
->update("tasks")
|
|
|
|
|
->set("state", "stalled")
|
|
|
|
|
->where("done", "=", 0)
|
|
|
|
|
->where("state", "<>", "stalled")
|
2010-12-28 23:10:05 -08:00
|
|
|
->where(db::expr("UNIX_TIMESTAMP(NOW()) - `updated` > 15"))
|
2009-12-02 00:34:34 -08:00
|
|
|
->execute();
|
2008-12-28 23:48:15 +00:00
|
|
|
$stalled_count = $query->count();
|
|
|
|
|
if ($stalled_count) {
|
|
|
|
|
log::warning("tasks",
|
2009-01-15 09:30:15 +00:00
|
|
|
t2("One task is stalled",
|
2009-01-15 10:02:41 +00:00
|
|
|
"%count tasks are stalled",
|
2009-01-15 09:30:15 +00:00
|
|
|
$stalled_count),
|
2009-03-21 07:44:46 +00:00
|
|
|
t('<a href="%url">view</a>',
|
2009-08-31 02:12:01 -07:00
|
|
|
array("url" => html::mark_clean(url::site("admin/maintenance")))));
|
2008-12-28 23:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$view = new Admin_View("admin.html");
|
2010-02-28 13:35:58 -08:00
|
|
|
$view->page_title = t("Maintenance tasks");
|
2008-12-28 10:12:41 +00:00
|
|
|
$view->content = new View("admin_maintenance.html");
|
2009-03-11 02:39:52 +00:00
|
|
|
$view->content->task_definitions = task::get_definitions();
|
2009-02-27 16:28:20 +00:00
|
|
|
$view->content->running_tasks = ORM::factory("task")
|
2009-11-26 12:09:04 -08:00
|
|
|
->where("done", "=", 0)->order_by("updated", "DESC")->find_all();
|
2009-02-27 16:28:20 +00:00
|
|
|
$view->content->finished_tasks = ORM::factory("task")
|
2009-11-26 12:09:04 -08:00
|
|
|
->where("done", "=", 1)->order_by("updated", "DESC")->find_all();
|
2008-12-28 10:12:41 +00:00
|
|
|
print $view;
|
2010-09-18 17:46:28 -07:00
|
|
|
|
|
|
|
|
// Do some maintenance while we're in here
|
|
|
|
|
db::build()
|
|
|
|
|
->delete("caches")
|
|
|
|
|
->where("expiration", "<>", 0)
|
|
|
|
|
->where("expiration", "<=", time())
|
|
|
|
|
->execute();
|
2008-12-28 10:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-28 23:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Start a new task
|
|
|
|
|
* @param string $task_callback
|
|
|
|
|
*/
|
|
|
|
|
public function start($task_callback) {
|
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
2010-02-01 16:31:24 -08:00
|
|
|
$task = task::start($task_callback);
|
2008-12-28 10:12:41 +00:00
|
|
|
$view = new View("admin_maintenance_task.html");
|
|
|
|
|
$view->task = $task;
|
2008-12-28 23:48:15 +00:00
|
|
|
|
2009-01-15 10:02:41 +00:00
|
|
|
log::info("tasks", t("Task %task_name started (task id %task_id)",
|
2009-01-08 17:13:06 +00:00
|
|
|
array("task_name" => $task->name, "task_id" => $task->id)),
|
2009-06-10 20:37:38 -07:00
|
|
|
html::anchor("admin/maintenance", t("maintenance")));
|
2010-07-31 21:16:17 -07:00
|
|
|
print $view;
|
2008-12-28 10:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-28 23:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Resume a stalled task
|
|
|
|
|
* @param string $task_id
|
|
|
|
|
*/
|
|
|
|
|
public function resume($task_id) {
|
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
2008-12-28 10:12:41 +00:00
|
|
|
$task = ORM::factory("task", $task_id);
|
2009-11-25 13:22:24 -08:00
|
|
|
if (!$task->loaded()) {
|
2008-12-28 10:12:41 +00:00
|
|
|
throw new Exception("@todo MISSING_TASK");
|
|
|
|
|
}
|
2008-12-28 23:48:15 +00:00
|
|
|
$view = new View("admin_maintenance_task.html");
|
|
|
|
|
$view->task = $task;
|
|
|
|
|
|
2009-07-05 17:38:49 -07:00
|
|
|
$task->log(t("Task %task_name resumed (task id %task_id)",
|
|
|
|
|
array("task_name" => $task->name, "task_id" => $task->id)));
|
2009-01-15 10:02:41 +00:00
|
|
|
log::info("tasks", t("Task %task_name resumed (task id %task_id)",
|
2009-01-08 17:13:06 +00:00
|
|
|
array("task_name" => $task->name, "task_id" => $task->id)),
|
2009-06-10 20:37:38 -07:00
|
|
|
html::anchor("admin/maintenance", t("maintenance")));
|
2010-07-31 21:16:17 -07:00
|
|
|
print $view;
|
2008-12-28 23:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-05 17:38:49 -07:00
|
|
|
/**
|
|
|
|
|
* Show the task log
|
|
|
|
|
* @param string $task_id
|
|
|
|
|
*/
|
|
|
|
|
public function show_log($task_id) {
|
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
|
|
|
|
$task = ORM::factory("task", $task_id);
|
2009-11-25 13:22:24 -08:00
|
|
|
if (!$task->loaded()) {
|
2009-07-05 17:38:49 -07:00
|
|
|
throw new Exception("@todo MISSING_TASK");
|
|
|
|
|
}
|
|
|
|
|
$view = new View("admin_maintenance_show_log.html");
|
|
|
|
|
$view->task = $task;
|
|
|
|
|
|
2010-07-31 21:16:17 -07:00
|
|
|
print $view;
|
2009-07-05 17:38:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save the task log
|
|
|
|
|
* @param string $task_id
|
|
|
|
|
*/
|
|
|
|
|
public function save_log($task_id) {
|
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
|
|
|
|
$task = ORM::factory("task", $task_id);
|
2009-11-25 13:22:24 -08:00
|
|
|
if (!$task->loaded()) {
|
2009-07-05 17:38:49 -07:00
|
|
|
throw new Exception("@todo MISSING_TASK");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header("Content-Type: application/text");
|
2009-07-07 12:49:21 -07:00
|
|
|
header("Content-Disposition: filename=gallery3_task_log.txt");
|
|
|
|
|
print $task->get_log();
|
2009-07-05 17:38:49 -07:00
|
|
|
}
|
|
|
|
|
|
2008-12-28 23:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Cancel a task.
|
|
|
|
|
* @param string $task_id
|
|
|
|
|
*/
|
|
|
|
|
public function cancel($task_id) {
|
|
|
|
|
access::verify_csrf();
|
2008-12-28 10:12:41 +00:00
|
|
|
|
2009-02-27 16:28:20 +00:00
|
|
|
task::cancel($task_id);
|
2008-12-28 23:48:15 +00:00
|
|
|
|
2009-01-08 17:13:06 +00:00
|
|
|
message::success(t("Task cancelled"));
|
2008-12-28 23:48:15 +00:00
|
|
|
url::redirect("admin/maintenance");
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-09 07:45:38 +00:00
|
|
|
public function cancel_running_tasks() {
|
|
|
|
|
access::verify_csrf();
|
2009-12-02 00:34:34 -08:00
|
|
|
db::build()
|
|
|
|
|
->update("tasks")
|
|
|
|
|
->set("done", 1)
|
|
|
|
|
->set("state", "cancelled")
|
|
|
|
|
->where("done", "=", 0)
|
|
|
|
|
->execute();
|
2009-03-09 07:45:38 +00:00
|
|
|
message::success(t("All running tasks cancelled"));
|
|
|
|
|
url::redirect("admin/maintenance");
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-28 23:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Remove a task.
|
|
|
|
|
* @param string $task_id
|
|
|
|
|
*/
|
|
|
|
|
public function remove($task_id) {
|
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
2009-02-27 16:28:20 +00:00
|
|
|
task::remove($task_id);
|
|
|
|
|
|
2009-01-08 17:13:06 +00:00
|
|
|
message::success(t("Task removed"));
|
2008-12-28 23:48:15 +00:00
|
|
|
url::redirect("admin/maintenance");
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-09 07:28:19 +00:00
|
|
|
public function remove_finished_tasks() {
|
|
|
|
|
access::verify_csrf();
|
2009-07-06 22:20:04 -07:00
|
|
|
|
|
|
|
|
// Do it the long way so we can call delete and remove the cache.
|
|
|
|
|
$finished = ORM::factory("task")
|
2009-11-26 12:09:04 -08:00
|
|
|
->where("done", "=", 1)
|
2009-07-06 22:20:04 -07:00
|
|
|
->find_all();
|
|
|
|
|
foreach ($finished as $task) {
|
|
|
|
|
task::remove($task->id);
|
|
|
|
|
}
|
2009-03-09 07:28:19 +00:00
|
|
|
message::success(t("All finished tasks removed"));
|
|
|
|
|
url::redirect("admin/maintenance");
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-28 23:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Run a task. This will trigger the task to do a small amount of work, then it will report
|
|
|
|
|
* back with status on the task.
|
|
|
|
|
* @param string $task_id
|
|
|
|
|
*/
|
|
|
|
|
public function run($task_id) {
|
|
|
|
|
access::verify_csrf();
|
|
|
|
|
|
2009-05-13 01:21:09 +00:00
|
|
|
try {
|
|
|
|
|
$task = task::run($task_id);
|
|
|
|
|
} catch (Exception $e) {
|
2009-11-25 13:22:24 -08:00
|
|
|
Kohana_Log::add(
|
2009-05-13 01:21:09 +00:00
|
|
|
"error",
|
|
|
|
|
sprintf(
|
|
|
|
|
"%s in %s at line %s:\n%s", $e->getMessage(), $e->getFile(),
|
|
|
|
|
$e->getLine(), $e->getTraceAsString()));
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
2008-12-28 10:12:41 +00:00
|
|
|
|
2008-12-28 23:48:15 +00:00
|
|
|
if ($task->done) {
|
|
|
|
|
switch ($task->state) {
|
|
|
|
|
case "success":
|
2009-01-15 10:02:41 +00:00
|
|
|
log::success("tasks", t("Task %task_name completed (task id %task_id)",
|
2009-01-08 17:13:06 +00:00
|
|
|
array("task_name" => $task->name, "task_id" => $task->id)),
|
2009-06-10 20:37:38 -07:00
|
|
|
html::anchor("admin/maintenance", t("maintenance")));
|
2009-01-08 17:13:06 +00:00
|
|
|
message::success(t("Task completed successfully"));
|
2008-12-28 23:48:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "error":
|
2009-01-15 10:02:41 +00:00
|
|
|
log::error("tasks", t("Task %task_name failed (task id %task_id)",
|
2009-01-08 17:13:06 +00:00
|
|
|
array("task_name" => $task->name, "task_id" => $task->id)),
|
2009-06-10 20:37:38 -07:00
|
|
|
html::anchor("admin/maintenance", t("maintenance")));
|
2009-01-08 17:13:06 +00:00
|
|
|
message::success(t("Task failed"));
|
2008-12-28 23:48:15 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2010-02-21 23:23:48 -08:00
|
|
|
// Using sprintf("%F") to avoid comma as decimal separator.
|
2010-07-21 21:30:13 -07:00
|
|
|
json::reply(array("result" => "success",
|
2010-07-31 21:16:17 -07:00
|
|
|
"task" => array(
|
|
|
|
|
"percent_complete" => sprintf("%F", $task->percent_complete),
|
|
|
|
|
"status" => (string) $task->status,
|
|
|
|
|
"done" => (bool) $task->done),
|
|
|
|
|
"location" => url::site("admin/maintenance")));
|
2009-03-05 06:28:23 +00:00
|
|
|
|
2008-12-28 23:48:15 +00:00
|
|
|
} else {
|
2010-07-21 21:30:13 -07:00
|
|
|
json::reply(array("result" => "in_progress",
|
2010-07-31 21:16:17 -07:00
|
|
|
"task" => array(
|
|
|
|
|
"percent_complete" => sprintf("%F", $task->percent_complete),
|
|
|
|
|
"status" => (string) $task->status,
|
|
|
|
|
"done" => (bool) $task->done)));
|
2008-12-28 23:48:15 +00:00
|
|
|
}
|
2008-12-28 10:12:41 +00:00
|
|
|
}
|
2010-08-01 21:00:30 -07:00
|
|
|
|
|
|
|
|
public function maintenance_mode($value) {
|
|
|
|
|
access::verify_csrf();
|
|
|
|
|
module::set_var("gallery", "maintenance_mode", $value);
|
|
|
|
|
url::redirect("admin/maintenance");
|
|
|
|
|
}
|
2008-12-28 10:12:41 +00:00
|
|
|
}
|