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

add tombstone handler support #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion config/ksql.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use ZiffMedia\LaravelKsql\DiscoverResources;
use ZiffMedia\Ksql\ContentType;
use ZiffMedia\LaravelKsql\DiscoverResources;

return [
'endpoint' => env('KSQL_ENDPOINT'),
Expand Down
7 changes: 6 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ZiffMedia\Ksql\Client as KsqlClient;
use ZiffMedia\Ksql\PullQuery;
use ZiffMedia\Ksql\ResultRow;
use ZiffMedia\Ksql\TombstoneRow;

class Client extends KsqlClient
{
Expand All @@ -18,7 +19,11 @@ public function streamAndEmit(array|PushQuery $query): void
}

$handler = function (ResultRow $row) {
event($row->query->event, $row);
if ($row instanceof TombstoneRow) {
event($row->query->tombstoneEvent, $row);
} else {
event($row->query->event, $row);
}
};

foreach ($query as $q) {
Expand Down
1 change: 1 addition & 0 deletions src/ConsumerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function handle()
if ($resource->shouldConsume) {
$query = new PushQuery($resource->getKeyName(), $resource->getKsqlStreamQuery(), fn () => null, $resource->offset);
$query->event = $resource->getEventName();
$query->tombstoneEvent = $resource->getTombstoneEventName();
$streamQueries[] = $query;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/KsqlResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Str;
use ZiffMedia\Ksql\Offset;
use ZiffMedia\Ksql\ResultRow;
use ZiffMedia\Ksql\TombstoneRow;

class KsqlResource
{
Expand All @@ -32,6 +33,10 @@ public function handle(ResultRow $data): void
{
}

public function handleTombstone(TombstoneRow $data): void
{
}

public function getKsqlStreamQuery(): string
{
return sprintf('SELECT * FROM %s EMIT CHANGES;', $this->ksqlTable);
Expand Down Expand Up @@ -60,6 +65,11 @@ public function getEventName(): string
return 'ksql.'.$this->getKeyName();
}

public function getTombstoneEventName(): string
{
return $this->getEventName().'.tombstone';
}

public function getCatchupQuery(): string
{
$latestModel = $this->getLatestModel();
Expand Down
2 changes: 2 additions & 0 deletions src/KsqlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function register()
config('ksql.auth.password')
);
$client->setAcceptContentType(config('ksql.client_content_type', ContentType::V1_DELIMITTED));

return $client;
});
}
Expand All @@ -59,6 +60,7 @@ private function discoverResources()
$instance = new $className;
app(ResourceManager::class)->{$instance->getKeyName()} = $instance;
Event::listen($instance->getEventName(), [$instance, 'handle']);
Event::listen($instance->getTombstoneEventName(), [$instance, 'handleTombstone']);
});
}
}
2 changes: 2 additions & 0 deletions src/PushQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
class PushQuery extends KsqlPushQuery
{
public string $event;

public string $tombstoneEvent;
}
5 changes: 5 additions & 0 deletions tests/KsqlResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,8 @@ public function getCatchupQuery(): string
$kr = new KsqlResource();
expect($kr->getEventName())->toBe('ksql.ksql_resource');
});

test('it should generate tombstone event names', function () {
$kr = new KsqlResource();
expect($kr->getTombstoneEventName())->toBe('ksql.ksql_resource.tombstone');
});