Skip to content

Commit

Permalink
TASK: Do not send $commitResult to generator but calculate expected…
Browse files Browse the repository at this point in the history
… version instead

Also readd lost documentation and simplifies the `handle`

The ->throw logic was initially introduced via

neos/neos-development-collection#5315

but then removed again as we thought it was no longer needed.
  • Loading branch information
mhsdesign committed Nov 9, 2024
1 parent 8b55830 commit 725225f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
3 changes: 1 addition & 2 deletions Classes/CommandHandler/CommandHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
namespace Neos\ContentRepository\Core\CommandHandler;

use Neos\ContentRepository\Core\EventStore\EventsToPublish;
use Neos\EventStore\Model\EventStore\CommitResult;

/**
* Common interface for all Content Repository command handlers
*
* The {@see CommandHandlingDependencies} are available during handling to do soft-constraint checks
*
* @phpstan-type YieldedEventsToPublish \Generator<int, EventsToPublish, CommitResult|null, void>
* @phpstan-type YieldedEventsToPublish \Generator<int, EventsToPublish>
* @internal no public API, because commands are no extension points of the CR
*/
interface CommandHandlerInterface
Expand Down
15 changes: 10 additions & 5 deletions Classes/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,28 @@ public function handle(CommandInterface $command): void

// control-flow aware command handling via generator
try {
$yieldedEventsToPublish = $toPublish->current();
while ($yieldedEventsToPublish !== null) {
foreach ($toPublish as $yieldedEventsToPublish) {
if ($yieldedEventsToPublish->events->isEmpty()) {
$yieldedEventsToPublish = $toPublish->send(null);
continue;
}
$eventsToPublish = $this->enrichEventsToPublishWithMetadata($yieldedEventsToPublish);
try {
$commitResult = $this->eventPersister->publishWithoutCatchup($eventsToPublish);
$this->eventPersister->publishWithoutCatchup($eventsToPublish);
} catch (ConcurrencyException $concurrencyException) {
// we pass the exception into the generator (->throw), so it could be try-caught and reacted upon:
//
// try {
// yield EventsToPublish(...);
// } catch (ConcurrencyException $e) {
// yield $this->reopenContentStream();
// throw $e;
// }
$yieldedErrorStrategy = $toPublish->throw($concurrencyException);
if ($yieldedErrorStrategy instanceof EventsToPublish) {
$this->eventPersister->publishWithoutCatchup($yieldedErrorStrategy);
}
throw $concurrencyException;
}
$yieldedEventsToPublish = $toPublish->send($commitResult);
}
} finally {
// We always NEED to catchup even if there was an unexpected ConcurrencyException to make sure previous commits are handled.
Expand Down
35 changes: 19 additions & 16 deletions Classes/Feature/WorkspaceCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,17 @@ static function ($handle) use ($rebaseableCommands): void {
throw WorkspaceRebaseFailed::duringPublish($commandSimulator->getCommandsThatFailed());
}

$eventsOfWorkspaceToPublish = $this->getCopiedEventsOfEventStream(
$baseWorkspace->workspaceName,
$baseWorkspace->currentContentStreamId,
$commandSimulator->eventStream(),
);

try {
$commitResult = yield new EventsToPublish(
yield new EventsToPublish(
ContentStreamEventStreamName::fromContentStreamId($baseWorkspace->currentContentStreamId)
->getEventStreamName(),
$this->getCopiedEventsOfEventStream(
$baseWorkspace->workspaceName,
$baseWorkspace->currentContentStreamId,
$commandSimulator->eventStream(),
),
$eventsOfWorkspaceToPublish,
ExpectedVersion::fromVersion($baseWorkspaceContentStreamVersion)
);
} catch (ConcurrencyException $concurrencyException) {
Expand All @@ -268,7 +270,7 @@ static function ($handle) use ($rebaseableCommands): void {
yield $this->forkContentStream(
$newContentStreamId,
$baseWorkspace->currentContentStreamId,
$commitResult->highestCommittedVersion
Version::fromInteger($baseWorkspaceContentStreamVersion->value + $eventsOfWorkspaceToPublish->count())
);

yield new EventsToPublish(
Expand Down Expand Up @@ -507,16 +509,18 @@ static function ($handle) use ($commandSimulator, $matchingCommands, $remainingC
throw WorkspaceRebaseFailed::duringPublish($commandSimulator->getCommandsThatFailed());
}

// this could empty and a no-op for the rare case when a command returns empty events e.g. the node was already tagged with this subtree tag
$selectedEventsOfWorkspaceToPublish = $this->getCopiedEventsOfEventStream(
$baseWorkspace->workspaceName,
$baseWorkspace->currentContentStreamId,
$commandSimulator->eventStream()->withMaximumSequenceNumber($highestSequenceNumberForMatching),
);

try {
// this could be a no-op for the rare case when a command returns empty events e.g. the node was already tagged with this subtree tag, meaning we actually just rebase
$commitResult = yield new EventsToPublish(
yield new EventsToPublish(
ContentStreamEventStreamName::fromContentStreamId($baseWorkspace->currentContentStreamId)
->getEventStreamName(),
$this->getCopiedEventsOfEventStream(
$baseWorkspace->workspaceName,
$baseWorkspace->currentContentStreamId,
$commandSimulator->eventStream()->withMaximumSequenceNumber($highestSequenceNumberForMatching),
),
$selectedEventsOfWorkspaceToPublish,
ExpectedVersion::fromVersion($baseWorkspaceContentStreamVersion)
);
} catch (ConcurrencyException $concurrencyException) {
Expand All @@ -529,8 +533,7 @@ static function ($handle) use ($commandSimulator, $matchingCommands, $remainingC
yield from $this->forkNewContentStreamAndApplyEvents(
$command->contentStreamIdForRemainingPart,
$baseWorkspace->currentContentStreamId,
// todo otherwise Features/W8-IndividualNodePublication/03-MoreBasicFeatures.feature:185 fails, see comment about emptiness above ... or should we manually count?
$commitResult?->highestCommittedVersion ?: $baseWorkspaceContentStreamVersion,
Version::fromInteger($baseWorkspaceContentStreamVersion->value + $selectedEventsOfWorkspaceToPublish->count()),
new EventsToPublish(
WorkspaceEventStreamName::fromWorkspaceName($command->workspaceName)->getEventStreamName(),
Events::fromArray([
Expand Down

0 comments on commit 725225f

Please sign in to comment.