Skip to content
Permalink
Browse files

Improve handling of when the Redis class goes missing

  • Loading branch information...
SeinopSys committed Jun 11, 2019
1 parent e874349 commit f336550cdc97012635b184c1c50c5b93bb49bed0
Showing with 51 additions and 41 deletions.
  1. +1 −1 app/CoreUtils.php
  2. +7 −9 app/RouteHelper.php
  3. +25 −12 config/init.php
  4. +17 −18 public/index.php
  5. +1 −1 templates/error/fatal.html.twig
@@ -772,7 +772,7 @@ public static function checkStringValidity($string, $Thing, $pattern, $returnErr
public static function getFooterGitInfo(bool $wrap = WRAP, bool $reload_warning = false):string {
$commit_info = RedisHelper::get('commit_info');
if ($commit_info === null || !self::env('PRODUCTION')){
$commit_info = rtrim(shell_exec('git log -1 --date=short --pretty="format:%h;%ci"'));
$commit_info = rtrim(shell_exec('git log -1 --date=short --pretty="format:%h;%ci"'));
RedisHelper::set('commit_info', $commit_info);
}
$data = [
@@ -5,14 +5,12 @@
use App\Controllers\Controller;
class RouteHelper {
public static function processHandler(string $handler):callable {
return function($params) use ($handler){
list($class, $method) = explode('#', $handler);
$class = "App\\Controllers\\$class";
$controller = new $class();
if (false === $controller instanceof Controller)
throw new \RuntimeException("$class must be an instance of ".Controller::class);
$controller->{$method}($params);
};
public static function processHandler(string $handler, $params):void {
[$class, $method] = explode('#', $handler);
$class = "App\\Controllers\\$class";
$controller = new $class();
if (false === $controller instanceof Controller)
throw new \RuntimeException("$class must be an instance of ".Controller::class);
$controller->{$method}($params);
}
}
@@ -15,6 +15,11 @@
use App\About;
use App\CoreUtils;
use App\HTTP;
use App\LibHelper;
use App\NavBreadcrumb;
use App\RedisHelper;
use App\Twig;
if (CoreUtils::env('CSP_ENABLED')){
$csp_header = implode(';', [
@@ -37,29 +42,37 @@
}
function fatal_error(string $cause, ?Throwable $e = null){
\App\HTTP::statusCode(503);
HTTP::statusCode(503);
if ($e !== null)
CoreUtils::error_log(__FILE__.": Fatal error of type $cause; ".$e->getMessage()."\nStack trace:\n".$e->getTraceAsString());
$bc = new \App\NavBreadcrumb('Error');
$bc->setChild(\App\HTTP::STATUS_CODES[503]);
CoreUtils::error_log(sprintf("%s: Fatal error of type $cause; %s\nStack trace:\n%s", __FILE__, $e->getMessage(), $e->getTraceAsString()));
$bc = new NavBreadcrumb('Error');
$bc->setChild(HTTP::STATUS_CODES[503]);
$scope = [
'err_cause' => $cause,
'breadcrumbs' => $bc,
'default_js' => true,
'css' => CoreUtils::DEFAULT_CSS,
'js' => CoreUtils::DEFAULT_JS,
];
\App\LibHelper::process($scope, [], CoreUtils::DEFAULT_LIBS);
foreach ($scope['css'] as &$css)
$css = CoreUtils::cachedAssetLink($css, 'css', 'min.css');
unset($css);
foreach ($scope['js'] as &$js)
$js = CoreUtils::cachedAssetLink($js, 'js', 'min.js');
unset($js);
echo \App\Twig::$env->render('error/fatal.html.twig', $scope);
LibHelper::process($scope, [], CoreUtils::DEFAULT_LIBS);
$scope['css'] = array_map(static function($css){
return CoreUtils::cachedAssetLink($css, 'css', 'min.css');
}, $scope['css']);
$scope['js'] = array_map(static function($js){
return CoreUtils::cachedAssetLink($js, 'js', 'min.js');
}, $scope['js']);
echo Twig::$env->render('error/fatal.html.twig', $scope);
die();
}
// Redis is missing (again) \\
try {
RedisHelper::getInstance();
}
catch (Throwable $e) {
fatal_error('config', $e);
}
// Maintenance mode \\
if (CoreUtils::env('MAINTENANCE_START'))
fatal_error('maintenance');
@@ -1,24 +1,23 @@
<?php
require __DIR__.'/../config/init.php';
require __DIR__.'/../config/init.php';
use App\RegExp;
use App\HTTP;
use App\Users;
use App\CoreUtils;
use App\CoreUtils;
use App\RegExp;
use App\RouteHelper;
// Strip &hellip; and what comes after
$decoded_uri = CoreUtils::trim(urldecode($_SERVER['REQUEST_URI']));
$request_uri = preg_replace(new RegExp('(?:….*|<)$'),'',$decoded_uri);
// Strip non-ascii
$safe_uri = preg_replace(new RegExp('[^ -~]'), '', $request_uri);
// Enforce URL
CoreUtils::fixPath($safe_uri);
// Strip &hellip; and what comes after
$decoded_uri = CoreUtils::trim(urldecode($_SERVER['REQUEST_URI']));
$request_uri = preg_replace(new RegExp('(?:….*|<)$'), '', $decoded_uri);
// Strip non-ascii
$safe_uri = preg_replace(new RegExp('[^ -~]'), '', $request_uri);
// Enforce URL
CoreUtils::fixPath($safe_uri);
require CONFPATH.'routes.php';
/** @var $match array */
$match = $router->match($safe_uri);
if (!isset($match['target']))
CoreUtils::notFound();
(\App\RouteHelper::processHandler($match['target']))($match['params']);
require CONFPATH.'routes.php';
/** @var $match array */
$match = $router->match($safe_uri);
if (!isset($match['target']))
CoreUtils::notFound();
RouteHelper::processHandler($match['target'], $match['params']);
@@ -33,7 +33,7 @@
{% elseif err_cause == 'maintenance' %}
<p>Started {{ time_tag(env('MAINTENANCE_START')) }}</p>
<div class='notice info align-center'>
<p><span class="typcn typcn-info-large"></span> The developer is currently performing some actions that requre the site to be temporarily offline. We'll be back up and running as soon as possible, thank you for your understanding.</p>
<p><span class="typcn typcn-info-large"></span> The developer is currently performing some actions that require the site to be temporarily offline. We'll be back up and running as soon as possible, thank you for your understanding.</p>
</div>
{% endif %}
</div>

0 comments on commit f336550

Please sign in to comment.
You can’t perform that action at this time.