0

I have upgraded a Lravel 10 app to the latest Laravel 12.36.1.

On the Laravel 10 app I have an Ajax call in the backoffice to put the project down or up that calls a method setMaintenanceMode(string $mode) like the following:

use Artisan;
use Str;

class MaintenanceService {

    public function setMaintenanceMode(string $mode): array
    {
        if($mode == 'down') {
            Artisan::call('down', [
                    '--render' => 'errors.maintenance',
                    '--secret' => config('app.maintenance.secret'),
                    '--retry'  => config('app.maintenance.app_downtime_seconds', 600),
            ]);
            session()->put('maintenance_mode', true);
        }
        if($mode == 'up') {
            Artisan::call('up');
            session()->forget('maintenance_mode');
        }
        
        $mode_new = $mode == 'down' ? 'up' : 'down';
        
        $data = [
                'command'           => $mode,
                'command_name'      => ucfirst($mode),
                'command_name_slug' => Str::slug($mode),
                'last_run'          => now(),
                'run_mode'          => 'manual',
                'run_result'        => 1,
        ];
        
        return [
                'message'  => session()->has('maintenance_mode') ? 'Site is down' : 'Site is up',
                'mode'     => $mode,
                'new_mode' => $mode_new,
                'class'    => session()->has('maintenance_mode') ? 'danger' : 'success',
        ];
    }
}

Even in Maintenace mode, the down file that is set by Laravel can de removed when calling the method setMaintenanceMode(string $mode) being the value passed $mode="up".

On Laravel 10 it works, yet on Laravel 12 when using the same method as above and trying to put the project up by passing the value as $mode="up" it throws a 503 error and the the Ajax call returns it as 503.

My question is: is there a way to override the maintenance mode when calling Artisan::call() while in maintenance mode and get a 200 ok status?

4
  • Are you sure it's the Artisan call that's returning the 503 and not the route itself? You can bypass maintenance mode with a secret token Commented Nov 3 at 17:25
  • One Larvel 10 the secret was not needed to perform the same task. Now I can override it by going to myproject.test/my-secret-code and Laravel 12 sets a cookie. After this I can go to the backoffice and ran the Ajax call with no problem. But this is not optimal for turning the system around and make it work. Commented Nov 3 at 17:30
  • The 503 code represents a server error. Technically there should be an error log somewhere that tells you exactly what that error is. Solving that should bring you closer to that goal. Commented Nov 3 at 17:35
  • @FélixAdriyelGagnon-Grenier The 503 code is the one Laravel throws while in maintenance mode. Commented Nov 3 at 17:37

1 Answer 1

-2

In laravel 12 you can use this in your bootstrap/app.php

   ->withMiddleware(function (Middleware $middleware) {
        $middleware->preventRequestsDuringMaintenance([
            '/maintenance/toggle', // the route name 
        ]);
    })
Route::post('/maintenance/toggle', function (Request $request) {
    $mode = $request->input('mode'); // "up" or "down"

    if ($mode === 'down') {
        Artisan::call('down');

        session()->put('maintenance_mode', true);

        return response()->json([
            'message' => 'Site is now DOWN',
            'status'  => 'down'
        ]);
    }

    if ($mode === 'up') {
        Artisan::call('up');
        session()->forget('maintenance_mode');

        return response()->json([
            'message' => 'Site is now UP',
            'status'  => 'up'
        ]);
    }

    return response()->json(['error' => 'Invalid mode'], 400);
})->withoutMiddleware([VerifyCsrfToken::class]);
    /**
     * Configure the middleware that prevents requests during maintenance mode.
     *
     * @param  array<int, string>  $except
     * @return $this
     */
    public function preventRequestsDuringMaintenance(array $except = [])
    {
        PreventRequestsDuringMaintenance::except($except);

        return $this;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I´ve implemented and tested it, and it still doesn't work with the above solution. Still wondering how to override the 503 maintenance mode.
Well it works for me. you can also try this in your AppServiceProvider use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode; and in your boot method CheckForMaintenanceMode::except([ "your url" ]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.