-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathParallelImportCommand.php
More file actions
133 lines (118 loc) · 3.95 KB
/
Copy pathParallelImportCommand.php
File metadata and controls
133 lines (118 loc) · 3.95 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
<?php
/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace MauticPlugin\MauticCustomImportBundle\Command;
use Symfony\Component\Console\Command\Command;
use Mautic\CoreBundle\Helper\ProgressBarHelper;
use MauticPlugin\MauticCustomImportBundle\Exception\InvalidImportException;
use MauticPlugin\MauticCustomImportBundle\Import\CustomImportFactory;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ParallelImportCommand extends Command
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var CustomImportFactory
*/
private $customImportFactory;
/**
* ParallelImportCommand constructor.
*
* @param CustomImportFactory $customImportFactory
* @param TranslatorInterface $translator
*/
public function __construct(CustomImportFactory $customImportFactory, TranslatorInterface $translator)
{
parent::__construct();
$this->translator = $translator;
$this->customImportFactory = $customImportFactory;
// parent constructor removed for Mautic 5; locking deps are DI'd into parent in core
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('mautic:import:parallel')
->setDescription('Parallel import for Mautic')
->setHelp('This command processed parallel imports')
->addOption(
'--output_from_import',
null,
InputOption::VALUE_NONE,
'Display output from each mautic:import processes.'
);
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$processSet = $this->customImportFactory->processParallelImport();
$this->processParallelCommandsOutput($input, $output, $processSet);
} catch (InvalidImportException $importException) {
$output->writeln($importException->getMessage());
}
return 0;
}
/**
* @param Input $input
* @param Output $output
* @param array $processSet
*/
private function processParallelCommandsOutput(Input $input, Output $output, array $processSet)
{
$processCount = count($processSet);
if (empty($processCount)) {
return;
}
$output->writeln(
sprintf(
"<info>%s</info>",
$this->translator->trans('mautic.custom.import.csv.import.parallel.start', ['%s' => $processCount])
)
);
$progress = ProgressBarHelper::init($output, count($processSet));
$progress->start();
$response = [];
while (!empty($processSet)) {
foreach ($processSet as $index => &$process) {
$process->checkTimeout();
// Not running, let's display result
if (!$process->isRunning()) {
unset($processSet[$index]);
$progress->advance();
if (!$process->isSuccessful()) {
$output->writeln(sprintf("<error>%s</error>", $process->getErrorOutput()));
} else {
$response[] = $process->getOutput();
}
}
}
sleep(1);
}
$progress->finish();
if ($input->getOption('output_from_import')) {
$output->writeln('');
foreach ($response as $res) {
$output->writeln($res);
}
}
}
}