Skip to content

Commit e50bccb

Browse files
Merge pull request #91 from spryker/feature/cc-33982/dev-ps-optimization
P&S optimisation
2 parents febd6aa + 1c8b266 commit e50bccb

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/Spryker/Shared/EventBehavior/EventBehaviorConstants.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,15 @@ interface EventBehaviorConstants
5151
* @var string
5252
*/
5353
public const ENABLE_INSTANCE_POOLING = 'EVENT_BEHAVIOR:ENABLE_INSTANCE_POOLING';
54+
55+
/**
56+
* Specification:
57+
* - Recommended maximum data size for event messages in KB.
58+
* - Used to log a warning if the event message data size exceeds this limit.
59+
*
60+
* @api
61+
*
62+
* @var string
63+
*/
64+
public const MAX_EVENT_MESSAGE_DATA_SIZE = 'EVENT_BEHAVIOR:MAX_EVENT_MESSAGE_DATA_SIZE';
5465
}

src/Spryker/Zed/EventBehavior/EventBehaviorConfig.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,18 @@ public static function isEventBehaviorDisabled()
111111
{
112112
return static::$isEventDisabled;
113113
}
114+
115+
/**
116+
* Specification:
117+
* - Recommended maximum data size for event messages in KB.
118+
* - Used to log a warning if the event message data size exceeds this limit.
119+
*
120+
* @api
121+
*
122+
* @return int
123+
*/
124+
public function getMaxEventMessageDataSize(): int
125+
{
126+
return $this->get(EventBehaviorConstants::MAX_EVENT_MESSAGE_DATA_SIZE, 256);
127+
}
114128
}

src/Spryker/Zed/EventBehavior/Persistence/Propel/Behavior/EventBehavior.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
use Propel\Generator\Model\Table;
1414
use Propel\Generator\Util\PhpParser;
1515
use Propel\Runtime\Exception\PropelException;
16+
use Spryker\Zed\Kernel\BundleConfigResolverAwareTrait;
1617

18+
/**
19+
* @method \Spryker\Zed\EventBehavior\EventBehaviorConfig getConfig()
20+
*/
1721
class EventBehavior extends Behavior
1822
{
23+
use BundleConfigResolverAwareTrait;
24+
1925
/**
2026
* @var string
2127
*/
@@ -427,6 +433,8 @@ protected function getForeignKeys()
427433
*/
428434
protected function addSaveEventBehaviorEntityChangeMethod()
429435
{
436+
$maxEventMessageDataSize = $this->getConfig()->getMaxEventMessageDataSize();
437+
430438
return "
431439
/**
432440
* @param array \$data
@@ -435,10 +443,24 @@ protected function addSaveEventBehaviorEntityChangeMethod()
435443
*/
436444
protected function saveEventBehaviorEntityChange(array \$data)
437445
{
446+
\$encodedData = json_encode(\$data);
447+
\$dataLength = strlen(\$encodedData);
448+
449+
if (\$dataLength > $maxEventMessageDataSize * 1024) {
450+
\$warningMessage = sprintf(
451+
'%s event message data size (%d KB) exceeds the allowable limit of %d KB. Please reduce the event message size or it might disrupt P&S process.',
452+
(\$data['event'] ?? ''),
453+
\$dataLength / 1024,
454+
$maxEventMessageDataSize,
455+
);
456+
457+
\$this->log(\$warningMessage, \\Propel\\Runtime\\Propel::LOG_WARNING);
458+
}
459+
438460
\$isInstancePoolingDisabledSuccessfully = \\Propel\\Runtime\\Propel::disableInstancePooling();
439461
440462
\$spyEventBehaviorEntityChange = new \\Orm\\Zed\\EventBehavior\\Persistence\\SpyEventBehaviorEntityChange();
441-
\$spyEventBehaviorEntityChange->setData(json_encode(\$data));
463+
\$spyEventBehaviorEntityChange->setData(\$encodedData);
442464
\$spyEventBehaviorEntityChange->setProcessId(\\Spryker\\Zed\\Kernel\\RequestIdentifier::getRequestId());
443465
\$spyEventBehaviorEntityChange->save();
444466

0 commit comments

Comments
 (0)