Skip to content

Commit b23c5f8

Browse files
committed
Timeout status code fix
1 parent 6309c33 commit b23c5f8

5 files changed

Lines changed: 40 additions & 38 deletions

File tree

docs/sql/cache.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ CREATE TABLE `robotstxt__cache0` (
4343
`base` VARCHAR(250)
4444
COLLATE utf8_unicode_ci NOT NULL,
4545
`content` TEXT COLLATE utf8_unicode_ci NOT NULL,
46-
`statusCode` SMALLINT(4) UNSIGNED NOT NULL,
46+
`statusCode` SMALLINT(4) UNSIGNED DEFAULT NULL,
4747
`validUntil` INT(10) UNSIGNED NOT NULL,
4848
`nextUpdate` INT(10) UNSIGNED NOT NULL,
49-
`worker` TINYINT(3) UNSIGNED DEFAULT NULL,
49+
`worker` TINYINT(3) UNSIGNED DEFAULT NULL,
5050
PRIMARY KEY (`base`),
5151
KEY `worker` (`worker`, `nextUpdate`)
5252
)

src/Cache.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,14 @@ private function push(UriClient $client)
180180
$statusCode = $client->getStatusCode();
181181
$nextUpdate = $client->nextUpdate();
182182
if (
183-
$statusCode >= 500 &&
184-
$statusCode < 600 &&
185183
mb_stripos($base, 'http') === 0 &&
184+
(
185+
$statusCode === null ||
186+
(
187+
$statusCode >= 500 &&
188+
$statusCode < 600
189+
)
190+
) &&
186191
$this->displacePush($base, $nextUpdate)
187192
) {
188193
return true;
@@ -238,10 +243,29 @@ private function displacePush($base, $nextUpdate)
238243
$query->bindParam(':nextUpdate', $nextUpdate, PDO::PARAM_INT);
239244
return $query->execute();
240245
}
246+
$this->invalidate($base);
241247
}
242248
return false;
243249
}
244250

251+
/**
252+
* Invalidate cache
253+
*
254+
* @param $baseUri
255+
* @return bool
256+
*/
257+
public function invalidate($baseUri)
258+
{
259+
$base = $this->urlBase($this->urlEncode($baseUri));
260+
$query = $this->pdo->prepare(<<<SQL
261+
DELETE FROM robotstxt__cache0
262+
WHERE base = :base;
263+
SQL
264+
);
265+
$query->bindParam(':base', $base, PDO::PARAM_STR);
266+
return $query->execute();
267+
}
268+
245269
/**
246270
* Process the update queue
247271
*
@@ -314,22 +338,4 @@ public function clean($delay = 600)
314338
$query->bindParam(':delay', $delay, PDO::PARAM_INT);
315339
return $query->execute();
316340
}
317-
318-
/**
319-
* Invalidate cache
320-
*
321-
* @param $baseUri
322-
* @return bool
323-
*/
324-
public function invalidate($baseUri)
325-
{
326-
$base = $this->urlBase($this->urlEncode($baseUri));
327-
$query = $this->pdo->prepare(<<<SQL
328-
DELETE FROM robotstxt__cache0
329-
WHERE base = :base;
330-
SQL
331-
);
332-
$query->bindParam(':base', $base, PDO::PARAM_STR);
333-
return $query->execute();
334-
}
335341
}

src/SQL/cache.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ CREATE TABLE `robotstxt__cache0` (
22
`base` VARCHAR(250)
33
COLLATE utf8_unicode_ci NOT NULL,
44
`content` TEXT COLLATE utf8_unicode_ci NOT NULL,
5-
`statusCode` SMALLINT(4) UNSIGNED NOT NULL,
5+
`statusCode` SMALLINT(4) UNSIGNED DEFAULT NULL,
66
`validUntil` INT(10) UNSIGNED NOT NULL,
77
`nextUpdate` INT(10) UNSIGNED NOT NULL,
8-
`worker` TINYINT(3) UNSIGNED DEFAULT NULL,
8+
`worker` TINYINT(3) UNSIGNED DEFAULT NULL,
99
PRIMARY KEY (`base`),
1010
KEY `worker` (`worker`, `nextUpdate`)
1111
)

src/UriClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function __construct($baseUri, array $guzzleConfig = [], $byteLimit = sel
8484
$this->encoding = $this->headerEncoding($response->getHeader('content-type'));
8585
$this->maxAge = $this->headerMaxAge($response->getHeader('cache-control'));
8686
} catch (GuzzleHttp\Exception\TransferException $e) {
87-
$this->statusCode = 404;
87+
$this->statusCode = null;
8888
$this->contents = '';
8989
$this->encoding = self::ENCODING;
9090
$this->maxAge = 0;
@@ -153,7 +153,7 @@ public function getBaseUri()
153153
/**
154154
* Status code
155155
*
156-
* @return int
156+
* @return int|null
157157
*/
158158
public function getStatusCode()
159159
{

tests/CacheDisplaceSQLTest.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,19 @@ public function testCacheDisplaceSQL()
7575
private function check($base)
7676
{
7777
$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
78+
$parser = new RobotsTxtParser\Cache($pdo);
79+
$this->assertInstanceOf('vipnytt\RobotsTxtParser\Cache', $parser);
80+
7881
// Insert fake data
82+
$parser->invalidate($base);
7983
$query = $pdo->prepare(<<<SQL
80-
INSERT IGNORE INTO robotstxt__cache0 (base, content, statusCode, validUntil, nextUpdate)
81-
VALUES (:base, '', 555, UNIX_TIMESTAMP() + 604800, UNIX_TIMESTAMP() - 3600);
84+
INSERT INTO robotstxt__cache0 (base, content, statusCode, validUntil, nextUpdate)
85+
VALUES (:base, '', NULL, UNIX_TIMESTAMP() + 86400, UNIX_TIMESTAMP() - 3600);
8286
SQL
8387
);
8488
$query->bindParam(':base', $base, PDO::PARAM_STR);
8589
$query->execute();
8690

87-
$parser = new RobotsTxtParser\Cache($pdo);
88-
$this->assertInstanceOf('vipnytt\RobotsTxtParser\Cache', $parser);
8991
$parser->client($base);
9092

9193
$parser->cron();
@@ -94,19 +96,13 @@ private function check($base)
9496
$query = $pdo->prepare(<<<SQL
9597
SELECT *
9698
FROM robotstxt__cache0
97-
WHERE base = :base AND validUntil > UNIX_TIMESTAMP() AND nextUpdate > UNIX_TIMESTAMP() AND statusCode >= 500;
99+
WHERE base = :base AND validUntil > UNIX_TIMESTAMP() AND nextUpdate > UNIX_TIMESTAMP() AND statusCode IS NULL;
98100
SQL
99101
);
100102
$query->bindParam(':base', $base, PDO::PARAM_STR);
101103
$query->execute();
102104
// Delete fake data
103-
$cleanup = $pdo->prepare(<<<SQL
104-
DELETE FROM robotstxt__cache0
105-
WHERE base = :base;
106-
SQL
107-
);
108-
$cleanup->bindParam(':base', $base, PDO::PARAM_STR);
109-
$cleanup->execute();
105+
$parser->invalidate($base);
110106
if ($query->rowCount() > 0) {
111107
return true;
112108
}

0 commit comments

Comments
 (0)