Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"spryker/event-dispatcher-extension": "^1.0.0",
"spryker/kernel": "^3.72.0",
"spryker/laminas": "^1.0.0",
"spryker/propel": "^3.45.0",
"spryker/propel": "^3.47.0",
"spryker/propel-orm": "^1.16.0",
"spryker/symfony": "^3.1.0",
"spryker/transfer": "^3.39.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ protected function filterPluginsByResources(array $eventResourcePlugins, array $
if (!isset($eventResourcePlugins[$resource])) {
throw new EventResourceNotFoundException(
sprintf(
'There is no resource with the name: %s.',
'There is no resource with the name: %s. Available resources are: %s',
$resource,
implode(', ', array_keys($eventResourcePlugins)),
),
);
}
Expand Down
108 changes: 37 additions & 71 deletions src/Spryker/Zed/EventBehavior/Business/Model/TriggerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Copy link
Contributor

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

$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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
And this is also makes the config value obsolete and this sounds a bit BC breaky to me


if ($countEvents === $triggeredEvents && $triggeredEvents > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}

/**
Expand All @@ -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);

Expand All @@ -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 {
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
Loading