Skip to content

Commit

Permalink
Merge pull request #588 from patchlevel/trigger-subscription-engine-e…
Browse files Browse the repository at this point in the history
…vent-bus

Add RunSubscriptionEngineRepository
  • Loading branch information
DavidBadura authored Apr 23, 2024
2 parents 8dfa057 + 773908d commit 83a4979
Show file tree
Hide file tree
Showing 14 changed files with 844 additions and 271 deletions.
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

0 comments on commit 83a4979

Please sign in to comment.