-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRun.php
More file actions
112 lines (78 loc) · 3.11 KB
/
Copy pathRun.php
File metadata and controls
112 lines (78 loc) · 3.11 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
<?php declare(strict_types = 1);
namespace Spameri\Elastic\Import;
class Run
{
private \Symfony\Component\Console\Output\OutputInterface $output;
protected string $runName;
protected string $fileName;
private \Symfony\Component\Console\Helper\ProgressBar $progressBar;
/**
* @throws \ReflectionException
*/
public function __construct(
protected string $logDir,
private readonly \Spameri\Elastic\Import\LoggerHandlerInterface $loggerHandler,
private readonly \Spameri\Elastic\Import\LockInterface $lock,
private readonly \Spameri\Elastic\Import\RunHandlerInterface $runHandler,
private readonly \Spameri\Elastic\Import\DataProviderInterface $dataProvider,
private readonly \Spameri\Elastic\Import\PrepareImportDataInterface $prepareImportData,
private readonly \Spameri\Elastic\Import\DataImportInterface $dataImport,
private readonly \Spameri\Elastic\Import\AfterImportInterface $afterImport,
)
{
$this->runName = (new \ReflectionClass($this))->getShortName();
$this->lock->setRunName($this->runName);
$this->setUpLogger($logDir);
}
public function setOutput(\Symfony\Component\Console\Output\OutputInterface $output): void
{
$this->output = $output;
}
protected function setUpLogger(string $logDir): void
{
$directory = $logDir;
\Nette\Utils\FileSystem::createDir($directory);
$directory .= \DIRECTORY_SEPARATOR;
$directory .= $this->runName;
\Nette\Utils\FileSystem::createDir($directory);
$directory .= \DIRECTORY_SEPARATOR;
$directory .= (new \DateTime())->format('Y-m-d');
\Nette\Utils\FileSystem::createDir($directory);
$this->fileName = (new \DateTime())->format('H-i-s');
}
public function execute(
\Spameri\Elastic\Import\Run\Options $options,
): void
{
$this->lock->acquire($options->lockDuration());
$this->initializeProgressBar($this->dataProvider->count($options));
$data = $this->dataProvider->provide($options);
foreach ($data as $item) {
try {
$this->loggerHandler->logItemStart($item);
$prepared = $this->prepareImportData->prepare($item);
$this->loggerHandler->logPrepared($prepared);
$response = $this->dataImport->import($prepared);
$this->loggerHandler->logResponse($response);
$this->afterImport->process($item, $response);
$this->runHandler->advance($this->runName, $this->progressBar, $prepared);
$this->lock->extend($options->lockDuration());
} catch (\Spameri\Elastic\Import\Exception\Omit $exception) {
$this->loggerHandler->logOmitException($exception);
} catch (\Spameri\Elastic\Import\Exception\Error $exception) {
$this->loggerHandler->logErrorException($exception);
} catch (\Spameri\Elastic\Import\Exception\Fatal $exception) {
$this->loggerHandler->logFatalException($exception);
}
}
$this->runHandler->finish($this->runName, $this->progressBar, $prepared ?? NULL);
$this->lock->release();
$this->loggerHandler->logFinish();
}
public function initializeProgressBar(int $maxCount): void
{
$this->progressBar = new \Symfony\Component\Console\Helper\ProgressBar($this->output, $maxCount);
$this->progressBar->setFormat('debug');
$this->progressBar->start($maxCount);
}
}