-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save Asynchronous Operations with one Batch improvement #27257
Merged
magento-engcom-team
merged 7 commits into
magento:2.4-develop
from
comwrap:async-mass-save
Apr 1, 2020
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aec45cc
Change Way of saving Operations to Bulk during sheduling new Bulk Req…
nuzil ca1a374
Merge branch '2.4-develop' into async-mass-save
nuzil 4aadde4
Fix statict
nuzil 63ce966
Change implementation for use DTO in API. And provide with integratio…
nuzil 27719c9
Merge remote-tracking branch 'upstream-nuzil/2.4-develop' into async-…
nuzil 5da3313
Fix static test
nuzil 04663de
Merge remote-tracking branch 'upstream-nuzil/2.4-develop' into async-…
nuzil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
app/code/Magento/AsynchronousOperations/Api/SaveMultipleOperationsInterface.php
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,27 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\AsynchronousOperations\Api; | ||
|
||
use Magento\AsynchronousOperations\Api\Data\OperationInterface; | ||
|
||
/** | ||
* Interface for saving multiple operations | ||
* | ||
* @api | ||
*/ | ||
interface SaveMultipleOperationsInterface | ||
{ | ||
/** | ||
* Save Operations for Bulk | ||
* | ||
* @param OperationInterface[] $operations | ||
* @return void | ||
*/ | ||
public function execute(array $operations): void; | ||
} |
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
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
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
56 changes: 56 additions & 0 deletions
56
app/code/Magento/AsynchronousOperations/Model/SaveMultipleOperations.php
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,56 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\AsynchronousOperations\Model; | ||
|
||
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface; | ||
use Magento\AsynchronousOperations\Model\ResourceModel\Operation as OperationResource; | ||
use Magento\Framework\Exception\CouldNotSaveException; | ||
|
||
/** | ||
* Implementation for saving multiple operations | ||
*/ | ||
class SaveMultipleOperations implements SaveMultipleOperationsInterface | ||
{ | ||
|
||
/** | ||
* @var OperationResource | ||
*/ | ||
private $operationResource; | ||
|
||
/** | ||
* BulkSummary constructor. | ||
* | ||
* @param OperationResource $operationResource | ||
*/ | ||
public function __construct( | ||
OperationResource $operationResource | ||
) { | ||
$this->operationResource = $operationResource; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute(array $operations): void | ||
{ | ||
try { | ||
$operationsToInsert = array_map(function ($operation) { | ||
return $operation->getData(); | ||
}, $operations); | ||
|
||
$connection = $this->operationResource->getConnection(); | ||
$connection->insertMultiple( | ||
$this->operationResource->getTable(OperationResource::TABLE_NAME), | ||
$operationsToInsert | ||
); | ||
} catch (\Exception $exception) { | ||
throw new CouldNotSaveException(__($exception->getMessage())); | ||
} | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
...integration/testsuite/Magento/AsynchronousOperations/Model/SaveMultipleOperationsTest.php
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,117 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\AsynchronousOperations\Model; | ||
|
||
use Magento\AsynchronousOperations\Api\Data\OperationInterface; | ||
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory; | ||
use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface; | ||
use Magento\AsynchronousOperations\Model\BulkStatus; | ||
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface; | ||
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterfaceFactory; | ||
use Magento\Framework\EntityManager\EntityManager; | ||
|
||
class SaveMultipleOperationsTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
|
||
private const BULK_UUID = "bulk-uuid-multiple-0"; | ||
|
||
/** | ||
* @var BulkStatus | ||
*/ | ||
private $bulkStatusManagement; | ||
|
||
/** | ||
* @var OperationInterfaceFactory | ||
*/ | ||
private $operationFactory; | ||
|
||
/** | ||
* @var SaveMultipleOperationsInterface | ||
*/ | ||
private $saveMultipleOperationsInterface; | ||
|
||
/** | ||
* @var EntityManager | ||
*/ | ||
private $entityManager; | ||
|
||
/** | ||
* @var BulkSummaryInterfaceFactory | ||
*/ | ||
private $bulkSummaryFactory; | ||
|
||
/** | ||
* Set Up the test | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->saveMultipleOperationsInterface = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( | ||
SaveMultipleOperationsInterface::class | ||
); | ||
$this->operationFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( | ||
OperationInterfaceFactory::class | ||
); | ||
$this->bulkStatusManagement = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( | ||
BulkStatus::class | ||
); | ||
$this->bulkSummaryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( | ||
BulkSummaryInterfaceFactory::class | ||
); | ||
$this->entityManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( | ||
EntityManager::class | ||
); | ||
} | ||
|
||
/** | ||
* Test execute() of SaveMultipleOperations | ||
*/ | ||
public function testExecute() | ||
{ | ||
$operation = $this->createOperation(); | ||
$operations = [$operation, $operation, $operation]; | ||
|
||
$bulkSummary = $this->bulkSummaryFactory->create(); | ||
$this->entityManager->load($bulkSummary, self::BULK_UUID); | ||
$bulkSummary->setBulkId(self::BULK_UUID); | ||
$bulkSummary->setDescription("Test Bulk"); | ||
$bulkSummary->setUserId(1); | ||
$bulkSummary->setUserType(1); | ||
$bulkSummary->setOperationCount(count($operations)); | ||
$this->entityManager->save($bulkSummary); | ||
|
||
$this->saveMultipleOperationsInterface->execute($operations); | ||
$operationsCount = $this->bulkStatusManagement | ||
->getOperationsCountByBulkIdAndStatus(self::BULK_UUID, OperationInterface::STATUS_TYPE_OPEN); | ||
$this->assertEquals($operationsCount, 3); | ||
} | ||
|
||
/** | ||
* Create Operation object and pre-fill with test data | ||
* @return OperationInterface | ||
*/ | ||
public function createOperation() | ||
{ | ||
$serializedData = [ | ||
'entity_id' => null, | ||
'entity_link' => '', | ||
'meta_information' => json_encode([ | ||
'entity_id' => 5, | ||
'meta_information' => 'Test' | ||
]) | ||
]; | ||
|
||
$data = [ | ||
'data' => [ | ||
OperationInterface::BULK_ID => self::BULK_UUID, | ||
OperationInterface::TOPIC_NAME => "topic-4", | ||
OperationInterface::SERIALIZED_DATA => json_encode($serializedData), | ||
OperationInterface::STATUS => OperationInterface::STATUS_TYPE_OPEN, | ||
], | ||
]; | ||
return $this->operationFactory->create($data); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have declared OperationInterface[] expected type for '$operations', but in current implementation it will works correctly only with data in arrays.
Please keep contract on OperationInterface or do not make current service as API.