Skip to content

Commit d096342

Browse files
committed
MC-42360: [Magento Cloud] Stock Web Service Stuck
1 parent ab59159 commit d096342

File tree

4 files changed

+140
-5
lines changed

4 files changed

+140
-5
lines changed

app/code/Magento/AsynchronousOperations/Model/BulkManagement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ public function scheduleBulk($bulkUuid, array $operations, $description, $userId
119119
$bulkSummary->setOperationCount((int)$bulkSummary->getOperationCount() + count($operations));
120120
$this->entityManager->save($bulkSummary);
121121

122+
$this->publishOperations($operations);
123+
122124
$connection->commit();
123125
} catch (\Exception $exception) {
124126
$connection->rollBack();
125127
$this->logger->critical($exception->getMessage());
126128
return false;
127129
}
128-
$this->publishOperations($operations);
129130

130131
return true;
131132
}

app/code/Magento/AsynchronousOperations/Model/MassSchedule.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,17 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
166166
}
167167
}
168168

169-
$this->saveMultipleOperations->execute($operations);
170169
if (!$this->bulkManagement->scheduleBulk($groupId, $operations, $bulkDescription, $userId)) {
171-
throw new LocalizedException(
172-
__('Something went wrong while processing the request.')
173-
);
170+
try {
171+
$this->bulkManagement->deleteBulk($groupId);
172+
} finally {
173+
throw new LocalizedException(
174+
__('Something went wrong while processing the request.')
175+
);
176+
}
174177
}
178+
$this->saveMultipleOperations->execute($operations);
179+
175180
/** @var AsyncResponseInterface $asyncResponse */
176181
$asyncResponse = $this->asyncResponseFactory->create();
177182
$asyncResponse->setBulkUuid($groupId);

app/code/Magento/AsynchronousOperations/Test/Unit/Model/BulkManagementTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,85 @@ public function testScheduleBulkWithException()
186186
$this->assertFalse($this->bulkManagement->scheduleBulk($bulkUuid, [$operation], $description, $userId));
187187
}
188188

189+
/**
190+
* Test for scheduleBulk method with exception during publishing.
191+
*
192+
* @return void
193+
*/
194+
public function testScheduleBulkWithExceptionDuringPublishing()
195+
{
196+
$bulkUuid = 'bulk-001';
197+
$description = 'Bulk summary description...';
198+
$userId = 1;
199+
$userType = UserContextInterface::USER_TYPE_ADMIN;
200+
$connectionName = 'default';
201+
$exceptionMessage = 'Exception message';
202+
$operation = $this->createMock(OperationInterface::class);
203+
$metadata = $this->createMock(EntityMetadataInterface::class);
204+
$this->metadataPool->expects($this->once())
205+
->method('getMetadata')
206+
->with(BulkSummaryInterface::class)
207+
->willReturn($metadata);
208+
$metadata->expects($this->once())
209+
->method('getEntityConnectionName')
210+
->willReturn($connectionName);
211+
$connection = $this->createMock(AdapterInterface::class);
212+
$this->resourceConnection->expects($this->once())
213+
->method('getConnectionByName')
214+
->with($connectionName)
215+
->willReturn($connection);
216+
$connection->expects($this->once())
217+
->method('beginTransaction')
218+
->willReturnSelf();
219+
$bulkSummary = $this->createMock(BulkSummaryInterface::class);
220+
$this->bulkSummaryFactory->expects($this->once())
221+
->method('create')
222+
->willReturn($bulkSummary);
223+
$this->entityManager->expects($this->once())
224+
->method('load')
225+
->with($bulkSummary, $bulkUuid)
226+
->willReturn($bulkSummary);
227+
$bulkSummary->expects($this->once())
228+
->method('setBulkId')
229+
->with($bulkUuid)
230+
->willReturnSelf();
231+
$bulkSummary->expects($this->once())
232+
->method('setDescription')
233+
->with($description)
234+
->willReturnSelf();
235+
$bulkSummary->expects($this->once())
236+
->method('setUserId')
237+
->with($userId)
238+
->willReturnSelf();
239+
$bulkSummary->expects($this->once())
240+
->method('setUserType')
241+
->with($userType)
242+
->willReturnSelf();
243+
$bulkSummary->expects($this->once())
244+
->method('getOperationCount')
245+
->willReturn(1);
246+
$bulkSummary->expects($this->once())
247+
->method('setOperationCount')
248+
->with(2)
249+
->willReturnSelf();
250+
$this->entityManager->expects($this->once())
251+
->method('save')
252+
->with($bulkSummary)
253+
->willReturn($bulkSummary);
254+
$this->publisher->expects($this->once())
255+
->method('publish')
256+
->willThrowException(new \Exception($exceptionMessage));
257+
$connection->expects($this->never())
258+
->method('commit');
259+
$connection->expects($this->once())
260+
->method('rollBack')
261+
->willReturnSelf();
262+
$this->logger->expects($this->once())
263+
->method('critical')
264+
->with($exceptionMessage);
265+
$this->assertFalse($this->bulkManagement->scheduleBulk($bulkUuid, [$operation], $description, $userId));
266+
}
267+
189268
/**
190269
* Test for retryBulk method.
191270
*

dev/tests/integration/testsuite/Magento/AsynchronousOperations/Model/MassScheduleTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
namespace Magento\AsynchronousOperations\Model;
1313

14+
use Magento\AsynchronousOperations\Model\ResourceModel\Bulk\Collection as BulkCollection;
15+
use Magento\Framework\MessageQueue\BulkPublisherInterface;
1416
use Magento\Framework\Exception\BulkException;
1517
use Magento\Framework\Phrase;
1618
use Magento\Framework\Registry;
@@ -123,6 +125,54 @@ public function testScheduleMass($products)
123125
}
124126
}
125127

128+
/**
129+
* @dataProvider publisherExceptionDataProvider
130+
* @param \Exception $exception
131+
*/
132+
public function testScheduleMassWithExceptionDuringPublishing(\Exception $exception)
133+
{
134+
$products = [
135+
['product' => $this->getProduct()],
136+
];
137+
138+
$publisher = $this->createMock(BulkPublisherInterface::class);
139+
$publisher->expects($this->atLeastOnce())
140+
->method('publish')
141+
->willThrowException($exception);
142+
$bulkManagement = $this->objectManager->create(
143+
BulkManagement::class,
144+
['publisher' => $publisher]
145+
);
146+
$this->massSchedule = $this->objectManager->create(
147+
MassSchedule::class,
148+
['bulkManagement' => $bulkManagement]
149+
);
150+
151+
$bulkCollection = $this->objectManager->create(BulkCollection::class);
152+
$bulksCount = $bulkCollection->getSize();
153+
try {
154+
$this->massSchedule->publishMass(
155+
'async.magento.catalog.api.productrepositoryinterface.save.post',
156+
$products
157+
);
158+
$this->fail('Publish is not failed when operations are not published.');
159+
} catch (\Exception $e) {
160+
$bulkCollection = $this->objectManager->create(BulkCollection::class);
161+
$this->assertEquals($bulksCount, $bulkCollection->getSize());
162+
}
163+
}
164+
165+
/**
166+
* @return array
167+
*/
168+
public function publisherExceptionDataProvider(): array
169+
{
170+
return [
171+
[new \InvalidArgumentException('Unknown publisher type async')],
172+
[new \LogicException('Could not find an implementation type for type "async" and connection "amqp".')],
173+
];
174+
}
175+
126176
public function sendBulk($products)
127177
{
128178
$this->skus = [];

0 commit comments

Comments
 (0)