Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for X-Roll-NTime

  • Loading branch information...
commit 30e06ac6ca3a93a800edb78655af55fad9a0cb41 1 parent 117a13f
@cdhowie authored
View
71 database/migrate.sql
@@ -11,6 +11,9 @@ DELIMITER $$
CREATE PROCEDURE `upgrade_schema`()
ret:
BEGIN
+ DECLARE current_version INT DEFAULT 0;
+ DECLARE target_version INT DEFAULT 3;
+
SET autocommit = 0;
-- Create settings table for users who don't have it.
@@ -24,35 +27,79 @@ BEGIN
INSERT IGNORE INTO `settings` (`key`, `value`)
VALUES ('version', '0');
- -- Get current DB version.
- SELECT @version := `value` FROM `settings` WHERE `key` = 'version';
+ -- Fetch the current version out of the database.
+ SELECT `value` INTO current_version
+ FROM `settings` WHERE `key` = 'version';
+
+ -- If the current version is the target, then nothing needs to be done.
+ IF current_version = target_version THEN
+ SELECT CONCAT(
+ 'Current database version is ',
+ current_version,
+ ', which is the latest. No migration is necessary.'
+ ) AS ' ';
+ LEAVE ret;
+ END IF;
+
+ SELECT CONCAT(
+ 'Current database version is ',
+ current_version,
+ '. Attempting migration to version ',
+ target_version,
+ '.'
+ ) AS ' ';
+
+ -- Migrate paths for each version:
- -- Upgrade paths for each version:
+ IF current_version = '0' THEN
+ SELECT 'Migrating 0 -> 1 ...' AS ' ';
- IF @version = '0' THEN
ALTER TABLE `submitted_work`
ADD INDEX `dashboard_status_index2` (`time`, `worker_id`);
+ UPDATE `settings` SET `value` = '1' WHERE `key` = 'version';
COMMIT;
- SET @version = '1';
+ SET current_version = '1';
END IF;
- IF @version = '1' THEN
+ IF current_version = '1' THEN
+ SELECT 'Migrating 1 -> 2 ...' AS ' ';
+
ALTER TABLE `work_data`
ADD INDEX `time_requested_index` (`time_requested`);
ALTER TABLE `work_data`
ADD INDEX `pool_time` (`pool_id`, `time_requested`);
+ UPDATE `settings` SET `value` = '2' WHERE `key` = 'version';
COMMIT;
- SET @version = '2';
+ SET current_version = '2';
+ END IF;
+
+ IF current_version = '2' THEN
+ SELECT 'Migrating 2 -> 3 ...' AS ' ';
+
+ BEGIN
+ DECLARE EXIT HANDLER FOR SQLEXCEPTION
+ SELECT 'The work_data.data column cannot be shrunk because that would result in duplicate primary key values. Please truncate the work_data table and try migrating again.' AS ' ';
+
+ ALTER TABLE `work_data`
+ CHANGE `data`
+ `data` CHAR(136)
+ CHARACTER SET ascii
+ COLLATE ascii_bin
+ NOT NULL;
+
+ UPDATE `settings` SET `value` = '3' WHERE `key` = 'version';
+ COMMIT;
+ SET current_version = '3';
+ END;
END IF;
- -- Store updated version.
- UPDATE `settings` SET `value` = @version WHERE `key` = 'version';
- COMMIT;
+ SELECT CONCAT('Final database version: ', current_version, '.') AS ' ';
- -- Message for the console.
- SELECT 'Database upgraded successfully.';
+ SELECT IF(current_version = target_version,
+ 'Database migrated successfully.',
+ 'Database migration did not fully complete. Correct the errors displayed above and try again.') AS ' ';
END
$$
View
6 database/schema.sql
@@ -60,7 +60,7 @@ DROP TABLE IF EXISTS `work_data`;
CREATE TABLE `work_data` (
`worker_id` int(11) NOT NULL,
`pool_id` int(11) NOT NULL,
- `data` char(152) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
+ `data` char(136) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`time_requested` datetime NOT NULL,
PRIMARY KEY (`worker_id`,`data`),
KEY `worker_time` (`worker_id`,`time_requested`),
@@ -119,9 +119,9 @@ CREATE TABLE IF NOT EXISTS `settings` (
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
INSERT INTO `settings` (`key`, `value`)
- VALUES ('version', '2')
+ VALUES ('version', '3')
- ON DUPLICATE KEY UPDATE `value` = '2';
+ ON DUPLICATE KEY UPDATE `value` = '3';
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
View
2  htdocs/common.inc.php
@@ -21,7 +21,7 @@
require_once(dirname(__FILE__) . '/config.inc.php');
-define('DB_SCHEMA_VERSION', 2);
+define('DB_SCHEMA_VERSION', 3);
# This header satisfies the Section 13 requirement in the AGPL for both
# unauthenticated users and clients requesting work from the proxy.
View
35 htdocs/index.php
@@ -58,7 +58,7 @@ function process_work($pdo, $worker_id, $pool_id, $response, $json_id) {
(:worker_id, :pool_id, :data, UTC_TIMESTAMP())
');
- $data = strtolower(substr($response->result->data, 0, 152));
+ $data = strtolower(substr($response->result->data, 0, 136));
if (!$q->execute(array(
':worker_id' => $worker_id,
@@ -68,13 +68,20 @@ function process_work($pdo, $worker_id, $pool_id, $response, $json_id) {
}
}
-function set_lp_header($headers, $id, $url) {
- foreach ($headers as $header) {
- $pieces = explode(': ', $header, 2);
+function set_headers($headers, $id, $url) {
+ foreach ($headers as $fullHeader) {
+ $pieces = explode(': ', $fullHeader, 2);
- if (count($pieces) == 2 && $pieces[0] == 'X-Long-Polling') {
- if (strpos($pieces[1], '://') !== FALSE) {
- $lpurl = $pieces[1];
+ if (count($pieces) != 2) {
+ continue;
+ }
+
+ $header = strtolower($pieces[0]);
+ $value = $pieces[1];
+
+ if ($header == 'x-long-polling') {
+ if (strpos($value, '://') !== FALSE) {
+ $lpurl = $value;
} else {
$parts = parse_url($url);
@@ -82,13 +89,13 @@ function set_lp_header($headers, $id, $url) {
$parts['scheme'],
$parts['host'],
(isset($parts['port']) ? (':' . $parts['port']) : ''),
- $pieces[1]);
+ $value);
}
header(sprintf('X-Long-Polling: %s/%d/%s',
$_SERVER['PHP_SELF'], $id, urlencode(base64_encode($lpurl))));
-
- break;
+ } elseif ($header == 'x-roll-ntime') {
+ header($fullHeader);
}
}
}
@@ -139,7 +146,7 @@ function set_lp_header($headers, $id, $url) {
$response = place_json_call(null, $lpurl, $row['username'], $row['password'], $headers);
if (is_object($response) && is_object($response->result)) {
- set_lp_header($headers, $pool, $row['url']);
+ set_headers($headers, $pool, $row['url']);
process_work($pdo, $worker_id, $pool, $response, $response->id);
@@ -184,7 +191,7 @@ function set_lp_header($headers, $id, $url) {
$params = $json->params;
if (is_array($params) && count($params) == 1) {
- $data = substr($params[0], 0, 152);
+ $data = substr($params[0], 0, 136);
$q = $pdo->prepare('
SELECT
@@ -233,7 +240,7 @@ function set_lp_header($headers, $id, $url) {
json_error('Work submission request failed too many times.', $json->id);
}
- set_lp_header($headers, $row['pool_id'], $row['url']);
+ set_headers($headers, $row['pool_id'], $row['url']);
$q = $pdo->prepare('
INSERT INTO submitted_work
@@ -286,7 +293,7 @@ function set_lp_header($headers, $id, $url) {
$response = place_json_call($request, $row['url'], $row['username'], $row['password'], $headers);
if (is_object($response) && is_object($response->result)) {
- set_lp_header($headers, $row['id'], $row['url']);
+ set_headers($headers, $row['id'], $row['url']);
process_work($pdo, $worker_id, $row['id'], $response, $response->id);
Please sign in to comment.
Something went wrong with that request. Please try again.