-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGECLOUD-4071: Re-work consumers to terminate as soon as there is no…
…thing left to process (#594)
- Loading branch information
1 parent
ec1f037
commit 68f81e0
Showing
14 changed files
with
605 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
patches/MAGECLOUD-4071__terminate_consumers_if_the_queue_is_empty__2.2.0.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
diff -Naur a/vendor/magento/framework-message-queue/CallbackInvoker.php b/vendor/magento/framework-message-queue/CallbackInvoker.php | ||
--- a/vendor/magento/framework-message-queue/CallbackInvoker.php | ||
+++ b/vendor/magento/framework-message-queue/CallbackInvoker.php | ||
@@ -6,11 +6,28 @@ | ||
|
||
namespace Magento\Framework\MessageQueue; | ||
|
||
+use Magento\Framework\App\DeploymentConfig; | ||
+ | ||
/** | ||
* Class CallbackInvoker to invoke callbacks for consumer classes | ||
*/ | ||
class CallbackInvoker | ||
{ | ||
+ /** | ||
+ * @var DeploymentConfig | ||
+ */ | ||
+ private $deploymentConfig; | ||
+ | ||
+ /** | ||
+ * CallbackInvoker constructor. | ||
+ * @param DeploymentConfig $deploymentConfig | ||
+ */ | ||
+ public function __construct( | ||
+ DeploymentConfig $deploymentConfig | ||
+ ) { | ||
+ $this->deploymentConfig = $deploymentConfig; | ||
+ } | ||
+ | ||
/** | ||
* Run short running process | ||
* | ||
@@ -24,8 +41,23 @@ class CallbackInvoker | ||
for ($i = $maxNumberOfMessages; $i > 0; $i--) { | ||
do { | ||
$message = $queue->dequeue(); | ||
- } while ($message === null && (sleep(1) === 0)); | ||
+ } while ($message === null && $this->isWaitingNextMessage() && (sleep(1) === 0)); | ||
+ | ||
+ if ($message === null) { | ||
+ break; | ||
+ } | ||
+ | ||
$callback($message); | ||
} | ||
} | ||
+ | ||
+ /** | ||
+ * Checks if consumers should wait for message from the queue | ||
+ * | ||
+ * @return bool | ||
+ */ | ||
+ private function isWaitingNextMessage(): bool | ||
+ { | ||
+ return $this->deploymentConfig->get('queue/consumers_wait_for_messages', 1) === 1; | ||
+ } | ||
} | ||
diff -Naur a/vendor/magento/module-message-queue/Setup/ConfigOptionsList.php b/vendor/magento/module-message-queue/Setup/ConfigOptionsList.php | ||
new file mode 100644 | ||
--- /dev/null | ||
+++ b/vendor/magento/module-message-queue/Setup/ConfigOptionsList.php | ||
@@ -0,0 +1,108 @@ | ||
+<?php | ||
+/** | ||
+ * Copyright © Magento, Inc. All rights reserved. | ||
+ * See COPYING.txt for license details. | ||
+ */ | ||
+declare(strict_types=1); | ||
+ | ||
+namespace Magento\MessageQueue\Setup; | ||
+ | ||
+use Magento\Framework\Setup\ConfigOptionsListInterface; | ||
+use Magento\Framework\Setup\Option\SelectConfigOption; | ||
+use Magento\Framework\App\DeploymentConfig; | ||
+use Magento\Framework\Config\Data\ConfigData; | ||
+use Magento\Framework\Config\File\ConfigFilePool; | ||
+ | ||
+/** | ||
+ * Deployment configuration consumers options needed for Setup application | ||
+ */ | ||
+class ConfigOptionsList implements ConfigOptionsListInterface | ||
+{ | ||
+ /** | ||
+ * Input key for the option | ||
+ */ | ||
+ const INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES ='consumers-wait-for-messages'; | ||
+ | ||
+ /** | ||
+ * Path to the value in the deployment config | ||
+ */ | ||
+ const CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES = 'queue/consumers_wait_for_messages'; | ||
+ | ||
+ /** | ||
+ * Default value | ||
+ */ | ||
+ const DEFAULT_CONSUMERS_WAIT_FOR_MESSAGES = 1; | ||
+ | ||
+ /** | ||
+ * The available configuration values | ||
+ * | ||
+ * @var array | ||
+ */ | ||
+ private $selectOptions = [0, 1]; | ||
+ | ||
+ /** | ||
+ * @inheritdoc | ||
+ */ | ||
+ public function getOptions() | ||
+ { | ||
+ return [ | ||
+ new SelectConfigOption( | ||
+ self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, | ||
+ SelectConfigOption::FRONTEND_WIZARD_SELECT, | ||
+ $this->selectOptions, | ||
+ self::CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, | ||
+ 'Should consumers wait for a message from the queue? 1 - Yes, 0 - No', | ||
+ self::DEFAULT_CONSUMERS_WAIT_FOR_MESSAGES | ||
+ ), | ||
+ ]; | ||
+ } | ||
+ | ||
+ /** | ||
+ * @inheritdoc | ||
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
+ */ | ||
+ public function createConfig(array $data, DeploymentConfig $deploymentConfig) | ||
+ { | ||
+ $configData = new ConfigData(ConfigFilePool::APP_ENV); | ||
+ | ||
+ if (!$this->isDataEmpty($data, self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES)) { | ||
+ $configData->set( | ||
+ self::CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, | ||
+ (int)$data[self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES] | ||
+ ); | ||
+ } | ||
+ | ||
+ return [$configData]; | ||
+ } | ||
+ | ||
+ /** | ||
+ * @inheritdoc | ||
+ */ | ||
+ public function validate(array $options, DeploymentConfig $deploymentConfig) | ||
+ { | ||
+ $errors = []; | ||
+ | ||
+ if (!$this->isDataEmpty($options, self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES) | ||
+ && !in_array($options[self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES], $this->selectOptions)) { | ||
+ $errors[] = 'You can use only 1 or 0 for ' . self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES . ' option'; | ||
+ } | ||
+ | ||
+ return $errors; | ||
+ } | ||
+ | ||
+ /** | ||
+ * Check if data ($data) with key ($key) is empty | ||
+ * | ||
+ * @param array $data | ||
+ * @param string $key | ||
+ * @return bool | ||
+ */ | ||
+ private function isDataEmpty(array $data, $key) | ||
+ { | ||
+ if (isset($data[$key]) && $data[$key] !== '') { | ||
+ return false; | ||
+ } | ||
+ | ||
+ return true; | ||
+ } | ||
+} |
181 changes: 181 additions & 0 deletions
181
patches/MAGECLOUD-4071__terminate_consumers_if_the_queue_is_empty__2.3.2.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
diff -Naur a/vendor/magento/framework-message-queue/CallbackInvoker.php b/vendor/magento/framework-message-queue/CallbackInvoker.php | ||
--- a/vendor/magento/framework-message-queue/CallbackInvoker.php | ||
+++ b/vendor/magento/framework-message-queue/CallbackInvoker.php | ||
@@ -8,6 +8,7 @@ namespace Magento\Framework\MessageQueue; | ||
|
||
use Magento\Framework\MessageQueue\PoisonPill\PoisonPillCompareInterface; | ||
use Magento\Framework\MessageQueue\PoisonPill\PoisonPillReadInterface; | ||
+use Magento\Framework\App\DeploymentConfig; | ||
|
||
/** | ||
* Class CallbackInvoker to invoke callbacks for consumer classes | ||
@@ -29,16 +30,24 @@ class CallbackInvoker implements CallbackInvokerInterface | ||
*/ | ||
private $poisonPillCompare; | ||
|
||
+ /** | ||
+ * @var DeploymentConfig | ||
+ */ | ||
+ private $deploymentConfig; | ||
+ | ||
/** | ||
* @param PoisonPillReadInterface $poisonPillRead | ||
* @param PoisonPillCompareInterface $poisonPillCompare | ||
+ * @param DeploymentConfig $deploymentConfig | ||
*/ | ||
public function __construct( | ||
PoisonPillReadInterface $poisonPillRead, | ||
- PoisonPillCompareInterface $poisonPillCompare | ||
+ PoisonPillCompareInterface $poisonPillCompare, | ||
+ DeploymentConfig $deploymentConfig | ||
) { | ||
$this->poisonPillRead = $poisonPillRead; | ||
$this->poisonPillCompare = $poisonPillCompare; | ||
+ $this->deploymentConfig = $deploymentConfig; | ||
} | ||
|
||
/** | ||
@@ -56,13 +65,29 @@ class CallbackInvoker implements CallbackInvokerInterface | ||
do { | ||
$message = $queue->dequeue(); | ||
// phpcs:ignore Magento2.Functions.DiscouragedFunction | ||
- } while ($message === null && (sleep(1) === 0)); | ||
+ } while ($message === null && $this->isWaitingNextMessage() && (sleep(1) === 0)); | ||
+ | ||
+ if ($message === null) { | ||
+ break; | ||
+ } | ||
+ | ||
if (false === $this->poisonPillCompare->isLatestVersion($this->poisonPillVersion)) { | ||
$queue->reject($message); | ||
// phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage | ||
exit(0); | ||
} | ||
+ | ||
$callback($message); | ||
} | ||
} | ||
+ | ||
+ /** | ||
+ * Checks if consumers should wait for message from the queue | ||
+ * | ||
+ * @return bool | ||
+ */ | ||
+ private function isWaitingNextMessage(): bool | ||
+ { | ||
+ return $this->deploymentConfig->get('queue/consumers_wait_for_messages', 1) === 1; | ||
+ } | ||
} | ||
diff -Naur a/vendor/magento/module-message-queue/Setup/ConfigOptionsList.php b/vendor/magento/module-message-queue/Setup/ConfigOptionsList.php | ||
new file mode 100644 | ||
--- /dev/null | ||
+++ b/vendor/magento/module-message-queue/Setup/ConfigOptionsList.php | ||
@@ -0,0 +1,108 @@ | ||
+<?php | ||
+/** | ||
+ * Copyright © Magento, Inc. All rights reserved. | ||
+ * See COPYING.txt for license details. | ||
+ */ | ||
+declare(strict_types=1); | ||
+ | ||
+namespace Magento\MessageQueue\Setup; | ||
+ | ||
+use Magento\Framework\Setup\ConfigOptionsListInterface; | ||
+use Magento\Framework\Setup\Option\SelectConfigOption; | ||
+use Magento\Framework\App\DeploymentConfig; | ||
+use Magento\Framework\Config\Data\ConfigData; | ||
+use Magento\Framework\Config\File\ConfigFilePool; | ||
+ | ||
+/** | ||
+ * Deployment configuration consumers options needed for Setup application | ||
+ */ | ||
+class ConfigOptionsList implements ConfigOptionsListInterface | ||
+{ | ||
+ /** | ||
+ * Input key for the option | ||
+ */ | ||
+ const INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES ='consumers-wait-for-messages'; | ||
+ | ||
+ /** | ||
+ * Path to the value in the deployment config | ||
+ */ | ||
+ const CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES = 'queue/consumers_wait_for_messages'; | ||
+ | ||
+ /** | ||
+ * Default value | ||
+ */ | ||
+ const DEFAULT_CONSUMERS_WAIT_FOR_MESSAGES = 1; | ||
+ | ||
+ /** | ||
+ * The available configuration values | ||
+ * | ||
+ * @var array | ||
+ */ | ||
+ private $selectOptions = [0, 1]; | ||
+ | ||
+ /** | ||
+ * @inheritdoc | ||
+ */ | ||
+ public function getOptions() | ||
+ { | ||
+ return [ | ||
+ new SelectConfigOption( | ||
+ self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, | ||
+ SelectConfigOption::FRONTEND_WIZARD_SELECT, | ||
+ $this->selectOptions, | ||
+ self::CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, | ||
+ 'Should consumers wait for a message from the queue? 1 - Yes, 0 - No', | ||
+ self::DEFAULT_CONSUMERS_WAIT_FOR_MESSAGES | ||
+ ), | ||
+ ]; | ||
+ } | ||
+ | ||
+ /** | ||
+ * @inheritdoc | ||
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
+ */ | ||
+ public function createConfig(array $data, DeploymentConfig $deploymentConfig) | ||
+ { | ||
+ $configData = new ConfigData(ConfigFilePool::APP_ENV); | ||
+ | ||
+ if (!$this->isDataEmpty($data, self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES)) { | ||
+ $configData->set( | ||
+ self::CONFIG_PATH_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES, | ||
+ (int)$data[self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES] | ||
+ ); | ||
+ } | ||
+ | ||
+ return [$configData]; | ||
+ } | ||
+ | ||
+ /** | ||
+ * @inheritdoc | ||
+ */ | ||
+ public function validate(array $options, DeploymentConfig $deploymentConfig) | ||
+ { | ||
+ $errors = []; | ||
+ | ||
+ if (!$this->isDataEmpty($options, self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES) | ||
+ && !in_array($options[self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES], $this->selectOptions)) { | ||
+ $errors[] = 'You can use only 1 or 0 for ' . self::INPUT_KEY_QUEUE_CONSUMERS_WAIT_FOR_MESSAGES . ' option'; | ||
+ } | ||
+ | ||
+ return $errors; | ||
+ } | ||
+ | ||
+ /** | ||
+ * Check if data ($data) with key ($key) is empty | ||
+ * | ||
+ * @param array $data | ||
+ * @param string $key | ||
+ * @return bool | ||
+ */ | ||
+ private function isDataEmpty(array $data, $key) | ||
+ { | ||
+ if (isset($data[$key]) && $data[$key] !== '') { | ||
+ return false; | ||
+ } | ||
+ | ||
+ return true; | ||
+ } | ||
+} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.