Skip to content
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

Cached health endpoint not working when in maintenance mode #53943

Closed
sanfair opened this issue Dec 17, 2024 · 0 comments · Fixed by #53974
Closed

Cached health endpoint not working when in maintenance mode #53943

sanfair opened this issue Dec 17, 2024 · 0 comments · Fixed by #53974
Labels

Comments

@sanfair
Copy link

sanfair commented Dec 17, 2024

Laravel Version

11.35.0

PHP Version

8.3.14

Database Driver & Version

No response

Description

Follow up for #53772

This implementation does work, but only if you don't use php artisan optimize or php artisan route:cache

If you cache routes, then route definitions are restored from the cache. (check the first code snipped in the spoiler below)

And the closure that eventually excludes a health route from maintenance mode will never be executed.

Closure execution (spoiler)

if ($this->routesAreCached()) {
$this->loadCachedRoutes();
} else {
$this->loadRoutes();

protected function loadRoutes()
{
if (! is_null(self::$alwaysLoadRoutesUsing)) {
$this->app->call(self::$alwaysLoadRoutesUsing);

public static function loadRoutesUsing(?Closure $routesCallback)
{
self::$alwaysLoadRoutesUsing = $routesCallback;
}

if (is_null($using) && (is_string($web) || is_array($web) || is_string($api) || is_array($api) || is_string($pages) || is_string($health)) || is_callable($then)) {
$using = $this->buildRoutingCallback($web, $api, $pages, $health, $apiPrefix, $then);
}
AppRouteServiceProvider::loadRoutesUsing($using);

protected function buildRoutingCallback(array|string|null $web,
array|string|null $api,
?string $pages,
?string $health,
string $apiPrefix,
?callable $then)
{
return function () use ($web, $api, $pages, $health, $apiPrefix, $then) {
if (is_string($api) || is_array($api)) {
if (is_array($api)) {
foreach ($api as $apiRoute) {
if (realpath($apiRoute) !== false) {
Route::middleware('api')->prefix($apiPrefix)->group($apiRoute);
}
}
} else {
Route::middleware('api')->prefix($apiPrefix)->group($api);
}
}
if (is_string($health)) {
PreventRequestsDuringMaintenance::except($health);

Steps To Reproduce

  1. Run php artisan optimize
  2. Put site in maintenance mode php artisan down
  3. Open health route
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants