Skip to content

Commit

Permalink
Improve handling of when the Redis class goes missing
Browse files Browse the repository at this point in the history
  • Loading branch information
SeinopSys committed Jun 11, 2019
1 parent e874349 commit f336550
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 41 deletions.
2 changes: 1 addition & 1 deletion app/CoreUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
16 changes: 7 additions & 9 deletions app/RouteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
37 changes: 25 additions & 12 deletions config/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(';', [
Expand All @@ -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');
Expand Down
35 changes: 17 additions & 18 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -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']);

2 changes: 1 addition & 1 deletion templates/error/fatal.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit f336550

Please sign in to comment.