Edit in GitHubLog an issue

Handling outdated in-memory object states

When Adobe Commerce or Magento Open Source is launched, the store's configuration is loaded into memory. The application uses the in-memory configuration for queue message transactions. When the store's configuration is updated in Admin, the copy in memory does not refresh automatically, resulting in an outdated in-memory object state.

To ensure the copy in memory is re-instantiated after an update, use the PoisonPill interfaces available in the MessageQueue module. There are three type of the PoisonPill interfaces:

  • Magento\Framework\MessageQueue\PoisonPill\PoisonPillPutInterface - the interface contains the put method. An implementation of the method puts a new version of poison pill inside the database.
  • Magento\Framework\MessageQueue\PoisonPill\PoisonPillReadInterface - the interface contains the getLatestVersion method. An implementation of the method gets and returns get latest version of the poison pill.
  • Magento\Framework\MessageQueue\PoisonPill\PoisonPillCompareInterface - the interface contains the isLatestVersion method. An implementation of the method checks if the version of current poison pill is the latest.

Before a new message is processed, the PoisonPillCompareInterface compares the version of the in-memory copy to the latest in the database. If the versions are different, the in-memory copy is destroyed and a new copy is created when the next cron:run job executes.

In addition to changes in the configuration, a new store view or a new website can also trigger the PoisonPill interface.

If PoisonPill determines the copy of the in-memory state needs to be re-instantiated and you have set up a consumers_runner cron job, the application will automatically restart all consumers on the next run of the job. If you did not set up the cron job, you will need to manually restart any consumers that were terminated by PoisonPill.

How to use PoisonPill interfaces#

The method put of PoisonPillPutInterface using in the Magento\Store\Model\Website

Copied to your clipboard
1 /**
2 * Clear configuration cache after creation website
3 * @return $this
4 * @since 100.2.0
5 */
6 public function afterSave()
7 {
8 ...
9 $this->pillPut->put();
10 return parent::afterSave();
11 }

The method getLatestVersion of PoisonPillReadInterface using in the Magento\Framework\MessageQueue\CallbackInvoker:

Copied to your clipboard
1 /**
2 * Run short running process
3 * @param QueueInterface $queue
4 * @param int $maxNumberOfMessages
5 * @param \Closure $callback
6 * @return void
7 */
8 public function invoke(QueueInterface $queue, $maxNumberOfMessages, $callback)
9 {
10 $this->poisonPillVersion = $this->poisonPillRead->getLatestVersion();
11 ...
12 }

The method isLatestVersion of PoisonPillCompareInterface using in the Magento\Framework\MessageQueue\CallbackInvoker:

Copied to your clipboard
1 /**
2 * Run short running process
3 *
4 * @param QueueInterface $queue
5 * @param int $maxNumberOfMessages
6 * @param \Closure $callback
7 * @return void
8 */
9 public function invoke(QueueInterface $queue, $maxNumberOfMessages, $callback)
10 {
11 $this->poisonPillVersion = $this->poisonPillRead->getLatestVersion();
12 for ($i = $maxNumberOfMessages; $i > 0; $i--) {
13 do {
14 $message = $queue->dequeue();
15 // phpcs:ignore Magento2.Functions.DiscouragedFunction
16 } while ($message === null && (sleep(1) === 0));
17 if (false === $this->poisonPillCompare->isLatestVersion($this->poisonPillVersion)) {
18 $queue->reject($message);
19 // phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
20 exit(0);
21 }
22 $callback($message);
23 }
24 }
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.