Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkManagement.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory;
16 use Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory;
18 
25 {
29  private $entityManager;
30 
34  private $bulkSummaryFactory;
35 
39  private $operationCollectionFactory;
40 
44  private $publisher;
45 
49  private $metadataPool;
50 
54  private $resourceConnection;
55 
59  private $userContext;
60 
64  private $logger;
65 
77  public function __construct(
78  EntityManager $entityManager,
79  BulkSummaryInterfaceFactory $bulkSummaryFactory,
80  CollectionFactory $operationCollectionFactory,
81  BulkPublisherInterface $publisher,
82  MetadataPool $metadataPool,
83  ResourceConnection $resourceConnection,
84  \Psr\Log\LoggerInterface $logger,
85  UserContextInterface $userContext = null
86  ) {
87  $this->entityManager = $entityManager;
88  $this->bulkSummaryFactory= $bulkSummaryFactory;
89  $this->operationCollectionFactory = $operationCollectionFactory;
90  $this->metadataPool = $metadataPool;
91  $this->resourceConnection = $resourceConnection;
92  $this->publisher = $publisher;
93  $this->logger = $logger;
94  $this->userContext = $userContext ?: ObjectManager::getInstance()->get(UserContextInterface::class);
95  }
96 
100  public function scheduleBulk($bulkUuid, array $operations, $description, $userId = null)
101  {
102  $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
103  $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
104  // save bulk summary and related operations
105  $connection->beginTransaction();
106  $userType = $this->userContext->getUserType();
107  if ($userType === null) {
109  }
110  try {
112  $bulkSummary = $this->bulkSummaryFactory->create();
113  $this->entityManager->load($bulkSummary, $bulkUuid);
114  $bulkSummary->setBulkId($bulkUuid);
115  $bulkSummary->setDescription($description);
116  $bulkSummary->setUserId($userId);
117  $bulkSummary->setUserType($userType);
118  $bulkSummary->setOperationCount((int)$bulkSummary->getOperationCount() + count($operations));
119 
120  $this->entityManager->save($bulkSummary);
121 
122  $connection->commit();
123  } catch (\Exception $exception) {
124  $connection->rollBack();
125  $this->logger->critical($exception->getMessage());
126  return false;
127  }
128  $this->publishOperations($operations);
129 
130  return true;
131  }
132 
140  public function retryBulk($bulkUuid, array $errorCodes)
141  {
142  $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
143  $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
144 
146  $retriablyFailedOperations = $this->operationCollectionFactory->create()
147  ->addFieldToFilter('error_code', ['in' => $errorCodes])
148  ->addFieldToFilter('bulk_uuid', ['eq' => $bulkUuid])
149  ->getItems();
150 
151  // remove corresponding operations from database (i.e. move them to 'open' status)
152  $connection->beginTransaction();
153  try {
154  $operationIds = [];
155  $currentBatchSize = 0;
156  $maxBatchSize = 10000;
158  foreach ($retriablyFailedOperations as $operation) {
159  if ($currentBatchSize === $maxBatchSize) {
160  $connection->delete(
161  $this->resourceConnection->getTableName('magento_operation'),
162  $connection->quoteInto('id IN (?)', $operationIds)
163  );
164  $operationIds = [];
165  $currentBatchSize = 0;
166  }
167  $currentBatchSize++;
168  $operationIds[] = $operation->getId();
169  // Rescheduled operations must be put in queue in 'open' state (i.e. without ID)
170  $operation->setId(null);
171  }
172  // remove operations from the last batch
173  if (!empty($operationIds)) {
174  $connection->delete(
175  $this->resourceConnection->getTableName('magento_operation'),
176  $connection->quoteInto('id IN (?)', $operationIds)
177  );
178  }
179 
180  $connection->commit();
181  } catch (\Exception $exception) {
182  $connection->rollBack();
183  $this->logger->critical($exception->getMessage());
184  return 0;
185  }
186  $this->publishOperations($retriablyFailedOperations);
187 
188  return count($retriablyFailedOperations);
189  }
190 
197  private function publishOperations(array $operations)
198  {
199  $operationsByTopics = [];
200  foreach ($operations as $operation) {
201  $operationsByTopics[$operation->getTopicName()][] = $operation;
202  }
203  foreach ($operationsByTopics as $topicName => $operations) {
204  $this->publisher->publish($topicName, $operations);
205  }
206  }
207 
211  public function deleteBulk($bulkId)
212  {
213  return $this->entityManager->delete(
214  $this->entityManager->load(
215  $this->bulkSummaryFactory->create(),
216  $bulkId
217  )
218  );
219  }
220 }
$operations
Definition: bulk.php:55
$logger
__construct(EntityManager $entityManager, BulkSummaryInterfaceFactory $bulkSummaryFactory, CollectionFactory $operationCollectionFactory, BulkPublisherInterface $publisher, MetadataPool $metadataPool, ResourceConnection $resourceConnection, \Psr\Log\LoggerInterface $logger, UserContextInterface $userContext=null)
scheduleBulk($bulkUuid, array $operations, $description, $userId=null)
$connection
Definition: bulk.php:13