Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PublisherConsumerController.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
14 
16 {
20  private $consumers = [];
21 
25  private $publisher;
26 
30  private $logFilePath;
31 
35  private $maxMessages = null;
36 
40  private $osInfo;
41 
45  private $appInitParams;
46 
50  private $amqpHelper;
51 
52  public function __construct(
53  PublisherInterface $publisher,
54  OsInfo $osInfo,
55  Amqp $amqpHelper,
56  $logFilePath,
57  $consumers,
58  $appInitParams,
59  $maxMessages = null
60  ) {
61  $this->consumers = $consumers;
62  $this->publisher = $publisher;
63  $this->logFilePath = $logFilePath;
64  $this->maxMessages = $maxMessages;
65  $this->osInfo = $osInfo;
66  $this->appInitParams = $appInitParams;
67  $this->amqpHelper = $amqpHelper;
68  }
69 
76  public function initialize()
77  {
78  if ($this->osInfo->isWindows()) {
80  "This test relies on *nix shell and should be skipped in Windows environment."
81  );
82  }
83  $connections = $this->amqpHelper->getConnections();
84  foreach (array_keys($connections) as $connectionName) {
85  $this->amqpHelper->deleteConnection($connectionName);
86  }
87  $this->amqpHelper->clearQueue("async.operations.all");
88  foreach ($this->consumers as $consumer) {
89  foreach ($this->getConsumerProcessIds($consumer) as $consumerProcessId) {
90  exec("kill {$consumerProcessId}");
91  }
92  }
93  foreach ($this->consumers as $consumer) {
94  if (!$this->getConsumerProcessIds($consumer)) {
95  exec("{$this->getConsumerStartCommand($consumer, true)} > /dev/null &");
96  }
97  sleep(5);
98  }
99 
100  if (file_exists($this->logFilePath)) {
101  // try to remove before failing the test
102  unlink($this->logFilePath);
103  if (file_exists($this->logFilePath)) {
104  throw new PreconditionFailedException(
105  "Precondition failed: test log ({$this->logFilePath}) cannot be deleted before test execution."
106  );
107  }
108  }
109  }
110 
114  public function stopConsumers()
115  {
116  foreach ($this->consumers as $consumer) {
117  foreach ($this->getConsumerProcessIds($consumer) as $consumerProcessId) {
118  exec("kill {$consumerProcessId}");
119  }
120  }
121  }
122 
126  public function getConsumersProcessIds()
127  {
128  $consumers = [];
129  foreach ($this->consumers as $consumer) {
130  $consumers[$consumer] = $this->getConsumerProcessIds($consumer);
131  }
132  return $consumers;
133  }
134 
139  private function getConsumerProcessIds($consumer)
140  {
141  exec("ps ax | grep -v grep | grep '{$this->getConsumerStartCommand($consumer)}' | awk '{print $1}'", $output);
142  return $output;
143  }
144 
152  private function getConsumerStartCommand($consumer, $withEnvVariables = false)
153  {
154  $binDirectory = realpath(INTEGRATION_TESTS_DIR . '/bin/');
155  $magentoCli = $binDirectory . '/magento';
156  $consumerStartCommand = "php {$magentoCli} queue:consumers:start -vvv " . $consumer;
157  if ($this->maxMessages) {
158  $consumerStartCommand .= " --max-messages={$this->maxMessages}";
159  }
160  if ($withEnvVariables) {
161  $params = $this->appInitParams;
162  $params['MAGE_DIRS']['base']['path'] = BP;
163  $params = 'INTEGRATION_TEST_PARAMS="' . urldecode(http_build_query($params)) . '"';
164  $consumerStartCommand = $params . ' ' . $consumerStartCommand;
165  }
166  return $consumerStartCommand;
167  }
168 
174  public function waitForAsynchronousResult(callable $condition, $params)
175  {
176  $i = 0;
177  do {
178  sleep(1);
179  $assertion = call_user_func_array($condition, $params);
180  } while (!$assertion && ($i++ < 180));
181 
182  if (!$assertion) {
183  throw new PreconditionFailedException("No asynchronous messages were processed.");
184  }
185  }
186 
190  public function getPublisher()
191  {
192  return $this->publisher;
193  }
194 }
exec($command, array &$output=null, &$return_var=null)
const BP
Definition: autoload.php:14
__construct(PublisherInterface $publisher, OsInfo $osInfo, Amqp $amqpHelper, $logFilePath, $consumers, $appInitParams, $maxMessages=null)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31