Skip to content
This repository was archived by the owner on May 20, 2019. It is now read-only.

Commit 10af68d

Browse files
committed
magento-engcom/bulk-api#4 Support for Async operations in WebAPI
- changed interface names per architectural feedback - added statuses tracking for successfull operations
1 parent 7d3a565 commit 10af68d

18 files changed

+124
-151
lines changed

app/code/Magento/AsynchronousOperations/Api/BulkStatusInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface BulkStatusInterface extends \Magento\Framework\Bulk\BulkStatusInterfac
2020
* Get Bulk summary data with list of operations items full data.
2121
*
2222
* @param string $bulkUuid
23-
* @return \Magento\AsynchronousOperations\Api\Data\DetailedBulkStatusInterface
23+
* @return \Magento\AsynchronousOperations\Api\Data\DetailedBulkOperationsStatusInterface
2424
* @throws \Magento\Framework\Exception\NoSuchEntityException
2525
*/
2626
public function getBulkDetailedStatus($bulkUuid);
@@ -29,7 +29,7 @@ public function getBulkDetailedStatus($bulkUuid);
2929
* Get Bulk summary data with list of operations items short data.
3030
*
3131
* @param string $bulkUuid
32-
* @return \Magento\AsynchronousOperations\Api\Data\BulkStatusInterface
32+
* @return \Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterface
3333
* @throws \Magento\Framework\Exception\NoSuchEntityException
3434
*/
3535
public function getBulkShortStatus($bulkUuid);

app/code/Magento/AsynchronousOperations/Api/Data/BulkStatusInterface.php renamed to app/code/Magento/AsynchronousOperations/Api/Data/BulkOperationsStatusInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
*
1616
* @api
1717
*/
18-
interface BulkStatusInterface extends BulkSummaryInterface
18+
interface BulkOperationsStatusInterface extends BulkSummaryInterface
1919
{
2020

2121
const OPERATIONS_LIST = 'operations_list';
2222

2323
/**
2424
* Retrieve list of operation with statuses (short data).
2525
*
26-
* @return \Magento\AsynchronousOperations\Api\Data\OperationStatusInterface[]
26+
* @return \Magento\AsynchronousOperations\Api\Data\SummaryOperationStatusInterface[]
2727
*/
2828
public function getOperationsList();
2929

3030
/**
3131
* Set operations list.
3232
*
33-
* @param \Magento\AsynchronousOperations\Api\Data\OperationStatusInterface[] $operationStatusList
33+
* @param \Magento\AsynchronousOperations\Api\Data\SummaryOperationStatusInterface[] $operationStatusList
3434
* @return $this
3535
*/
3636
public function setOperationsList($operationStatusList);

app/code/Magento/AsynchronousOperations/Api/Data/DetailedBulkStatusInterface.php renamed to app/code/Magento/AsynchronousOperations/Api/Data/DetailedBulkOperationsStatusInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
*
1616
* @api
1717
*/
18-
interface DetailedBulkStatusInterface extends BulkSummaryInterface
18+
interface DetailedBulkOperationsStatusInterface extends BulkSummaryInterface
1919
{
2020

2121
const OPERATIONS_LIST = 'operations_list';
2222

2323
/**
2424
* Retrieve operations list.
2525
*
26-
* @return \Magento\AsynchronousOperations\Api\Data\OperationInterface[]
26+
* @return \Magento\AsynchronousOperations\Api\Data\DetailedOperationStatusInterface[]
2727
*/
2828
public function getOperationsList();
2929

3030
/**
3131
* Set operations list.
3232
*
33-
* @param \Magento\AsynchronousOperations\Api\Data\OperationInterface[] $operationStatusList
33+
* @param \Magento\AsynchronousOperations\Api\Data\DetailedOperationStatusInterface[] $operationStatusList
3434
* @return $this
3535
*/
3636
public function setOperationsList($operationStatusList);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AsynchronousOperations\Api\Data;
10+
11+
/**
12+
* @api
13+
*/
14+
interface DetailedOperationStatusInterface extends OperationInterface
15+
{
16+
/**
17+
* Result serialized Data
18+
*
19+
* @return string
20+
*/
21+
public function getResultSerializedData();
22+
23+
/**
24+
* Set result serialized data
25+
*
26+
* @param string $resultSerializedData
27+
* @return $this
28+
*/
29+
public function setResultSerializedData($resultSerializedData);
30+
}
31+

app/code/Magento/AsynchronousOperations/Api/Data/OperationStatusInterface.php renamed to app/code/Magento/AsynchronousOperations/Api/Data/SummaryOperationStatusInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
* without serialized_data and result_serialized_data
1616
*
1717
* @api
18-
* @see \Magento\AsynchronousOperations\Api\Data\OperationInterface
1918
*/
20-
interface OperationStatusInterface
19+
interface SummaryOperationStatusInterface
2120
{
2221
/**
2322
* Operation id

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\EntityManager\EntityManager;
13-
use Magento\AsynchronousOperations\Api\Data\BulkStatusInterfaceFactory as BulkStatusShortFactory;
14-
use Magento\AsynchronousOperations\Api\Data\DetailedBulkStatusInterfaceFactory as BulkStatusDetailedFactory;
13+
use Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterfaceFactory as BulkStatusShortFactory;
14+
use Magento\AsynchronousOperations\Api\Data\DetailedBulkOperationsStatusInterfaceFactory as BulkStatusDetailedFactory;
1515
use Magento\AsynchronousOperations\Api\Data\OperationDetailsInterfaceFactory;
1616
use Magento\AsynchronousOperations\Api\BulkStatusInterface;
1717
use Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory;
@@ -42,7 +42,7 @@ class BulkOperationsStatus implements BulkStatusInterface
4242
private $bulkStatus;
4343

4444
/**
45-
* @var \Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory
45+
* @var CollectionFactory
4646
*/
4747
private $operationCollectionFactory;
4848

@@ -108,7 +108,7 @@ public function getBulkDetailedStatus($bulkUuid)
108108
{
109109
$bulkSummary = $this->bulkDetailedFactory->create();
110110

111-
/** @var \Magento\AsynchronousOperations\Api\Data\DetailedBulkStatusInterface $bulk */
111+
/** @var \Magento\AsynchronousOperations\Api\Data\DetailedBulkOperationsStatusInterface $bulk */
112112
$bulk = $this->entityManager->load($bulkSummary, $bulkUuid);
113113

114114
if ($bulk->getBulkId() === null) {
@@ -132,7 +132,7 @@ public function getBulkShortStatus($bulkUuid)
132132
{
133133
$bulkSummary = $this->bulkShortFactory->create();
134134

135-
/** @var \Magento\AsynchronousOperations\Api\Data\BulkStatusInterface $bulk */
135+
/** @var \Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterface $bulk */
136136
$bulk = $this->entityManager->load($bulkSummary, $bulkUuid);
137137
if ($bulk->getBulkId() === null) {
138138
throw new NoSuchEntityException(

app/code/Magento/AsynchronousOperations/Model/BulkStatus/CalculatedStatusSql.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magento\AsynchronousOperations\Model\BulkStatus;
88

9-
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
109
use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
1110

1211
class CalculatedStatusSql

app/code/Magento/AsynchronousOperations/Model/BulkStatus/Detailed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
namespace Magento\AsynchronousOperations\Model\BulkStatus;
1010

11-
use Magento\AsynchronousOperations\Api\Data\DetailedBulkStatusInterface;
11+
use Magento\AsynchronousOperations\Api\Data\DetailedBulkOperationsStatusInterface;
1212
use Magento\AsynchronousOperations\Model\BulkSummary;
1313

14-
class Detailed extends BulkSummary implements DetailedBulkStatusInterface
14+
class Detailed extends BulkSummary implements DetailedBulkOperationsStatusInterface
1515
{
1616
/**
1717
* @inheritDoc

app/code/Magento/AsynchronousOperations/Model/BulkStatus/Short.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
namespace Magento\AsynchronousOperations\Model\BulkStatus;
1010

11-
use Magento\AsynchronousOperations\Api\Data\BulkStatusInterface;
11+
use Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterface;
1212
use Magento\AsynchronousOperations\Model\BulkSummary;
1313

14-
class Short extends BulkSummary implements BulkStatusInterface
14+
class Short extends BulkSummary implements BulkOperationsStatusInterface
1515
{
1616
/**
1717
* @inheritDoc

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*/
66
namespace Magento\AsynchronousOperations\Model;
77

8-
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
8+
use Magento\AsynchronousOperations\Api\Data\DetailedOperationStatusInterface;
99
use Magento\Framework\DataObject;
1010
use Magento\Framework\Api\ExtensibleDataInterface;
1111

1212
/**
1313
* Class Operation
1414
*/
15-
class Operation extends DataObject implements OperationInterface, ExtensibleDataInterface
15+
class Operation extends DataObject implements DetailedOperationStatusInterface, ExtensibleDataInterface
1616
{
1717
/**
1818
* @inheritDoc

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Magento\AsynchronousOperations\Model;
88

9-
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
9+
use Magento\AsynchronousOperations\Api\Data\DetailedOperationStatusInterfaceFactory;
1010
use Magento\Framework\EntityManager\EntityManager;
1111

1212
/**
@@ -20,7 +20,7 @@ class OperationManagement implements \Magento\Framework\Bulk\OperationManagement
2020
private $entityManager;
2121

2222
/**
23-
* @var OperationInterfaceFactory
23+
* @var DetailedOperationStatusInterfaceFactory
2424
*/
2525
private $operationFactory;
2626

@@ -33,31 +33,39 @@ class OperationManagement implements \Magento\Framework\Bulk\OperationManagement
3333
* OperationManagement constructor.
3434
*
3535
* @param EntityManager $entityManager
36-
* @param OperationInterfaceFactory $operationFactory
36+
* @param DetailedOperationStatusInterfaceFactory $operationFactory
3737
* @param \Psr\Log\LoggerInterface $logger
3838
*/
3939
public function __construct(
4040
EntityManager $entityManager,
41-
OperationInterfaceFactory $operationFactory,
41+
DetailedOperationStatusInterfaceFactory $operationFactory,
4242
\Psr\Log\LoggerInterface $logger
4343
) {
4444
$this->entityManager = $entityManager;
45-
$this->operationFactory= $operationFactory;
45+
$this->operationFactory = $operationFactory;
4646
$this->logger = $logger;
4747
}
4848

4949
/**
5050
* @inheritDoc
5151
*/
52-
public function changeOperationStatus($operationId, $status, $errorCode = null, $message = null, $data = null)
53-
{
52+
public function changeOperationStatus(
53+
$operationId,
54+
$status,
55+
$errorCode = null,
56+
$message = null,
57+
$data = null,
58+
$resultData = null
59+
) {
5460
try {
5561
$operationEntity = $this->operationFactory->create();
5662
$this->entityManager->load($operationEntity, $operationId);
5763
$operationEntity->setErrorCode($errorCode);
5864
$operationEntity->setStatus($status);
5965
$operationEntity->setResultMessage($message);
6066
$operationEntity->setSerializedData($data);
67+
$operationEntity->setResultSerializedData($resultData);
68+
$operationEntity->setResultSerializedData($resultData);
6169
$this->entityManager->save($operationEntity);
6270
} catch (\Exception $exception) {
6371
$this->logger->critical($exception->getMessage());

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Magento\Framework\DB\Adapter\ConnectionException;
2323
use Magento\Framework\DB\Adapter\DeadlockException;
2424
use Magento\Framework\DB\Adapter\LockWaitException;
25+
use Magento\Framework\Webapi\ServiceOutputProcessor;
26+
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
2527

2628
/**
2729
* Class OperationProcessor
@@ -60,6 +62,16 @@ class OperationProcessor
6062
*/
6163
private $logger;
6264

65+
/**
66+
* @var ServiceOutputProcessor
67+
*/
68+
private $serviceOutputProcessor;
69+
70+
/**
71+
* @var CommunicationConfig
72+
*/
73+
private $communicationConfig;
74+
6375
/**
6476
* OperationProcessor constructor.
6577
*
@@ -69,13 +81,17 @@ class OperationProcessor
6981
* @param Json $jsonHelper
7082
* @param OperationManagementInterface $operationManagement
7183
* @param LoggerInterface $logger
84+
* @param \Magento\Framework\Webapi\ServiceOutputProcessor $serviceOutputProcessor
85+
* @param \Magento\Framework\Communication\ConfigInterface $communicationConfig
7286
*/
7387
public function __construct(
7488
MessageValidator $messageValidator,
7589
MessageEncoder $messageEncoder,
7690
ConsumerConfigurationInterface $configuration,
7791
Json $jsonHelper,
7892
OperationManagementInterface $operationManagement,
93+
ServiceOutputProcessor $serviceOutputProcessor,
94+
CommunicationConfig $communicationConfig,
7995
LoggerInterface $logger
8096
) {
8197
$this->messageValidator = $messageValidator;
@@ -84,6 +100,8 @@ public function __construct(
84100
$this->jsonHelper = $jsonHelper;
85101
$this->operationManagement = $operationManagement;
86102
$this->logger = $logger;
103+
$this->serviceOutputProcessor = $serviceOutputProcessor;
104+
$this->communicationConfig = $communicationConfig;
87105
}
88106

89107
/**
@@ -113,12 +131,32 @@ public function process(string $encodedMessage)
113131
$messages[] = $e->getMessage();
114132
}
115133

134+
$outputData = null;
116135
if ($errorCode === null) {
117136
foreach ($handlers as $callback) {
118137
$result = $this->executeHandler($callback, $entityParams);
119138
$status = $result['status'];
120139
$errorCode = $result['error_code'];
121140
$messages = array_merge($messages, $result['messages']);
141+
$outputData = $result['output_data'];
142+
}
143+
}
144+
145+
if (isset($outputData)) {
146+
try {
147+
$communicationConfig = $this->communicationConfig->getTopic($topicName);
148+
$asyncHandler =
149+
$communicationConfig[CommunicationConfig::TOPIC_HANDLERS][AsyncConfig::DEFAULT_HANDLER_NAME];
150+
$serviceClass = $asyncHandler[CommunicationConfig::HANDLER_TYPE];
151+
$serviceMethod = $asyncHandler[CommunicationConfig::HANDLER_METHOD];
152+
$outputData = $this->serviceOutputProcessor->process(
153+
$outputData,
154+
$serviceClass,
155+
$serviceMethod
156+
);
157+
$outputData = $this->jsonHelper->serialize($outputData);
158+
} catch (\Exception $e) {
159+
$messages[] = $e->getMessage();
122160
}
123161
}
124162

@@ -128,7 +166,8 @@ public function process(string $encodedMessage)
128166
$status,
129167
$errorCode,
130168
implode('; ', $messages),
131-
$serializedData
169+
$serializedData,
170+
$outputData
132171
);
133172
}
134173

@@ -144,10 +183,11 @@ private function executeHandler($callback, $entityParams)
144183
$result = [
145184
'status' => OperationInterface::STATUS_TYPE_COMPLETE,
146185
'error_code' => null,
147-
'messages' => []
186+
'messages' => [],
187+
'output_data' => null
148188
];
149189
try {
150-
call_user_func_array($callback, $entityParams);
190+
$result['output_data'] = call_user_func_array($callback, $entityParams);
151191
$result['messages'][] = sprintf('Service execution success %s::%s', get_class($callback[0]), $callback[1]);
152192
} catch (\Zend_Db_Adapter_Exception $e) {
153193
$this->logger->critical($e->getMessage());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
namespace Magento\AsynchronousOperations\Model;
1010

1111
use Magento\AsynchronousOperations\Api\Data\OperationInterface;
12-
use Magento\AsynchronousOperations\Api\Data\OperationStatusInterface;
12+
use Magento\AsynchronousOperations\Api\Data\SummaryOperationStatusInterface;
1313
use Magento\Framework\DataObject;
1414
use Magento\Framework\Api\ExtensibleDataInterface;
1515

1616
/**
1717
* Class OperationShortDetails
1818
*/
19-
class OperationStatus extends DataObject implements OperationStatusInterface, ExtensibleDataInterface
19+
class OperationStatus extends DataObject implements SummaryOperationStatusInterface, ExtensibleDataInterface
2020
{
2121
/**
2222
* @inheritDoc

0 commit comments

Comments
 (0)