Keep finished queued tasks and save start/end time
This commit is contained in:
Родитель
c7270a2a64
Коммит
4181a75da0
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
// Update control tables
|
||||
|
||||
$migrate = function() {
|
||||
mysql_query("ALTER TABLE `control_task_queue` DROP `busy`;") or die(mysql_error());
|
||||
mysql_query("ALTER TABLE `control_task_queue` ADD `finish` INT (10) UNSIGNED NOT NULL;") or die(mysql_error());
|
||||
mysql_query("ALTER TABLE `control_task_queue` ADD `error` TEXT NOT NULL;") or die(mysql_error());
|
||||
};
|
||||
|
||||
$rollback = function() {
|
||||
mysql_query("ALTER TABLE `control_task_queue` DROP `finish`;") or die(mysql_error());
|
||||
mysql_query("ALTER TABLE `control_task_queue` DROP `error`;") or die(mysql_error());
|
||||
mysql_query("ALTER TABLE `control_task_queue` ADD `busy` tinyint (1) NOT NULL;") or die(mysql_error());
|
||||
mysql_query("TRUNCATE TABLE control_task_queue");
|
||||
};
|
|
@ -33,6 +33,10 @@ class Config {
|
|||
}
|
||||
}
|
||||
|
||||
function throw_exception($exception) {
|
||||
throw new Exception($exception);
|
||||
}
|
||||
|
||||
function init_database()
|
||||
{
|
||||
global $config;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
require_once("DB.php");
|
||||
|
||||
class Build {
|
||||
class Build extends DB {
|
||||
|
||||
public static $db = "awfy_build";
|
||||
|
||||
|
|
|
@ -8,5 +8,21 @@ class DB {
|
|||
$field = mysql_fetch_object($qField);
|
||||
return $field->field;
|
||||
}
|
||||
|
||||
public function updateInt($field, $value) {
|
||||
$value = (int)$value;
|
||||
$this->updateRaw($field, $value);
|
||||
}
|
||||
|
||||
public function updateString($field, $value) {
|
||||
$value = "'".mysql_real_escape_string($value)."'";
|
||||
$this->updateRaw($field, $value);
|
||||
}
|
||||
|
||||
public function updateRaw($field, $value) {
|
||||
mysql_query("UPDATE {$this::$db}
|
||||
SET $field = $value
|
||||
WHERE id = {$this->id}") or throw_exception(mysql_error());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,30 +1,30 @@
|
|||
<?php
|
||||
|
||||
class QueuedTask {
|
||||
require_once("DB.php");
|
||||
|
||||
class QueuedTask extends DB {
|
||||
|
||||
public static $db = "control_task_queue";
|
||||
|
||||
// db: control_task_queue
|
||||
function __construct($id) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
function setBusy() {
|
||||
mysql_query("UPDATE control_task_queue SET busy = 1 WHERE id = {$this->id}") or die(mysql_error());
|
||||
function setStarted() {
|
||||
$this->updateRaw("start", "UNIX_TIMESTAMP()");
|
||||
}
|
||||
|
||||
function setFinished() {
|
||||
mysql_query("DELETE FROM control_task_queue WHERE id = {$this->id}");
|
||||
$this->updateRaw("finish", "UNIX_TIMESTAMP()");
|
||||
}
|
||||
|
||||
function reportError($error) {
|
||||
$this->setFinished();
|
||||
$this->updateString("error", empty($error) ? "unknown error" : $error);
|
||||
}
|
||||
|
||||
function task() {
|
||||
$qTask = mysql_query("SELECT task
|
||||
FROM control_task_queue
|
||||
WHERE id = {$this->id}");
|
||||
$task = mysql_fetch_object($qTask);
|
||||
return $task->task;
|
||||
}
|
||||
|
||||
function id() {
|
||||
return $this->id;
|
||||
return $this->select("task");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,32 +11,43 @@ class TaskQueue {
|
|||
|
||||
// Returns if there is a task still running.
|
||||
function has_active_task() {
|
||||
$qTask = mysql_query("SELECT *
|
||||
$qTask = mysql_query("SELECT id
|
||||
FROM control_task_queue
|
||||
WHERE control_unit_id = {$this->unit_id} AND busy = 1
|
||||
WHERE control_unit_id = {$this->unit_id} AND
|
||||
start > 0 AND
|
||||
finish = 0
|
||||
ORDER BY id LIMIT 1") or die(mysql_error());
|
||||
return mysql_num_rows($qTask) != 0;
|
||||
}
|
||||
|
||||
function get_active_task() {
|
||||
$qTask = mysql_query("SELECT id
|
||||
FROM control_task_queue
|
||||
WHERE control_unit_id = {$this->unit_id} AND
|
||||
start > 0 AND
|
||||
finish = 0
|
||||
ORDER BY id LIMIT 1") or die(mysql_error());
|
||||
$task = mysql_fetch_object($qTask);
|
||||
return new QueuedTask($task->id);
|
||||
}
|
||||
|
||||
function has_queued_tasks() {
|
||||
$qTask = mysql_query("SELECT *
|
||||
$qTask = mysql_query("SELECT id
|
||||
FROM control_task_queue
|
||||
WHERE control_unit_id = {$this->unit_id} and busy = 0
|
||||
WHERE control_unit_id = {$this->unit_id} AND
|
||||
start = 0
|
||||
ORDER BY id LIMIT 1") or die(mysql_error());
|
||||
return mysql_num_rows($qTask) != 0;
|
||||
|
||||
}
|
||||
|
||||
function pop() {
|
||||
function get_oldest_queued_tasks() {
|
||||
$qTask = mysql_query("SELECT id
|
||||
FROM control_task_queue
|
||||
WHERE control_unit_id = {$this->unit_id} and busy = 0
|
||||
WHERE control_unit_id = {$this->unit_id} AND
|
||||
start = 0
|
||||
ORDER BY id LIMIT 1") or die(mysql_error());
|
||||
$task = mysql_fetch_object($qTask);
|
||||
|
||||
$task = new QueuedTask($task->id);
|
||||
$task->setBusy();
|
||||
|
||||
return $task;
|
||||
return new QueuedTask($task->id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,7 +105,7 @@ class RetriggerController {
|
|||
foreach ($this->tasks as $task) {
|
||||
mysql_query("INSERT INTO control_task_queue
|
||||
(control_unit_id, task)
|
||||
VALUES ({$this->unit_id}, '".mysql_escape_string($task->task())."')");
|
||||
VALUES ({$this->unit_id}, '".mysql_escape_string($task->task())."')") or throw_exception(mysql_error());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,19 +15,25 @@ init_database();
|
|||
if ($unit = GET_int("unit")) {
|
||||
|
||||
$queue = new TaskQueue($unit);
|
||||
if ($queue->has_active_task())
|
||||
Slack::log("requesting new task, while old task is still running!");
|
||||
if ($queue->has_active_task()) {
|
||||
Slack::log("requesting new task, while old task is still running!");
|
||||
$task = $queue->get_active_task();
|
||||
$task->reportError("Requested new task, while this task was still running.");
|
||||
}
|
||||
|
||||
if (!$queue->has_queued_tasks()) {
|
||||
$retrigger = RetriggerController::fromUnit($unit);
|
||||
if (count($retrigger->tasks) == 0)
|
||||
die("No tasks to schedule");
|
||||
$retrigger->enqueue();
|
||||
}
|
||||
}
|
||||
|
||||
$task = $queue->pop();
|
||||
$task = $queue->get_oldest_queued_tasks();
|
||||
$task->setStarted();
|
||||
|
||||
echo json_encode(Array(
|
||||
echo json_encode(Array(
|
||||
"task" => $task->task(),
|
||||
"id" => $task->id()
|
||||
"id" => $task->id
|
||||
));
|
||||
|
||||
die();
|
||||
|
@ -37,5 +43,5 @@ if ($unit = GET_int("unit")) {
|
|||
$task = new QueuedTask($task_id);
|
||||
$task->setFinished();
|
||||
|
||||
die();
|
||||
die();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче