-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample1.php
More file actions
242 lines (199 loc) · 10.5 KB
/
Copy pathsample1.php
File metadata and controls
242 lines (199 loc) · 10.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace app\common\parserJobs;
use app\common\MaxItemsInterrputer;
use app\common\parsers\ParserInterface;
use app\models\general\Category;
use app\models\general\Item;
use app\models\general\ParserJobRun;
use app\models\general\Region;
use app\services\FlashNotificationService\FlashNotificationServiceInterface;
use app\services\Logger;
use app\services\NotifierService\Attachment;
use app\services\NotifierService\NotifierService;
use app\services\PaymentService;
use yii\base\BaseObject;
use yii\queue\JobInterface;
class ParserJob extends BaseObject implements JobInterface
{
public int $parser_job_run_id;
private FlashNotificationServiceInterface $notificationService;
private NotifierService $notifierService;
public function __construct($config = [])
{
parent::__construct($config);
$this->notificationService = \Yii::$container->get(FlashNotificationServiceInterface::class);
$this->notifierService = new NotifierService();
}
public function execute($queue)
{
$paymentService = new PaymentService();
$jobRun = ParserJobRun::findOne($this->parser_job_run_id);
$counter = new \stdClass();
$counter->countItems = 0;
$maxItems = $jobRun->settings['max_items'] ?? 0;
$logger = new Logger('ParserJobRun', $jobRun->id);
/**
* @var class-string<ParserInterface> $parserClass
*/
$parserClass = $jobRun->parserJob->parser->class_name;
$parser = new $parserClass($jobRun->settings ?? [], ['interrupter' => MaxItemsInterrputer::class, 'maxItems' => $maxItems, 'logger' => $logger,
'checkStopFlag' => function () use ($counter, $maxItems) {
return $counter->countItems >= $maxItems;
},
"notifyCallback" => function(array $data) use ($jobRun) {
$subject = $data['subject'] ?? \Yii::t('app', "Notification from importer job '{jobName}' at {date}",
[
'jobName' => $jobRun->parserJob->name,
'date' => (new \DateTime())->format('Y-m-d H:i:s')
]);
$message = $data['message'] ?? \Yii::t('app', "Job results are attached");
$filepath = $data['filename'] ?? null;
$attachments = [];
if ($filepath !== null) {
$filename = basename($filepath);
$content = file_get_contents($filepath);
$attachments = [
new Attachment($filename, $content)
];
}
$this->notifierService->notifyUser($subject, $message, $attachments);
}
]);
if (!($jobRun->settings['parse_category'] ?? false) && !($jobRun->settings['parse_regions'] ?? false)) {
$amount = $parserClass::getTariffRunPriceCoeff() * \Yii::$app->params['baseRunTariffPrice'] + $parser->getMaxItems() * $parserClass::getTariffItemPriceCoeff() * \Yii::$app->params['baseItemTariffPrice'];
try {
$balance = $paymentService->getBalance();
} catch (\Throwable $throwable) {
$logger->debug($throwable->getMessage() . $throwable->getTraceAsString());
return;
}
$logger->log(Logger::LEVEL_INFO, "Current balance: " . $balance);
if ($balance < $amount) {
$logger->log(Logger::LEVEL_INFO, "Not enough balance: " . $balance . ", need $amount");
$jobRun->status = ParserJobRun::STATUS_ERROR;
$jobRun->save();
return;
}
try {
$payment = $paymentService->createHoldPaymentForJobRun($jobRun, $amount);
} catch (\Throwable $throwable) {
$logger->debug($throwable->getMessage() . $throwable->getTraceAsString());
return;
}
if (!$payment->id) {
$logger->debug(print_r($payment->errors, 1));
}
}
$jobRun->date_started = (new \DateTime())->format('c');
$jobRun->status = ParserJobRun::STATUS_PROCESSING;
if (!$jobRun->save()) {
$logger->debug(print_r($jobRun->errors, 1));
}
if ($jobRun->settings['parse_category'] ?? false) {
try {
$parser->parseCategories(function (array $item) use ($counter, $logger, $parser) {
$category = Category::find()->where(['external_id'=>$item['external_id'], 'source'=>$parser->getInstanceName()])->one();
/**
* @var Category $parent
*/
$parent = Category::find()->where(['external_id'=>$item['parent'], 'source'=>$parser->getInstanceName()])->one();
$parent_id = $parent?->id;
if (!$category) {
$category = new Category();
}
$category->name = $item['name'];
$category->parent_id = $parent_id;
$category->external_id = (string)$item['external_id'];
$category->source = (string)$parser->getInstanceName();
if (!$category->save()) {
$logger->log(Logger::LEVEL_INFO, "Can not save category: " . print_r($category->errors, 1));
}
});
} catch (\Throwable $throwable) {
$logger->log(Logger::LEVEL_INFO, "Error while parsing categories: " . $throwable->getMessage() . $throwable->getTraceAsString());
}
} elseif ($jobRun->settings['parse_regions'] ?? false) {
try {
$parser->parseRegions(function (array $item) use ($counter, $logger, $parser) {
$region = Region::find()->where(['external_id'=>$item['external_id'], 'source'=>$parser->getInstanceName()])->one();
/**
* @var Region $parent
*/
$parent = Region::find()->where(['external_id'=>$item['parent'], 'source'=>$parser->getInstanceName()])->one();
$parent_id = $parent?->id;
if (!$region) {
$region = new Region();
}
$region->name = $item['name'];
$region->parent_id = $parent_id;
$region->external_id = (string)$item['external_id'];
$region->source = (string)$parser->getInstanceName();
if (!$region->save()) {
$logger->log(Logger::LEVEL_INFO, "Can not save region: " . print_r($region->errors, 1));
}
});
} catch (\Throwable $throwable) {
$logger->log(Logger::LEVEL_INFO, "Error while parsing regions: " . $throwable->getMessage() . $throwable->getTraceAsString());
}
} else {
try {
$parser->parse(function (array $item) use ($maxItems, $counter, $logger, $parser, $jobRun) {
if ($counter->countItems < $maxItems) {
/**
* @var null|Category $category
*/
$category = isset($item['category']) ? Category::find()->where(['external_id' => $item['category']])->one() : null;
/**
* @var null|Item $sameCorellationItem
*/
$sameCorellationItem = Item::find()->where(['main_external_id' => $item['external_id'], 'parser_job_run_id' => $this->parser_job_run_id])->one();
if ($sameCorellationItem) {
$corellationUuid = $sameCorellationItem->correlation_uuid;
} else {
$corellationUuid = \Ramsey\Uuid\Uuid::uuid4()->toString();
}
$it = new Item();
$it->name = preg_replace('/\s+/', ' ', trim($item['name'] ?? ''));
$it->price = $item['price'] ?? '0';
$it->brand_name = $item['brand_name'] ?? null;
$it->external_id = (string)$item['external_id'] . (isset($item['variant']) ? (' | ' . $item['variant']) : '');
$it->variant = $item['variant'] ?? null;
$it->main_external_id = (string)$item['external_id'];
$it->url = $item['url'] ?? $item['external_id'];
$it->correlation_uuid = $corellationUuid;
$it->source = $parser->getInstanceName();
$it->parser_job_run_id = $this->parser_job_run_id;
$it->images = $item['images'];
$it->description = $item['description'] ?? null;
$it->category_id = $category?->id;
$it->params = $item['params'] ?? [];
if (!$it->save()) {
$logger->log(Logger::LEVEL_INFO, "Can not save item: " . print_r($it->errors, 1));
return false;
} else {
$counter->countItems++;
return true;
}
} else {
// skip...
return false;
}
});
} catch (\Throwable $throwable) {
$logger->log(Logger::LEVEL_INFO, "Error while parsing items: " . $throwable->getMessage() . $throwable->getTraceAsString());
}
}
if (!($jobRun->settings['parse_category'] ?? false)) {
$numItems = Item::find()->where(['parser_job_run_id'=>$jobRun->id])->count();
$amount = $parserClass::getTariffRunPriceCoeff() * \Yii::$app->params['baseRunTariffPrice'] + $numItems * $parserClass::getTariffItemPriceCoeff() * \Yii::$app->params['baseItemTariffPrice'];
// TODO: correct amount to be equals to price_per_item * countItems
$paymentService->unholdPayment($payment, $amount);
}
$jobRun->date_finished = (new \DateTime())->format('c');
$jobRun->status = ParserJobRun::STATUS_FINISHED;
if (!$jobRun->save()) {
$logger->log(Logger::LEVEL_INFO, "Can not save jobRun: " . print_r($jobRun->errors, 1));
}
$this->notificationService->notify("JobRun " . $jobRun->id . " for parser job '" . $jobRun->parserJob->name . "' finished");
}
}