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 RunSubscriptionEngineRepository #588

Merged
merged 1 commit into from
Apr 23, 2024
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
1 change: 1 addition & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ deptrac:
- Clock
- Message
- MetadataSubscriber
- Repository
- Schema
- Store
Repository:
Expand Down
18 changes: 7 additions & 11 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer;
use Patchlevel\EventSourcing\Store\DoctrineDbalStore;
use Patchlevel\EventSourcing\Subscription\Engine\DefaultSubscriptionEngine;
use Patchlevel\EventSourcing\Subscription\Repository\RunSubscriptionEngineRepositoryManager;
use Patchlevel\EventSourcing\Subscription\Store\DoctrineSubscriptionStore;
use Patchlevel\EventSourcing\Subscription\Subscriber\MetadataSubscriberAccessorRepository;

Expand Down Expand Up @@ -308,9 +309,12 @@ $engine = new DefaultSubscriptionEngine(
$subscriberRepository,
);

$repositoryManager = new DefaultRepositoryManager(
$aggregateRegistry,
$eventStore,
$repositoryManager = new RunSubscriptionEngineRepositoryManager(
new DefaultRepositoryManager(
$aggregateRegistry,
$eventStore,
),
$engine,
);

$hotelRepository = $repositoryManager->get(Hotel::class);
Expand Down Expand Up @@ -361,7 +365,6 @@ We are now ready to use the Event Sourcing System. We can load, change and save
```php
use Patchlevel\EventSourcing\Aggregate\Uuid;
use Patchlevel\EventSourcing\Repository\Repository;
use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngine;

$hotel1 = Hotel::create(Uuid::generate(), 'HOTEL');
$hotel1->checkIn('David');
Expand All @@ -375,15 +378,8 @@ $hotel2 = $hotelRepository->load(Uuid::fromString('d0d0d0d0-d0d0-d0d0-d0d0-d0d0d
$hotel2->checkIn('David');
$hotelRepository->save($hotel2);

/** @var SubscriptionEngine $engine */
$engine->run();

$hotels = $hotelProjection->getHotels();
```
!!! warning

You need to run the subscription engine to update the projections and execute the processors.

!!! note

You can also use other forms of IDs such as uuid version 6 or a custom format.
Expand Down
41 changes: 39 additions & 2 deletions docs/pages/subscription.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ $catchupSubscriptionEngine = new CatchUpSubscriptionEngine($subscriptionEngine);

You can use the `CatchUpSubscriptionEngine` in your tests to process the events immediately.

### Throw by error Subscription Engine
### Throw on error Subscription Engine

This is another decorator for the subscription engine. It throws an exception if a subscription is in error state.
This is useful for testing or development to get directly feedback if something is wrong.
Expand All @@ -691,13 +691,50 @@ use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngine;
use Patchlevel\EventSourcing\Subscription\Engine\ThrowOnErrorSubscriptionEngine;

/** @var SubscriptionEngine $subscriptionStore */
$throwByErrorSubscriptionEngine = new ThrowOnErrorSubscriptionEngine($subscriptionEngine);
$throwOnErrorSubscriptionEngine = new ThrowOnErrorSubscriptionEngine($subscriptionEngine);
```
!!! warning

This is only for testing or development. Don't use it in production.
The subscription engine has an build in retry strategy to retry subscriptions that have failed.

### Run Subscription Engine after save

You can trigger the subscription engine after calling the `save` method on the repository.
This means that a worker to run the subscriptions are not needed.

```php
use Patchlevel\EventSourcing\Repository\RepositoryManager;
use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngine;
use Patchlevel\EventSourcing\Subscription\Repository\RunSubscriptionEngineRepositoryManager;

/**
* @var SubscriptionEngine $subscriptionEngine
* @var RepositoryManager $defaultRepositoryManager
*/
$eventBus = new RunSubscriptionEngineRepositoryManager(
$defaultRepositoryManager,
$subscriptionEngine,
['id1', 'id2'], // filter subscribers by id
['group1', 'group2'], // filter subscribers by group
100, // limit the number of messages
);
```
!!! danger

By using this, you can't wrap the repository in a transaction.
A rollback is not supported and can break the subscription engine.
Internally, the events are saved in a transaction to ensure data consistency.

!!! note

More about repository manager and repository can be found [here](./repository.md).

!!! tip

You can perfectly use it in development or testing.
Especially in combination with the `CatchUpSubscriptionEngine` and `ThrowOnErrorSubscriptionEngine` decorators.

## Usage

The Subscription Engine has a few methods needed to use it effectively.
Expand Down
15 changes: 15 additions & 0 deletions src/Subscription/Engine/AlreadyProcessing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Subscription\Engine;

use RuntimeException;

final class AlreadyProcessing extends RuntimeException
{
public function __construct()
{
parent::__construct('Subscription engine is already processing');
}
}
Loading
Loading