-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAmazonGetReportDocumentQueue.php
More file actions
206 lines (169 loc) · 9.49 KB
/
AmazonGetReportDocumentQueue.php
File metadata and controls
206 lines (169 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
/**
*
* @author xiaoguo0426
* @contact 740644717@qq.com
* @license MIT
*/
namespace App\Queue;
use AmazonPHP\SellingPartner\AccessToken;
use AmazonPHP\SellingPartner\Exception\ApiException;
use AmazonPHP\SellingPartner\Exception\InvalidArgumentException;
use AmazonPHP\SellingPartner\Model\Reports\ReportDocument;
use AmazonPHP\SellingPartner\SellingPartnerSDK;
use App\Queue\Data\AmazonGetReportDocumentData;
use App\Queue\Data\AmazonReportDocumentActionData;
use App\Queue\Data\QueueDataInterface;
use App\Util\Amazon\Report\ReportFactory;
use App\Util\AmazonApp;
use App\Util\AmazonSDK;
use App\Util\Log\AmazonReportDocumentLog;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Exception\NotFoundException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Hyperf\Config\config;
class AmazonGetReportDocumentQueue extends Queue
{
public function getQueueName(): string
{
return 'amazon-get-report-document';
}
public function getQueueDataClass(): string
{
return AmazonGetReportDocumentData::class;
}
/**
* @throws NotFoundException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function handleQueueData(QueueDataInterface $queueData): bool
{
/**
* @var AmazonGetReportDocumentData $queueData
*/
$merchant_id = $queueData->getMerchantId();
$merchant_store_id = $queueData->getMerchantStoreId();
$region = $queueData->getRegion();
$real_marketplace_ids = $queueData->getMarketplaceIds(); // 报告数据包含的marketplace id集合
$report_type = $queueData->getReportType();
$report_document_id = $queueData->getReportDocumentId();
$logger = ApplicationContext::getContainer()->get(AmazonReportDocumentLog::class);
$logger->info(sprintf('Get Document 报告队列数据:%s', $queueData->toJson()));
return AmazonApp::tok2($merchant_id, $merchant_store_id, $region, static function (AmazonSDK $amazonSDK, int $merchant_id, int $merchant_store_id, SellingPartnerSDK $sdk, AccessToken $accessToken, string $region, array $marketplace_ids) use ($real_marketplace_ids, $report_type, $report_document_id) {
$amazonReportDocumentActionQueue = new AmazonReportDocumentActionQueue();
$console = ApplicationContext::getContainer()->get(StdoutLoggerInterface::class);
$logger = ApplicationContext::getContainer()->get(AmazonReportDocumentLog::class);
$dir = sprintf('%s%s/%s/%s-%s/', config('amazon.report_template_path'), 'scheduled', $report_type, $merchant_id, $merchant_store_id);
if (! is_dir($dir) && ! mkdir($dir, 0755, true) && ! is_dir($dir)) {
$log = sprintf('Get Directory "%s" was not created', $dir);
$logger->error($log);
$console->error($log);
return true;
}
$instance = ReportFactory::getInstance($merchant_id, $merchant_store_id, $region, $report_type);
$file_base_name = $instance->getReportFileName($real_marketplace_ids, $region, $report_document_id);
$is_error_marketplace_id_flag = $instance->checkMarketplaceIds($real_marketplace_ids, $report_document_id);
// 检查报告是否存在,如果存在立即推入队列即可,减少请求报告
$file_path_gz = $dir . $file_base_name . '.gz';
$file_path = $dir . $file_base_name . '.txt';
if (file_exists($file_path_gz)) {
// 匿名函数处理gz压缩包,因为我不想再声明$handle_gz,$handle变量
(static function () use ($file_path, $file_path_gz) {
$handle = fopen($file_path, 'wb');
$buffer_size = 4096; // read 4kb at a time
$handle_gz = gzopen($file_path_gz, 'rb');
while (! gzeof($handle_gz)) {
fwrite($handle, gzread($handle_gz, $buffer_size)); // 提取gz文件内容
}
gzclose($handle_gz);
fclose($handle);
})();
} elseif (file_exists($file_path)) {
$log = sprintf('Get Document 报告已存在,直接进入队列. report_type: %s report_document_id: %s file_path:%s merchant_id: %s merchant_store_id: %s', $report_type, $report_document_id, $file_path, $merchant_id, $merchant_store_id);
$console->warning($log);
goto end;
} else {
$retry = 10;
while (true) {
try {
$response = $sdk->reports()->getReportDocument($accessToken, $region, $report_document_id);
$document_url = $response->getUrl();
$log = sprintf('Get Document 报告生成成功 report_type: %s report_document_id: %s url: %s merchant_id: %s merchant_store_id: %s', $report_type, $report_document_id, $document_url, $merchant_id, $merchant_store_id);
$logger->info($log);
$console->info($log);
$compression_algorithm = $response->getCompressionAlgorithm();
if ($compression_algorithm === ReportDocument::COMPRESSION_ALGORITHM_GZIP) {
file_put_contents($file_path_gz, file_get_contents($document_url)); // 保存gz文件
$handle = fopen($file_path, 'wb');
$buffer_size = 4096; // read 4kb at a time
$handle_gz = gzopen($file_path_gz, 'rb');
while (! gzeof($handle_gz)) {
fwrite($handle, gzread($handle_gz, $buffer_size)); // 提取gz文件内容
}
gzclose($handle_gz);
fclose($handle);
// 线上环境gz文件解压提取后需要删除
// if (! app()->isDebug()) {
// unlink($file_path_gz);
// }
} else {
// 下载并保存文件
file_put_contents($file_path, file_get_contents($document_url));
}
if ($is_error_marketplace_id_flag) {
// 如果有错误的marketplace_id,跳过处理.
$log = sprintf('merchant_id:%s merchant_store_id:%s region:%s report_type:%s report_id:%s 报告存在多个市场,已跳过处理', $merchant_id, $merchant_store_id, $region, $report_type, $report_document_id);
$console->warning($log);
$logger->warning($log);
}
break;
} catch (ApiException $e) {
--$retry;
if ($retry > 0) {
$console->warning(sprintf('Get Document report_type: %s report_document_id: %s retry: %s ', $report_type, $report_document_id, $retry));
sleep(10);
continue;
}
$log = sprintf('Get Document report_type: %s report_document_id: %s merchant_id: %s merchant_store_id: %s 获取报告出错 %s', $report_type, $report_document_id, $merchant_id, $merchant_store_id, json_encode([
'merchant_id' => $merchant_id,
'merchant_store_id' => $merchant_store_id,
'marketplace_ids' => $marketplace_ids,
'report_document_id' => $report_document_id,
'report_type' => $report_type,
], JSON_THROW_ON_ERROR));
$console->error($log);
$logger->error($log, [
'message' => $e->getMessage(),
'response body' => $e->getResponseBody(),
]);
break;
} catch (InvalidArgumentException $e) {
$logger->error(sprintf('Get Document report_type: %s report_id: %s merchant_id: %s merchant_store_id: %s 获取报告出错', $report_type, $report_document_id, $merchant_id, $merchant_store_id), [
'message' => 'InvalidArgumentException ' . $e->getMessage(),
]);
break;
}
}
}
end:
$amazonReportDocumentActionData = new AmazonReportDocumentActionData();
$amazonReportDocumentActionData->setMerchantId($merchant_id);
$amazonReportDocumentActionData->setMerchantStoreId($merchant_store_id);
$amazonReportDocumentActionData->setRegion($region);
$amazonReportDocumentActionData->setMarketplaceIds($real_marketplace_ids);
$amazonReportDocumentActionData->setReportType($report_type);
$amazonReportDocumentActionData->setReportDocumentId($report_document_id);
$amazonReportDocumentActionQueue->push($amazonReportDocumentActionData);
return true;
});
}
public function safetyLine(): int
{
return 70;
}
}