-
Notifications
You must be signed in to change notification settings - Fork 1
FRW-11561 duplications and deadlock fix #98
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,12 @@ | |
| use Spryker\Zed\EventBehavior\Persistence\Propel\Behavior\EventBehavior; | ||
| use Spryker\Zed\Kernel\Persistence\EntityManager\InstancePoolingTrait; | ||
| use Spryker\Zed\Kernel\RequestIdentifier; | ||
| use Spryker\Zed\Propel\Persistence\BatchProcessor\ActiveRecordBatchProcessorTrait; | ||
|
|
||
| class TriggerManager implements TriggerManagerInterface | ||
| { | ||
| use InstancePoolingTrait; | ||
| use ActiveRecordBatchProcessorTrait; | ||
|
|
||
| /** | ||
| * @uses \Orm\Zed\EventBehavior\Persistence\Map\SpyEventBehaviorEntityChangeTableMap::TABLE_NAME | ||
|
|
@@ -67,7 +69,7 @@ class TriggerManager implements TriggerManagerInterface | |
| /** | ||
| * @var bool|null | ||
| */ | ||
| protected static $eventBehaviorTableExists; | ||
| protected static ?bool $eventBehaviorTableExists = null; | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Facade\EventBehaviorToEventInterface $eventFacade | ||
|
|
@@ -98,45 +100,24 @@ public function __construct( | |
| */ | ||
| public function triggerRuntimeEvents() | ||
| { | ||
| if (static::$eventBehaviorTableExists === false) { | ||
| return; | ||
| } | ||
|
|
||
| if (!$this->config->getEventBehaviorTriggeringStatus()) { | ||
| return; | ||
| } | ||
|
|
||
| $processId = RequestIdentifier::getRequestId(); | ||
| if (!$this->eventBehaviorTableExists()) { | ||
| static::$eventBehaviorTableExists = false; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $triggeredEvents = 0; | ||
| $processId = RequestIdentifier::getRequestId(); | ||
|
|
||
| $limit = $this->config->getTriggerChunkSize(); | ||
| $primaryKeys = []; | ||
| $offset = 0; | ||
| $countEvents = 0; | ||
|
|
||
| do { | ||
| $events = $this->getEventEntitiesByProcessId($processId, $offset, $limit); | ||
| static::$eventBehaviorTableExists = true; | ||
| $events = $this->getEventEntitiesByProcessId($processId, $limit); | ||
| $currentCountEvents = count($events); | ||
| $countEvents += $currentCountEvents; | ||
|
|
||
| $triggeredEvents += $this->triggerEvents($events); | ||
| $primaryKeys = array_merge($primaryKeys, $this->getPrimaryKeys($events)); | ||
| $offset += $limit; | ||
| $this->triggerEventsAndDelete($events); | ||
| } while ($currentCountEvents === $limit); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing limit adding offset pulling makes this loop not needed. But do we really need to remove the limit? It might be there to not process a lot of events at once. |
||
|
|
||
| if ($countEvents === $triggeredEvents && $triggeredEvents > 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is here for some events that were not processed for some reason and we need to keep them in DB so the console command can pick the up later |
||
| $this->eventBehaviorEntityManager->deleteEventBehaviorEntityByProcessId($processId); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $this->eventBehaviorEntityManager->deleteEventBehaviorEntityByPrimaryKeys($primaryKeys); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -149,7 +130,7 @@ public function triggerRuntimeEventsWithReport(): EventTriggerResponseTransfer | |
| $eventTriggerResponseTransfer->setEventBehaviorTableExists(true); | ||
| $eventTriggerResponseTransfer->setIsEventTriggeringActive(true); | ||
|
|
||
| if (static::$eventBehaviorTableExists === false) { | ||
| if ($this->eventBehaviorTableExists() === false) { | ||
| $eventTriggerResponseTransfer->setMessage('Event behavior table does not exist.'); | ||
| $eventTriggerResponseTransfer->setEventBehaviorTableExists(false); | ||
|
|
||
|
|
@@ -163,25 +144,10 @@ public function triggerRuntimeEventsWithReport(): EventTriggerResponseTransfer | |
| return $eventTriggerResponseTransfer; | ||
| } | ||
|
|
||
| if (!$this->eventBehaviorTableExists()) { | ||
| static::$eventBehaviorTableExists = false; | ||
| $eventTriggerResponseTransfer->setMessage('Event behavior table does not exist.'); | ||
| $eventTriggerResponseTransfer->setEventBehaviorTableExists(false); | ||
|
|
||
| return $eventTriggerResponseTransfer; | ||
| } | ||
|
|
||
| $requestId = RequestIdentifier::getRequestId(); | ||
|
|
||
| $eventTriggerResponseTransfer->setRequestId($requestId); | ||
|
|
||
| $events = $this->queryContainer->queryEntityChange($requestId)->find()->getData(); | ||
| $eventTriggerResponseTransfer->setEventCount(count($events)); | ||
|
|
||
| static::$eventBehaviorTableExists = true; | ||
|
|
||
| $triggeredRows = $this->triggerEvents($events); | ||
|
|
||
| $deletedRows = 0; | ||
| $limit = $this->config->getTriggerChunkSize(); | ||
| do { | ||
|
|
@@ -191,8 +157,9 @@ public function triggerRuntimeEventsWithReport(): EventTriggerResponseTransfer | |
| $deletedRows += $this->triggerEventsAndDelete($events); | ||
| } while ($countEvents === $limit); | ||
|
|
||
| $eventTriggerResponseTransfer->setTriggeredRows($triggeredRows); | ||
| $eventTriggerResponseTransfer->setTriggeredRows($deletedRows); | ||
| $eventTriggerResponseTransfer->setDeletedRows($deletedRows); | ||
| $eventTriggerResponseTransfer->setEventCount($deletedRows); | ||
|
|
||
| $eventTriggerResponseTransfer->setIsSuccessful(true); | ||
|
|
||
|
|
@@ -223,20 +190,19 @@ public function triggerLostEvents() | |
|
|
||
| /** | ||
| * @param string $processId | ||
| * @param int $offset | ||
| * @param int $limit | ||
| * | ||
| * @return array<\Orm\Zed\EventBehavior\Persistence\SpyEventBehaviorEntityChange> | ||
| */ | ||
| protected function getEventEntitiesByProcessId(string $processId, int $offset, int $limit): array | ||
| protected function getEventEntitiesByProcessId(string $processId, int $limit): array | ||
| { | ||
| $instancePoolingDisabled = false; | ||
| if ($this->isInstancePoolingEnabled()) { | ||
| $this->disableInstancePooling(); | ||
| $instancePoolingDisabled = true; | ||
| } | ||
|
|
||
| $events = $this->queryContainer->queryEntityChange($processId)->setOffset($offset)->limit($limit)->find()->getData(); | ||
| $events = $this->queryContainer->queryEntityChange($processId)->limit($limit)->find()->getData(); | ||
|
|
||
| if ($instancePoolingDisabled) { | ||
| $this->enableInstancePooling(); | ||
|
|
@@ -249,21 +215,36 @@ protected function getEventEntitiesByProcessId(string $processId, int $offset, i | |
| return $events; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<\Orm\Zed\EventBehavior\Persistence\SpyEventBehaviorEntityChange> $events | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function deleteteEventEntities(array $events): void | ||
| { | ||
| foreach ($events as $event) { | ||
| $this->remove($event); | ||
| } | ||
|
|
||
| $this->commit(); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<\Orm\Zed\EventBehavior\Persistence\SpyEventBehaviorEntityChange> $events | ||
| * | ||
| * @return int | ||
| */ | ||
| protected function triggerEventsAndDelete(array $events): int | ||
| { | ||
| $primaryKeys = $this->getPrimaryKeys($events); | ||
| if (!$events) { | ||
| return 0; | ||
| } | ||
|
|
||
| $triggeredRows = $this->triggerEvents($events); | ||
|
|
||
| if ($triggeredRows !== 0 && count($events) === $triggeredRows) { | ||
| return $this->eventBehaviorEntityManager->deleteEventBehaviorEntityByPrimaryKeys($primaryKeys); | ||
| } | ||
| $this->deleteteEventEntities($events); | ||
|
|
||
| return 0; | ||
| return $triggeredRows; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -316,27 +297,12 @@ protected function triggerEvents(array $events): int | |
| */ | ||
| protected function eventBehaviorTableExists(): bool | ||
| { | ||
| return class_exists(BaseSpyEventBehaviorEntityChangeQuery::class) | ||
| && class_exists(SpyEventBehaviorEntityChangeQuery::class) | ||
| && $this->propelFacade->tableExists(static::TABLE_NAME_EVENT_BEHAVIOR_ENTITY_CHANGE); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<\Orm\Zed\EventBehavior\Persistence\SpyEventBehaviorEntityChange> $events | ||
| * | ||
| * @return array<int> | ||
| */ | ||
| protected function getPrimaryKeys(array $events): array | ||
| { | ||
| $keys = []; | ||
|
|
||
| foreach ($events as $event) { | ||
| /** @var int $idEventBehaviorEntityChange */ | ||
| $idEventBehaviorEntityChange = $event->getIdEventBehaviorEntityChange(); | ||
|
|
||
| $keys[] = $idEventBehaviorEntityChange; | ||
| if (static::$eventBehaviorTableExists === null) { | ||
| static::$eventBehaviorTableExists = class_exists(BaseSpyEventBehaviorEntityChangeQuery::class) | ||
| && class_exists(SpyEventBehaviorEntityChangeQuery::class) | ||
| && $this->propelFacade->tableExists(static::TABLE_NAME_EVENT_BEHAVIOR_ENTITY_CHANGE); | ||
| } | ||
|
|
||
| return $keys; | ||
| return static::$eventBehaviorTableExists; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this what a bit worries me. We set this property to true at some point. In the new implementation I can't see that. TBH I don't remember why we did this way, but this changes might break existing behaviour