-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: propagate status code from platform api when routing wiki requests #410
Changes from all commits
8ada66b
b7666ab
0b9240f
9e14373
0116f75
75c7bd9
41e6c57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,34 +2,38 @@ | |
|
||
namespace WBStack\Info; | ||
|
||
class GlobalSetException extends \Exception {} | ||
|
||
/** | ||
* A class for setting a global holding a WBStackInfo object. | ||
* This includes lookup of the data for the global from cache or API. | ||
*/ | ||
|
||
class GlobalSet { | ||
|
||
/** | ||
* @param string $requestDomain A request domain, or 'maint' Example: 'addshore-alpha.wiki.opencura.com' | ||
* @return bool was the global successfully set? | ||
* @return void | ||
*/ | ||
public static function forDomain( $requestDomain ) { | ||
public static function forDomain($requestDomain) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the linting here looks "funny" because MediaWiki uses a very non-php standard code style. No objection to this being changed here since we don't lint this anyway but perhaps in future we should add linting to match the MediaWiki style to this weird codebase in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean if we want a code style, then it should be applied everywhere. I just tried to make it consistent per function. Which is probably futile ... |
||
// Normalize the domain by setting to lowercase | ||
$requestDomain = strtolower($requestDomain); | ||
|
||
// If the domain is 'maint' then set the maint settings | ||
if($requestDomain === 'maint') { | ||
if ($requestDomain === 'maint') { | ||
self::forMaint(); | ||
return true; | ||
return; | ||
} | ||
|
||
$info = self::getCachedOrFreshInfo( $requestDomain ); | ||
|
||
if($info === null) { | ||
return false; | ||
$info = self::getCachedOrFreshInfo($requestDomain); | ||
if ($info instanceof WBStackLookupFailure) { | ||
throw new GlobalSetException( | ||
"Failure looking up wiki $requestDomain.", | ||
$info->statusCode, | ||
); | ||
} | ||
|
||
self::setGlobal( $info ); | ||
return true; | ||
self::setGlobal($info); | ||
} | ||
|
||
/** | ||
|
@@ -60,7 +64,7 @@ private static function setGlobal( $info ) { | |
|
||
/** | ||
* @param string $requestDomain | ||
* @return WBStackInfo|null | ||
* @return WBStackInfo|WBStackLookupFailure | ||
*/ | ||
private static function getCachedOrFreshInfo( $requestDomain ) { | ||
$info = self::getInfoFromApcCache( $requestDomain ); | ||
|
@@ -70,30 +74,29 @@ private static function getCachedOrFreshInfo( $requestDomain ) { | |
|
||
// TODO create an APC lock saying this proc is going to get fresh data? | ||
// TODO in reality all of this needs to change... | ||
|
||
$info = self::getInfoFromApi( $requestDomain ); | ||
|
||
// Cache positive results for 10 seconds, negative for 2 | ||
$ttl = $info ? 10 : 2; | ||
// Cache positive results for 10 seconds, failures for 2 | ||
$ttl = $info instanceof WBStackInfo ? 10 : 2; | ||
self::writeInfoToApcCache( $requestDomain, $info, $ttl ); | ||
|
||
return $info; | ||
} | ||
|
||
/** | ||
* @param string $requestDomain | ||
* @return WBStackInfo|null|bool false if no info is cached (should check), null for cached empty data (should not check) | ||
* @return WBStackInfo|WBStackLookupFailure|false false if no info is cached (should check) | ||
*/ | ||
private static function getInfoFromApcCache( $requestDomain ) { | ||
return apcu_fetch( self::cacheKey($requestDomain) ); | ||
} | ||
|
||
/** | ||
* @param string $requestDomain | ||
* @param WBStackInfo|null $info | ||
* @param WBStackInfo|WBStackLookupFailure $info | ||
* @param int $ttl | ||
*/ | ||
private static function writeInfoToApcCache( $requestDomain, ?WBStackInfo $info, $ttl ) { | ||
private static function writeInfoToApcCache( $requestDomain, $info, $ttl ) { | ||
$result = apcu_store( self::cacheKey($requestDomain), $info, $ttl ); | ||
if(!$result) { | ||
// TODO log failed stores?! | ||
|
@@ -106,7 +109,7 @@ private static function cacheKey( $requestDomain ) { | |
|
||
/** | ||
* @param string $requestDomain | ||
* @return WBStackInfo|null | ||
* @return WBStackInfo|WBStackLookupFailure | ||
*/ | ||
private static function getInfoFromApi( $requestDomain ) { | ||
// START generic getting of wiki info from domain | ||
|
@@ -120,16 +123,22 @@ private static function getInfoFromApi( $requestDomain ) { | |
curl_setopt($client, CURLOPT_HTTPHEADER, $headers); | ||
curl_setopt($client, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt( $client, CURLOPT_USERAGENT, "WBStack - MediaWiki - WBStackInfo::getInfoFromApi" ); | ||
|
||
$response = curl_exec($client); | ||
if ($response === false) { | ||
return new WBStackLookupFailure(502); | ||
} | ||
$responseCode = intval(curl_getinfo($client, CURLINFO_RESPONSE_CODE)); | ||
m90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TODO detect non 200 response here, and pass that out to the user as an error | ||
if ($responseCode > 399) { | ||
return new WBStackLookupFailure($responseCode); | ||
} | ||
|
||
$info = WBStackInfo::newFromJsonString($response, $requestDomain); | ||
if (!$info) { | ||
return null; | ||
try { | ||
return WBStackInfo::newFromJsonString($response, $requestDomain); | ||
} catch (\Exception $ex) { | ||
return new WBStackLookupFailure(502); | ||
} | ||
return $info; | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense and probably propagating the precise nature of the failure with an Exception.
In any case this probably makes sense and is then super clear that this is really a side effect only function.
Naturally trying to write this in a clean way is pretty hard since the desired behaviour is charging around a setting a load of globals