laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.75k stars 291 forks source link

Ability to get basic information about server #758

Closed dominikhalvonik closed 11 months ago

dominikhalvonik commented 11 months ago

Hi guys,

It would be beneficial if we had the capability to retrieve basic information when running the Swoole / OpenSwoole server. For instance, the workerId is crucial, but currently, there's no straightforward method to obtain this in Laravel. A simple solution could be creating a getter that returns an instance of Swoole\Http\Server.

The reason I'm raising this request is that I'm aiming to write code which identifies if it's being executed from a Worker or a Task Worker. According to the documentation, the only way to discern this is by knowing the workerId (https://openswoole.com/docs/modules/swoole-server-on-workerstart#description). I want to ensure that I don't run Octane::concurrently() inside a task worker.

Thank you.

driesvints commented 11 months ago

If you could send in a basic PR we could see how that would look 👍

dominikhalvonik commented 11 months ago

Hi @driesvints ,

I created a branch from master and wanted to push it so I can create PR from my branch to master but I am unable to push the branch - Permission denied. So just to simplified thisngs, the only change is done in file Laravel\Octane\Octane. And the file looks like this:

<?php

namespace Laravel\Octane;

use Exception;
use Laravel\Octane\Swoole\WorkerState;
use Swoole\Http\Server;
use Swoole\Table;
use Throwable;

class Octane
{
    use Concerns\ProvidesConcurrencySupport;
    use Concerns\ProvidesDefaultConfigurationOptions;
    use Concerns\ProvidesRouting;
    use Concerns\RegistersTickHandlers;

    /**
     * Get a Swoole table instance.
     */
    public function table(string $table): Table
    {
        if (! app()->bound(Server::class)) {
            throw new Exception('Tables may only be accessed when using the Swoole server.');
        }

        $tables = app(WorkerState::class)->tables;

        if (! isset($tables[$table])) {
            throw new Exception("Swoole table [{$table}] has not been configured.");
        }

        return $tables[$table];
    }

    /**
     * Format an exception to a string that should be returned to the client.
     */
    public static function formatExceptionForClient(Throwable $e, bool $debug = false): string
    {
        return $debug ? (string) $e : 'Internal server error.';
    }

    /**
     * Return Swoole/OpenSwoole server instance. Useful if you need to get server
     * info like worker id or identify if the correct code is called by worker or task worker etc.
     */
    public static function getServerInstance(): Server
    {
        if (! app()->bound(Server::class)) {
            throw new Exception('Server instance may only be accessed when using the Swoole server.');
        }

        $server = app(Server::class);

        return $server;
    }
}