1
0

Make the scheduler module work without any changes to the core modules. Cloned the admin_maintenace.html.php file into the scheduler module. Then the scheduler_event::admin_menu call back replaces the admin_maintenance url with its url.

This commit is contained in:
Tim Almdal 2010-05-31 08:32:17 -07:00
parent ff982c780b
commit 0b6b5bc72b
4 changed files with 281 additions and 33 deletions

View File

@ -18,24 +18,56 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Admin_Schedule_Controller extends Admin_Controller {
/**
* Show a list of all available, running and finished tasks.
*/
public function index() {
$query = db::build()
->update("tasks")
->set("state", "stalled")
->where("done", "=", 0)
->where("state", "<>", "stalled")
->where(new Database_Expression("UNIX_TIMESTAMP(NOW()) - `updated` > 15"))
->execute();
$stalled_count = $query->count();
if ($stalled_count) {
log::warning("tasks",
t2("One task is stalled",
"%count tasks are stalled",
$stalled_count),
t('<a href="%url">view</a>',
array("url" => html::mark_clean(url::site("admin/maintenance")))));
}
$view = new Admin_View("admin.html");
$view->page_title = t("Maintenance tasks");
$view->content = new View("admin_schedule.html");
$view->content->task_definitions = task::get_definitions();
$view->content->running_tasks = ORM::factory("task")
->where("done", "=", 0)->order_by("updated", "DESC")->find_all();
$view->content->finished_tasks = ORM::factory("task")
->where("done", "=", 1)->order_by("updated", "DESC")->find_all();
$view->content->schedule_definitions = scheduler::get_definitions();
print $view;
}
public function form_add($task_callback) {
access::verify_csrf();
$schedule = ORM::factory("schedule");
$schedule->task_callback = $task_callback;
$schedule->next_run_datetime = time();
$v = new View("admin_schedule.html");
$v = new View("admin_schedule_form.html");
$v->form = scheduler::get_form("define", $schedule);
$v->method = "define";
print $v;
}
public function update_form($id) {
public function form_edit($id) {
access::verify_csrf();
$schedule = ORM::factory("schedule", $id);
$v = new View("admin_schedule.html");
$v = new View("admin_schedule_form.html");
$v->form = scheduler::get_form("update", $schedule);
$v->method = "update";
print $v;

View File

@ -100,16 +100,12 @@ class scheduler_Core {
}
static function get_definitions() {
$v = "";
$schedule_definitions = array();
$events = ORM::factory("schedule")
->order_by("next_run_datetime", "asc")
->find_all();
if ($events->count()) {
$v = new View("scheduler_definitions.html");
$v->schedule_definitions = array();
foreach ($events as $schedule) {
$entry[] = $schedule->id;
$entry[] = $schedule->name;
$run_date = strftime("%A, %b %e, %Y %H:%M ", $schedule->next_run_datetime);
$intervals = scheduler::intervals();
$interval = $intervals[$schedule->interval];
@ -121,13 +117,13 @@ class scheduler_Core {
$status = t("Scheduled");
}
$v->schedule_definitions[] = (object)array("id" => $schedule->id,
$schedule_definitions[] = (object)array("id" => $schedule->id,
"name" => $schedule->name,
"run_date" => $run_date,
"interval" => $interval,
"status" => $status);
}
}
return $v;
return $schedule_definitions;
}
}

View File

@ -17,15 +17,10 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class scheduler_event_Core {
static function admin_maintenance_content($maintenance_content) {
$maintenance_content[] = scheduler::get_definitions();
}
static function admin_maintenance_task_buttons($task_buttons) {
$task_buttons[] = (object)array("text" => t("schedule"),
"url" =>url::site("form/add/admin/schedule"));
static function admin_menu($menu, $theme) {
$menu->get("maintenance")
->url(url::site("admin/schedule"));
}
/**

View File

@ -1,11 +1,236 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<style>
#g-define-schedule fieldset fieldset li {
margin-right: .5em;
}
<div id="g-admin-maintenance" class="g-block">
<h1> <?= t("Maintenance tasks") ?> </h1>
<p>
<?= t("Occasionally your Gallery will require some maintenance. Here are some tasks you can use to keep it running smoothly.") ?>
</p>
#g-schedule-time {
width: 40px !important;
}
</style>
<?= $form ?>
<div class="g-block-content">
<div id="g-available-tasks">
<h2> <?= t("Available tasks") ?> </h2>
<table>
<tr>
<th>
<?= t("Name") ?>
</th>
<th>
<?= t("Description") ?>
</th>
<th>
<?= t("Action") ?>
</th>
</tr>
<? foreach ($task_definitions as $task): ?>
<tr class="<?= text::alternate("g-odd", "g-even") ?> <?= log::severity_class($task->severity) ?>">
<td class="<?= log::severity_class($task->severity) ?>">
<?= $task->name ?>
</td>
<td>
<?= $task->description ?>
</td>
<td>
<a href="<?= url::site("admin/maintenance/start/$task->callback?csrf=$csrf") ?>"
class="g-dialog-link g-button ui-icon-left ui-state-default ui-corner-all">
<?= t("run") ?>
</a>
<a href="<?= url::site("form/add/admin/schedule/$task->callback?csrf=$csrf") ?>"
class="g-dialog-link g-button ui-icon-left ui-state-default ui-corner-all">
<?= t("schedule") ?>
</a>
</a>
</td>
</tr>
<? endforeach ?>
</table>
</div>
<? if (count($schedule_definitions) > 0): ?>
<div id="g-sheduled-tasks">
<h2> <?= t("Scheduled tasks") ?> </h2>
<table>
<tr>
<th>
<?= t("Name") ?>
</th>
<th>
<?= t("Next run") ?>
</th>
<th>
<?= t("Frequency") ?>
</th>
</tr>
<? foreach ($schedule_definitions as $entry): ?>
<tr class="<?= text::alternate("g-odd", "g-even") ?>">
<td>
<?= $entry->name ?>
</td>
<td>
<?= $entry->run_date ?>
</td>
<td>
<?= $entry->interval ?>
</td>
<td>
<a href="<?= url::site("form/edit/admin/schedule/$entry->id?csrf=$csrf") ?>"
class="g-dialog-link g-button ui-icon-left ui-state-default ui-corner-all">
<?= t("edit") ?>
</a>
<a href="<?= url::site("admin/schedule/remove_form/$entry->id?csrf=$csrf") ?>"
class="g-dialog-link g-button ui-icon-left ui-state-default ui-corner-all">
<?= t("remove") ?>
</a>
</td>
</tr>
<? endforeach ?>
</table>
</div>
<? endif ?>
<? if ($running_tasks->count()): ?>
<div id="g-running-tasks">
<h2> <?= t("Running tasks") ?> </h2>
<table>
<tr>
<th>
<?= t("Last updated") ?>
</th>
<th>
<?= t("Name") ?>
</th>
<th>
<?= t("Status") ?>
</th>
<th>
<?= t("Info") ?>
</th>
<th>
<?= t("Owner") ?>
</th>
<th>
<a href="<?= url::site("admin/maintenance/cancel_running_tasks?csrf=$csrf") ?>"
class="g-button g-right ui-icon-left ui-state-default ui-corner-all">
<?= t("cancel all") ?></a>
<?= t("Action") ?>
</th>
</tr>
<? foreach ($running_tasks as $task): ?>
<tr class="<?= text::alternate("g-odd", "g-even") ?> <?= $task->state == "stalled" ? "g-warning" : "" ?>">
<td class="<?= $task->state == "stalled" ? "g-warning" : "" ?>">
<?= gallery::date_time($task->updated) ?>
</td>
<td>
<?= $task->name ?>
</td>
<td>
<? if ($task->done): ?>
<? if ($task->state == "cancelled"): ?>
<?= t("Cancelled") ?>
<? endif ?>
<?= t("Close") ?>
<? elseif ($task->state == "stalled"): ?>
<?= t("Stalled") ?>
<? else: ?>
<?= t("%percent_complete% Complete", array("percent_complete" => $task->percent_complete)) ?>
<? endif ?>
</td>
<td>
<?= $task->status ?>
</td>
<td>
<?= html::clean($task->owner()->name) ?>
</td>
<td>
<a href="<?= url::site("admin/maintenance/cancel/$task->id?csrf=$csrf") ?>"
class="g-button g-right ui-icon-left ui-state-default ui-corner-all">
<?= t("cancel") ?>
</a>
<? if ($task->state == "stalled"): ?>
<a class="g-dialog-link g-button ui-icon-left ui-state-default ui-corner-all"
href="<?= url::site("admin/maintenance/resume/$task->id?csrf=$csrf") ?>">
<?= t("resume") ?>
</a>
<? endif ?>
</td>
</tr>
<? endforeach ?>
</table>
</div>
<? endif ?>
<? if ($finished_tasks->count()): ?>
<div id="g-finished-tasks">
<a href="<?= url::site("admin/maintenance/remove_finished_tasks?csrf=$csrf") ?>"
class="g-button g-right ui-icon-left ui-state-default ui-corner-all">
<span class="ui-icon ui-icon-trash"></span><?= t("remove all finished") ?></a>
<h2> <?= t("Finished tasks") ?> </h2>
<table>
<tr>
<th>
<?= t("Last updated") ?>
</th>
<th>
<?= t("Name") ?>
</th>
<th>
<?= t("Status") ?>
</th>
<th>
<?= t("Info") ?>
</th>
<th>
<?= t("Owner") ?>
</th>
<th>
<?= t("Action") ?>
</th>
</tr>
<? foreach ($finished_tasks as $task): ?>
<tr class="<?= text::alternate("g-odd", "g-even") ?> <?= $task->state == "success" ? "g-success" : "g-error" ?>">
<td class="<?= $task->state == "success" ? "g-success" : "g-error" ?>">
<?= gallery::date_time($task->updated) ?>
</td>
<td>
<?= $task->name ?>
</td>
<td>
<? if ($task->state == "success"): ?>
<?= t("Success") ?>
<? elseif ($task->state == "error"): ?>
<?= t("Failed") ?>
<? elseif ($task->state == "cancelled"): ?>
<?= t("Cancelled") ?>
<? endif ?>
</td>
<td>
<?= $task->status ?>
</td>
<td>
<?= html::clean($task->owner()->name) ?>
</td>
<td>
<? if ($task->done): ?>
<a href="<?= url::site("admin/maintenance/remove/$task->id?csrf=$csrf") ?>" class="g-button ui-state-default ui-corner-all">
<?= t("remove") ?>
</a>
<? if ($task->get_log()): ?>
<a href="<?= url::site("admin/maintenance/show_log/$task->id?csrf=$csrf") ?>" class="g-dialog-link g-button ui-state-default ui-corner-all">
<?= t("browse log") ?>
</a>
<? endif ?>
<? else: ?>
<a href="<?= url::site("admin/maintenance/resume/$task->id?csrf=$csrf") ?>" class="g-dialog-link g-button" ui-state-default ui-corner-all>
<?= t("resume") ?>
</a>
<a href="<?= url::site("admin/maintenance/cancel/$task->id?csrf=$csrf") ?>" class="g-button ui-state-default ui-corner-all">
<?= t("cancel") ?>
</a>
<? endif ?>
</ul>
</td>
</tr>
<? endforeach ?>
</table>
</div>
<? endif ?>
</div>
</div>