2

For example this code

<?php
if (some_condition()) {
    header('Location: /');
    exit();
}

// do a lot of things...

On classic PHP FPM previous code works fine. But on PHP+Swoole, we ge this error

swoole exit {"exception":"[object] (Swoole\\ExitException(code: 0): swoole exit at

The error is understandable. But, what is the easiest way to migrate it?

raina77ow
  • 103,633
  • 15
  • 192
  • 229
pablorsk
  • 3,861
  • 1
  • 32
  • 37

3 Answers3

2

For PHP FPM, each piece of code was executed individually, and the result (output of that process) was piped back to a client. So whenever we wanted to stop processing, we just triggered exit().

With Swoole, however, the server is expected to run all the time. Sure, it's possible to stop the Process with Swoole\Process::exit() - but it's usually up to controller's to trigger sending the response immediately. For example:

$server = new Swoole\HTTP\Server("127.0.0.1", 9501);

$server->on("start", function (Swoole\Http\Server $server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$server->on("request", function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$server->start();

In this case, $response->end is the method that does essentially the same thing as exit() in PHP FPM world. Note that it's quite similar to what happens in Node world: the server is expected to be running all the time, and it's the controllers (functions processing each individual request) deciding whether to stop handling individual request and pass back the response, both headers and body.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • Thanks! It will not be easy to implement on legacy code. Any idea for Laravel Octane? Like some Response Facade... – pablorsk Jul 04 '21 at 00:05
  • 1
    Yes, converting legacy php services to swoole might be tricky indeed. It might be worth analyzing the code to see all the places when there's a direct interaction with processes (like that exit() you mentioned). Perhaps it'll be possible to run the code as separate Swoole processes... but I'm not sure it'll be always possible. It's quite different models, after all. – raina77ow Jul 04 '21 at 00:12
1

You can learn about \Swoole\ExitException.

use Swoole\Coroutine;
use function Swoole\Coroutine\run;

function route()
{
    controller();
}

function controller()
{
    your_code();
}

function your_code()
{
    Coroutine::sleep(.001);
    exit(1);
}

run(function () {
    try {
        route();
    } catch (\Swoole\ExitException $e) {
        var_dump($e->getMessage());
        var_dump($e->getStatus() === 1);
        var_dump($e->getFlags() === SWOOLE_EXIT_IN_COROUTINE);
    }
});
Luffy
  • 101
  • 2
0

How to replace exit() on Laravel + Swoole

Yes, its a very bad practice exit() function. But own Laravel app bootstrap a Legacy code. Then, by associated costs, its a better solution this way.

  1. Do a renderable exception
class ExitException extends Exception implements Renderable
{
    public function report() {
        return true; // dont report
    }

    public function render()
    {
        return ' '; // prevent 500 html dump error
    }
}
  1. Replace exit(); with
// exit;
throw new ExitException();

That exception will be handled by your app Error Handler, and send an empty string as response to Swoole request handler.

I repeat: IS NOT AN ELEGANT SOLUTION, just prevent change a lot of code on legacy app.

pablorsk
  • 3,861
  • 1
  • 32
  • 37