Rxnet / eventstore-client

EventStore asynchronous PHP client with reactiveX flavours
Apache License 2.0
26 stars 10 forks source link

How to disconnect #19

Closed ontehfritz closed 6 years ago

ontehfritz commented 6 years ago

I am trying to use your library in service, I have this method that I am calling in a test:

function raiseEvents(array $events, $stream){
        $eventStore = new EventStore();
        $eventStore->connect($this->connectionString)
            ->subscribe(function () use ($eventStore, $stream, $jsonEvents) {
                $eventStore->write($stream, $jsonEvents);
        });

    }

The test run successfully but something keeps the test runner from exiting, I have to manually stop the test runner. Is there something running in the background, and how do I kill it.

callistino commented 6 years ago

The connect method returns an observable and every observable returns a DisposableInterface once subscribed to it. Try saving the "disposable" on a class property and call dispose on tearDown.

Vinceveve commented 6 years ago

@ontehfritz I've added disconnect method. The method by @callistino is what I usually use.

For one shot event firing, I think HTTP is better suited a simple curl can work

function store_event($stream, $eventType, $data)
{
    $eventStoreUrl = 'http://' . $_SERVER['EVENTSTORE_USERNAME'] . ':' . $_SERVER['EVENTSTORE_PASSWORD'] . '@' . $_SERVER['EVENTSTORE_SERVICE_HOST'] . ':2113/streams/' . $stream;
    //$eventStoreUrl = 'http://user:passord@127.0.0.1:2113/streams/'.$stream;
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $eventStoreUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $headers = [
        'Content-Type: application/json',
        "ES-EventType: $eventType",
    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $server_output = curl_exec($ch);
    $info = curl_getinfo($ch);

    if ($info['http_code'] >= 400) {
        throw new Exception("Saving $eventType in eventstore got Invalid status code : {$info['http_code']}");
    }
    //var_dump($server_output, $info);

    curl_close($ch);
    return $server_output;
}
ontehfritz commented 6 years ago

Great thanks for doing this!

I will check out the disconnect functionality, the only problem with using the curl way is that it is synchronous, I wanted to use a fire and forget functionality. However, in this case that I am specifically implementing, I need to know if it was successful, so the curl way maybe alright in the short term.

Vinceveve commented 6 years ago

if you want to keep asynchronous :

or have fun with curl_multi

ontehfritz commented 6 years ago

Oh sweet, thanks for the links!