Skip to content

Commit

Permalink
TASK: Use correlation id instead of causation id to group adjustment …
Browse files Browse the repository at this point in the history
…events

Applies suggestion originally posted in neos#4969 (comment)

the resulting event log would be similar to:

| event                           | correlation id | metadata                                                                                                                                   |
|---------------------------------|----------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| NodeAggregateWithNodeWasCreated | 123456         | `{structureAdjustment: 'Content Stream: %s; Dimension Space Point: %s, Node Aggregate: %s --- The tethered child node "bar" is missing.'}` |
| NodePropertiesWereSet           | 123456         |                                                                                                                                            |
  • Loading branch information
mhsdesign committed Jan 27, 2025
1 parent 2d22808 commit 4c64a01
Showing 1 changed file with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use Neos\ContentRepository\Core\EventStore\DecoratedEvent;
use Neos\ContentRepository\Core\EventStore\EventInterface;
use Neos\ContentRepository\Core\EventStore\EventNormalizer;
use Neos\ContentRepository\Core\EventStore\Events;
use Neos\ContentRepository\Core\EventStore\EventsToPublish;
use Neos\ContentRepository\Core\Factory\ContentRepositoryServiceInterface;
use Neos\ContentRepository\Core\Infrastructure\Property\PropertyConverter;
use Neos\ContentRepository\Core\NodeType\NodeTypeManager;
use Neos\ContentRepository\Core\NodeType\NodeTypeName;
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentGraphInterface;
use Neos\ContentRepository\Core\SharedModel\Id\UuidFactory;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\ContentRepository\Core\Subscription\Engine\SubscriptionEngine;
use Neos\ContentRepository\StructureAdjustment\Adjustment\DimensionAdjustment;
Expand All @@ -25,8 +25,8 @@
use Neos\ContentRepository\StructureAdjustment\Adjustment\TetheredNodeAdjustments;
use Neos\ContentRepository\StructureAdjustment\Adjustment\UnknownNodeTypeAdjustment;
use Neos\EventStore\EventStoreInterface;
use Neos\EventStore\Model\Event\EventId;
use Neos\EventStore\Model\Event\EventMetadata;
use Neos\EventStore\Model\Event\CorrelationId;
use Neos\EventStore\Model\Events;

class StructureAdjustmentService implements ContentRepositoryServiceInterface
{
Expand Down Expand Up @@ -114,41 +114,31 @@ public function fixError(StructureAdjustment $adjustment): void
$eventsToPublish = $remediation();
assert($eventsToPublish instanceof EventsToPublish);

$eventsWithMetaData = self::eventsWithCausationOfFirstEventAndAdditionalMetaData(
$eventsToPublish->events,
EventMetadata::fromArray([
'structureAdjustment' => mb_strimwidth($adjustment->render() , 0, 250, '')
])
);
// set correlation id and add debug metadata
$correlationId = CorrelationId::fromString(UuidFactory::create());
$isFirstEvent = true;
$normalizedEvents = Events::fromArray($eventsToPublish->events->map(function (EventInterface|DecoratedEvent $event) use (
&$isFirstEvent, $correlationId, $adjustment
) {
$metadata = $event instanceof DecoratedEvent ? $event->eventMetadata?->value ?? [] : [];
if ($isFirstEvent) {
$metadata['debug_structureAdjustment'] = mb_strimwidth($adjustment->render() , 0, 250, '');
$isFirstEvent = false;
}
$decoratedEvent = DecoratedEvent::create(
event: $event,
metadata: $metadata,
correlationId: $correlationId,
);

return $this->eventNormalizer->normalize($decoratedEvent);
}));

$normalizedEvents = \Neos\EventStore\Model\Events::fromArray(
$eventsWithMetaData->map($this->eventNormalizer->normalize(...))
);
$this->eventStore->commit(
$eventsToPublish->streamName,
$normalizedEvents,
$eventsToPublish->expectedVersion
);
$this->subscriptionEngine->catchUpActive();
}

private static function eventsWithCausationOfFirstEventAndAdditionalMetaData(Events $events, EventMetadata $metadata): Events
{
/** @var non-empty-list<EventInterface|DecoratedEvent> $restEvents */
$restEvents = iterator_to_array($events);
$firstEvent = array_shift($restEvents);

if ($firstEvent instanceof DecoratedEvent && $firstEvent->eventMetadata) {
$metadata = EventMetadata::fromArray(array_merge($firstEvent->eventMetadata->value, $metadata->value));
}

$decoratedFirstEvent = DecoratedEvent::create($firstEvent, eventId: EventId::create(), metadata: $metadata);

$decoratedRestEvents = [];
foreach ($restEvents as $event) {
$decoratedRestEvents[] = DecoratedEvent::create($event, causationId: $decoratedFirstEvent->eventId);
}

return Events::fromArray([$decoratedFirstEvent, ...$decoratedRestEvents]);
}
}

0 comments on commit 4c64a01

Please sign in to comment.