Merge branch 'master' of https://github.com/haytjes/arewefastyet
This commit is contained in:
Коммит
80a63801a3
|
@ -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;
|
||||
|
@ -40,33 +44,6 @@ function init_database()
|
|||
mysql_select_db($config->mysql_db_name) or die("ERROR: " . mysql_error());
|
||||
}
|
||||
|
||||
// (string) $message - message to be passed to Slack
|
||||
// (string) $room - room in which to write the message, too
|
||||
// (string) $icon - You can set up custom emoji icons to use with each message
|
||||
function slack($message, $room = "engineering", $icon = ":longbox:")
|
||||
{
|
||||
global $config;
|
||||
|
||||
$room = ($room) ? $room : "engineering";
|
||||
$data = "payload=" . json_encode(array(
|
||||
#"channel" => "#{$room}",
|
||||
"text" => $message,
|
||||
"icon_emoji" => $icon
|
||||
));
|
||||
|
||||
// You can get your webhook endpoint from your Slack settings
|
||||
$url = $config->slack_webhook;
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function username()
|
||||
{
|
||||
if (!isset($_SESSION['persona']))
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
class Slack {
|
||||
// (string) $message - message to be passed to Slack
|
||||
// (string) $icon - You can set up custom emoji icons to use with each message
|
||||
public static function log($message, $icon = ":longbox:")
|
||||
{
|
||||
global $config;
|
||||
|
||||
$data = "payload=" . json_encode(array(
|
||||
"text" => $message,
|
||||
"icon_emoji" => $icon
|
||||
));
|
||||
|
||||
// You can get your webhook endpoint from your Slack settings
|
||||
$url = $config->slack_webhook;
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
|
@ -8,25 +8,32 @@ require_once("internals.php");
|
|||
require_once("lib/RetriggerController.php");
|
||||
require_once("lib/DB/TaskQueue.php");
|
||||
require_once("lib/DB/QueuedTask.php");
|
||||
require_once("lib/Slack.php");
|
||||
|
||||
init_database();
|
||||
|
||||
if ($unit = GET_int("unit")) {
|
||||
|
||||
$queue = new TaskQueue($unit);
|
||||
if ($queue->has_active_task())
|
||||
slack("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();
|
||||
|
@ -36,5 +43,5 @@ if ($unit = GET_int("unit")) {
|
|||
$task = new QueuedTask($task_id);
|
||||
$task->setFinished();
|
||||
|
||||
die();
|
||||
die();
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче