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

Improve the debugging experience #446

Open
programarivm opened this issue Jan 13, 2025 · 0 comments
Open

Improve the debugging experience #446

programarivm opened this issue Jan 13, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@programarivm
Copy link
Member

At the moment, spatie/async is being used to run the commands in parallel so that the event loop is not blocked as shown in the following example.

<?php

namespace ChessServer\Command\Game\Async;

use ChessServer\Command\AbstractAsyncCommand;
use ChessServer\Socket\AbstractSocket;

class ExtractCommand extends AbstractAsyncCommand
{
    public function __construct()
    {
        $this->name = '/extract';
        $this->description = 'Extracts oscillations data from a game.';
        $this->params = [
            'params' => '<string>',
        ];
    }

    public function validate(array $argv)
    {
        return count($argv) - 1 === count($this->params);
    }

    public function run(AbstractSocket $socket, array $argv, int $id)
    {
        $params = json_decode(stripslashes($argv[1]), true);

        $this->pool->add(new ExtractTask($params))
            ->then(function ($result) use ($socket, $id) {
                return $socket->getClientStorage()->send([$id], [
                    $this->name => $result,
                ]);
            });
    }
}

However, with the current implementation of the command line interface, log messages are not being written into the log files.

  • storage/data.log
  • storage/game.log
  • storage/binary.log
  • storage/auth.log

A call to the wait() method needs to be made in order for the messages to be logged in the development environment.

<?php

namespace ChessServer\Command\Game\Async;

use ChessServer\Command\AbstractAsyncCommand;
use ChessServer\Socket\AbstractSocket;

class ExtractCommand extends AbstractAsyncCommand
{
    public function __construct()
    {
        $this->name = '/extract';
        $this->description = 'Extracts oscillations data from a game.';
        $this->params = [
            'params' => '<string>',
        ];
    }

    public function validate(array $argv)
    {
        return count($argv) - 1 === count($this->params);
    }

    public function run(AbstractSocket $socket, array $argv, int $id)
    {
        $params = json_decode(stripslashes($argv[1]), true);

        $this->pool->add(new ExtractTask($params))
            ->then(function ($result) use ($socket, $id) {
                return $socket->getClientStorage()->send([$id], [
                    $this->name => $result,
                ]);
            });
            
        $this->pool->wait();
    }
}

The thing is the call to the wait() method needs to be removed for the staging server not to block the event loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant