Skip to content

Commit

Permalink
TASK: WIP Prepare synchronous cr
Browse files Browse the repository at this point in the history
- `ContentRepository::handle()` will block by default
  • Loading branch information
mhsdesign committed May 13, 2024
1 parent 7c5784f commit 18f200f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,23 @@
namespace Neos\ContentRepository\Core\CommandHandler;

use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\Projection\ProjectionInterface;
use Neos\ContentRepository\Core\Projection\ProjectionStateInterface;
use Neos\EventStore\Model\Event\SequenceNumber;
use Neos\EventStore\Model\Event\Version;
use Neos\EventStore\Model\EventStore\CommitResult;

/**
* Result of the {@see ContentRepository::handle()} method to be able to block until the projections were updated.
* Was the result of the {@see ContentRepository::handle()} method.
* Previously one would need this to be able to block until the projections were updated.
*
* {@see PendingProjections} for a detailed explanation how the blocking works.
* This will no longer be required in the future see https://github.com/neos/neos-development-collection/pull/4988
*
* @deprecated this b/c layer will be removed with the next beta or before Neos 9 final release
* @api
*/
final readonly class CommandResult
{
public function __construct(
private PendingProjections $pendingProjections,
public CommitResult $commitResult,
) {
}

/**
* an empty command result which should not result in projection updates
* @return self
*/
public static function empty(): self
{
return new self(
PendingProjections::empty(),
new CommitResult(
Version::first(),
SequenceNumber::none()
)
);
}

/**
* Wait until all projections are up to date; i.e. have processed the events.
*
* @return void
* @api
* We block by default thus you must not call this method or use this legacy stub
* @deprecated this b/c layer will be removed with the next beta or before Neos 9 final release
*/
public function block(): void
{
foreach ($this->pendingProjections->projections as $pendingProjection) {
$expectedSequenceNumber = $this->pendingProjections->getExpectedSequenceNumber($pendingProjection);
$this->blockProjection($pendingProjection, $expectedSequenceNumber);
}
}

/**
* @param ProjectionInterface<ProjectionStateInterface> $projection
*/
private function blockProjection(ProjectionInterface $projection, SequenceNumber $expectedSequenceNumber): void
{
$attempts = 0;
while ($projection->getCheckpointStorage()->getHighestAppliedSequenceNumber()->value < $expectedSequenceNumber->value) {
usleep(50000); // 50000μs = 50ms
if (++$attempts > 100) { // 5 seconds
throw new \RuntimeException(
sprintf(
'TIMEOUT while waiting for projection "%s" to catch up to sequence number %d ' .
'- check the error logs for details.',
$projection::class,
$expectedSequenceNumber->value
),
1550232279
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Neos\ContentRepository\Core\Projection\WithMarkStaleInterface;
use Neos\EventStore\EventStoreInterface;
use Neos\EventStore\Exception\ConcurrencyException;
use Neos\EventStore\Model\Event;
use Neos\EventStore\Model\Event\EventId;
use Neos\EventStore\Model\Events;

/**
Expand All @@ -39,7 +37,7 @@ public function __construct(
public function publishEvents(EventsToPublish $eventsToPublish): CommandResult
{
if ($eventsToPublish->events->isEmpty()) {
return CommandResult::empty();
return new CommandResult();
}
// the following logic could also be done in an AppEventStore::commit method (being called
// directly from the individual Command Handlers).
Expand All @@ -66,8 +64,6 @@ public function publishEvents(EventsToPublish $eventsToPublish): CommandResult
}
}
$this->projectionCatchUpTrigger->triggerCatchUp($pendingProjections->projections);

// The CommandResult can be used to block until projections are up to date.
return new CommandResult($pendingProjections, $commitResult);
return new CommandResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* We will hopefully get rid of this class at some point; by introducing a NodeAggregate
* which will take care of constraint enforcement then.
*
* @deprecated remove me https://github.com/neos/neos-development-collection/pull/4988
* @internal
*/
class CatchUpTriggerWithSynchronousOption implements ProjectionCatchUpTriggerInterface
Expand All @@ -33,7 +34,10 @@ class CatchUpTriggerWithSynchronousOption implements ProjectionCatchUpTriggerInt
*/
protected $contentRepositoryRegistry;

private static bool $synchronousEnabled = false;
/**
* Hack by setting to true to be always sync mode: https://github.com/neos/neos-development-collection/pull/4988
*/
private static bool $synchronousEnabled = true;

/**
* INTERNAL
Expand Down

0 comments on commit 18f200f

Please sign in to comment.