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

MessageFactory doesn't seem to resolve #15

Open
factormaarten opened this issue Aug 11, 2018 · 14 comments
Open

MessageFactory doesn't seem to resolve #15

factormaarten opened this issue Aug 11, 2018 · 14 comments

Comments

@factormaarten
Copy link

factormaarten commented Aug 11, 2018

Hi Cees-Jan,

I'm trying to get the pool running, but something isn't adding up for me. This is the class I'm calling:

<?php

namespace Database;

use React\EventLoop\LoopInterface;
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
use function React\Promise\resolve;

class PhalconSQL implements ChildInterface
{
    private $phql;

    public static function create(Messenger $messenger, LoopInterface $loop)
    {
        $messenger->registerRpc('executeQuery', function (Payload $payload) {
            return resolve([
                'response' => 'works'
            ]);
        });
    }
}

Here's how I'm calling the rpc:

                    $loop = EventLoopFactory::create();
                    CpuCoreCountFlexible::createFromClass(PhalconSQL::class, $loop)->then(function (PoolInterface $pool) {
                        echo 'tack' . PHP_EOL;

                        $pool->rpc(
                            MessageFactory::rpc('executeQuery')
                        )->then(function (Payload $result){
                            echo $result['response'] . PHP_EOL;
                            $pool->terminate();
                        });

                        echo 'tock' . PHP_EOL;

                    });
                    $loop->run();

What should I change to make it all resolve?

Thanks so much!
Maarten

@Attilathekilla
Copy link

I too would be interested in the solution here

@WyriHaximus
Copy link
Owner

Could you replace the then's with done, and post the errors here?

@factormaarten
Copy link
Author

I've tried that, but it's not giving any errors unfortunately

@WyriHaximus
Copy link
Owner

That is weird, there should always be something. I'll run your code tonight and see if it works on my end

@WyriHaximus
Copy link
Owner

With some minor edits it works:

$loop = EventLoopFactory::create();
CpuCoreCountFlexible::createFromClass(PhalconSQL::class, $loop)->done(function (PoolInterface $pool) {
    echo 'tack' . PHP_EOL;

    $pool->rpc(
        MessagesFactory::rpc('executeQuery')
    )->done(function (Payload $result) use ($pool) {
        echo $result['response'] . PHP_EOL;
        $pool->terminate();
    });

    echo 'tock' . PHP_EOL;

});
$loop->run();

@factormaarten
Copy link
Author

Thanks for the response. This is what's happening:

I start the loop, it echo's tack, it echo's tock and after that it keeps waiting for $result['response']

use React\EventLoop\LoopInterface;
use WyriHaximus\React\ChildProcess\Messenger\ChildInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
use function React\Promise\resolve;

final class ReturnChild implements ChildInterface
{
    public static function create(Messenger $messenger, LoopInterface $loop)
    {
        $messenger->registerRpc('executeQuery', function (Payload $payload) {
            return resolve([
                'response' => 'works'
            ]);
        });
    }
}

I don't see anything going wrong here. As I see it the promise should resolve.

@WyriHaximus
Copy link
Owner

With those edits above it works for me earlier this week:
image

@WyriHaximus
Copy link
Owner

I'll set up a repo somewhere this weekend that has your code working

@WyriHaximus
Copy link
Owner

Hey @mpjraaij just set up a repository at https://github.com/WyriHaximus/shiny-octo-adventure run composer install and then php run.php and it (should) work. If it doesn't could you run strace php run.php > strace.txt 2>&1 and email strace.txt to the email address listed on my profile?

@factormaarten
Copy link
Author

Thanks a lot! I'll go over it this week and see if it works :) if not I'll send you an email.

@WyriHaximus
Copy link
Owner

👍

@cluigDE
Copy link

cluigDE commented Feb 4, 2020

Hi @WyriHaximus
I tried to get the flexible pool to work by using a custom class. But every time I try the return-class example with a 100% identical copy of the ReturnChild class delivered by the messaging package, I get no resolve at all. (The only difference would be the name)

Also tried this on different environments:
Debian, php7.0-7.4
OSX, php7.0-7.4
7.0 even breaks the whole project, which was kinda obvious.

eventloopserver.php

Flexible::createFromClass(LoopChildProcess::class, $loop)
->then(function (PoolInterface $pool) {
    echo 'tack', PHP_EOL;
    for ($i = 0; $i < 100; $i++) {
        //echo $i, PHP_EOL;

        $pool->rpc(
            MessagesFactory::rpc(
                'return',
                [
                    'i' => $i,
                    'time' => time(),
                    //'string' => str_pad('0', 1024 * 1024 * 5)
                    'string' => str_pad('0', 5)
                ]
            )
        )->then(function (Payload $payload) use ($pool) {
            echo $payload['i'], PHP_EOL;
            echo $payload['time'], PHP_EOL;
            if ($payload['i'] == 99) {
                $pool->terminate();
            }
            var_export($pool->info());
        });
    }
    echo 'tock', PHP_EOL;
});

$loop->run();

I also replaced the then()'s with done()'s, no error and actually just waiting for the promise resolve.

The LoopChildProcess has no namespace and is required the eventloopserver.php.

But the weird thing is everything works perfectly fine if I use:

Flexible::createFromClass('WyriHaximus\React\ChildProcess\Messenger\ReturnChild', $loop)

Anything I'm misunderstanding about the usage? Thanks for the help in advance.

@WyriHaximus
Copy link
Owner

hey @cluigDE silly question but is LoopChildProcess autoloaded?

@cluigDE
Copy link

cluigDE commented Feb 4, 2020

No, the file containing the class was just included in the file eventloopserver.php

Didn't thought of autoloading the class in composer. Will do that. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants