Skip to content

PHPLIB-920: Command monitoring and change stream examples #976

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

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions examples/changestream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

namespace MongoDB\Examples;

require '../vendor/autoload.php';

use MongoDB\Client;

use function assert;
use function fprintf;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;
use function time;

use const STDERR;

function toJSON(object $document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
}

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$collection = $client->test->coll;
$collection->drop();

$changeStream = $collection->watch();

$documents = [];

for ($i = 0; $i < 10; $i++) {
$documents[] = ['x' => $i];
}

$collection->insertMany($documents);

$changeStream->rewind();

$startTime = time();

while (true) {
if ($changeStream->valid()) {
$event = $changeStream->current();
assert(is_object($event));
printf("%s\n", toJSON($event));
}

$changeStream->next();

if (time() - $startTime > 3) {
fprintf(STDERR, "Aborting after 3 seconds...\n");
break;
}
}
84 changes: 84 additions & 0 deletions examples/command_logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);

namespace MongoDB\Examples;

require '../vendor/autoload.php';

use MongoDB\Client;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;

use function assert;
use function fprintf;
use function get_class;
use function getenv;
use function is_object;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toRelaxedExtendedJSON;
use function printf;

use const STDERR;

function toJSON(object $document): string
{
return toRelaxedExtendedJSON(fromPHP($document));
}

// phpcs:disable Squiz.Classes.ClassFileName.NoMatch
class CommandLogger implements CommandSubscriber
{
public function commandStarted(CommandStartedEvent $event): void
{
fprintf(STDERR, "%s command started\n", $event->getCommandName());

fprintf(STDERR, "command: %s\n", toJson($event->getCommand()));
fprintf(STDERR, "\n");
}

public function commandSucceeded(CommandSucceededEvent $event): void
{
fprintf(STDERR, "%s command succeeded\n", $event->getCommandName());
fprintf(STDERR, "reply: %s\n", toJson($event->getReply()));
fprintf(STDERR, "\n");
}

public function commandFailed(CommandFailedEvent $event): void
{
fprintf(STDERR, "%s command failed\n", $event->getCommandName());
fprintf(STDERR, "reply: %s\n", toJson($event->getReply()));

$exception = $event->getError();
fprintf(STDERR, "exception: %s\n", get_class($exception));
fprintf(STDERR, "exception.code: %d\n", $exception->getCode());
fprintf(STDERR, "exception.message: %s\n", $exception->getMessage());
fprintf(STDERR, "\n");
}
}

$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');

$client->getManager()->addSubscriber(new CommandLogger());

$collection = $client->test->coll;
$collection->drop();

$collection->insertMany([
['x' => 1],
['x' => 2],
['x' => 3],
]);

$collection->updateMany(
['x' => ['$gt' => 1]],
['$set' => ['y' => 1]]
);

$cursor = $collection->find([], ['batchSize' => 2]);

foreach ($cursor as $document) {
assert(is_object($document));
printf("%s\n", toJSON($document));
}