Completed
Push — master ( 97fb46...770b11 )
by Gaetano
15:34 queued 07:06
created

executeMigrationInSeparateProcess()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 65
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 21.5145

Importance

Changes 0
Metric Value
cc 10
eloc 35
nc 12
nop 5
dl 0
loc 65
ccs 19
cts 37
cp 0.5135
crap 21.5145
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Command;
4
5
use Symfony\Component\Console\Input\ArrayInput;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
use Symfony\Component\Process\PhpExecutableFinder;
12
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
13
use Kaliop\eZMigrationBundle\API\Value\Migration;
14
use Kaliop\eZMigrationBundle\API\Exception\AfterMigrationExecutionException;
15
use Kaliop\eZMigrationBundle\Core\MigrationService;
16
use Kaliop\eZMigrationBundle\Core\Process\Process;
17
use Kaliop\eZMigrationBundle\Core\Process\ProcessBuilder;
18
19
/**
20
 * Command to execute the available migration definitions.
21
 */
22
class MigrateCommand extends AbstractCommand
23
{
24
    // in between QUIET and NORMAL
25
    const VERBOSITY_CHILD = 0.5;
26
27
    protected $subProcessTimeout = 86400;
28
    protected $subProcessErrorString = '';
29
30
    const COMMAND_NAME = 'kaliop:migration:migrate';
31
32
    /**
33
     * Set up the command.
34
     *
35
     * Define the name, options and help text.
36
     */
37 94
    protected function configure()
38
    {
39 94
        parent::configure();
40
41
        $this
42 94
            ->setName(self::COMMAND_NAME)
43 94
            ->setAliases(array('kaliop:migration:update'))
44 94
            ->setDescription('Execute available migration definitions.')
45
            // nb: when adding options, remember to forward them to sub-commands executed in 'separate-process' mode
46 94
            ->addOption('admin-login', 'a', InputOption::VALUE_REQUIRED, "Login of admin account used whenever elevated privileges are needed (user id 14 used by default)")
47 94
            ->addOption('clear-cache', 'c', InputOption::VALUE_NONE, "Clear the cache after the command finishes")
48 94
            ->addOption('default-language', 'l', InputOption::VALUE_REQUIRED, "Default language code that will be used if no language is provided in migration steps")
49 94
            ->addOption('force', 'f', InputOption::VALUE_NONE, "Force (re)execution of migrations already DONE, SKIPPED or FAILED. Use with great care!")
50 94
            ->addOption('ignore-failures', 'i', InputOption::VALUE_NONE, "Keep executing migrations even if one fails")
51 94
            ->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question")
52 94
            ->addOption('no-transactions', 'u', InputOption::VALUE_NONE, "Do not use a repository transaction to wrap each migration. Unsafe, but needed for legacy slot handlers")
53 94
            ->addOption('path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, "The directory or file to load the migration definitions from")
54 94
            ->addOption('separate-process', 'p', InputOption::VALUE_NONE, "Use a separate php process to run each migration. Safe if your migration leak memory. A tad slower")
55 94
            ->addOption('force-sigchild-enabled', null, InputOption::VALUE_NONE, "When using a separate php process to run each migration, tell Symfony that php was compiled with --enable-sigchild option")
56 94
            ->addOption('survive-disconnected-tty', null, InputOption::VALUE_NONE, "Keep on executing migrations even if the tty where output is written to gets removed. Useful if you run the command over an unstable ssh connection")
57 94
            ->addOption('child', null, InputOption::VALUE_NONE, "*DO NOT USE* Internal option for when forking separate processes")
58 94
            ->setHelp(<<<EOT
59 94
The <info>kaliop:migration:migrate</info> command loads and executes migrations:
60
61
    <info>./ezpublish/console kaliop:migration:migrate</info>
62
63
You can optionally specify the path to migration definitions with <info>--path</info>:
64
65
    <info>./ezpublish/console kaliop:migrations:migrate --path=/path/to/bundle/version_directory --path=/path/to/bundle/version_directory/single_migration_file</info>
66
EOT
67
            );
68 94
    }
69
70
    /**
71
     * Execute the command.
72
     *
73
     * @param InputInterface $input
74
     * @param OutputInterface $output
75
     * @return null|int null or 0 if everything went fine, or an error code
76
     */
77 58
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79 58
        $start = microtime(true);
80
81 58
        $this->setOutput($output);
82 58
        $this->setVerbosity($output->getVerbosity());
83
84 58
        if ($input->getOption('child')) {
85
            $this->setVerbosity(self::VERBOSITY_CHILD);
86
        }
87
88 58
        $this->getContainer()->get('ez_migration_bundle.step_executed_listener.tracing')->setOutput($output);
89
90 58
        $migrationService = $this->getMigrationService();
91
92 58
        $force = $input->getOption('force');
93
94 58
        $toExecute = $this->buildMigrationsList($input->getOption('path'), $migrationService, $force);
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('path') can also be of type boolean and string; however, parameter $paths of Kaliop\eZMigrationBundle...::buildMigrationsList() does only seem to accept string[], maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        $toExecute = $this->buildMigrationsList(/** @scrutinizer ignore-type */ $input->getOption('path'), $migrationService, $force);
Loading history...
95
96 58
        if (!count($toExecute)) {
97
            $output->writeln('<info>No migrations to execute</info>');
98
            return 0;
99
        }
100
101 58
        $this->printMigrationsList($toExecute, $input, $output);
102
103
        // ask user for confirmation to make changes
104 58
        if (!$this->askForConfirmation($input, $output)) {
105
            return 0;
106
        }
107
108 58
        if ($input->getOption('separate-process')) {
109 2
            $builder = new ProcessBuilder();
110 2
            $executableFinder = new PhpExecutableFinder();
111 2
            if (false !== $php = $executableFinder->find()) {
112 2
                $builder->setPrefix($php);
113
            }
114 2
            $builderArgs = $this->createChildProcessArgs($input);
115
        }
116
117
        // For cli scripts, this means: do not die if anyone yanks out our stdout.
118 58
        if ($input->getOption('survive-disconnected-tty')) {
119 1
            ignore_user_abort(true);
120
        }
121
122
        // allow forcing handling of sigchild. Useful on eg. Debian and Ubuntu
123 58
        if ($input->getOption('force-sigchild-enabled')) {
124 1
            Process::forceSigchildEnabled(true);
125
        }
126
127 58
        $aborted = false;
128 58
        $executed = 0;
129 58
        $failed = 0;
130 58
        $skipped = 0;
131 58
        $total = count($toExecute);
132
133
        /** @var MigrationDefinition $migrationDefinition */
134 58
        foreach ($toExecute as $name => $migrationDefinition) {
135
136
            // let's skip migrations that we know are invalid - user was warned and he decided to proceed anyway
137 58
            if ($migrationDefinition->status == MigrationDefinition::STATUS_INVALID) {
138 9
                $output->writeln("<comment>Skipping $name</comment>");
139 9
                $skipped++;
140 9
                continue;
141
            }
142
143 49
            $this->writeln("<info>Processing $name</info>");
144
145 49
            if ($input->getOption('separate-process')) {
146
147
                try {
148
                    /// @todo in quiet mode, we could suppress output of the sub-command...
149 2
                    $this->executeMigrationInSeparateProcess($migrationDefinition, $migrationService, $builder, $builderArgs, true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $builderArgs does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $builder does not seem to be defined for all execution paths leading up to this point.
Loading history...
150
151 2
                    $executed++;
152
                } catch (\Exception $e) {
153
                    $failed++;
154
155
156
                    $errorMessage = $e->getMessage();
157
                    // we probably have already echoed the error message while the subprocess was executing, avoid repeating it
158
                    if ($errorMessage != $this->subProcessErrorString) {
159
                        if ($e instanceof AfterMigrationExecutionException) {
160
                            $errorMessage = "Failure after migration end! Reason: " . $errorMessage;
161
                        } else {
162
                            $errorMessage = "Migration failed! Reason: " . $errorMessage;
163
                        }
164
165
                        $this->writeErrorln("\n<error>$errorMessage</error>");
166
                    }
167
168
                    if (!$input->getOption('ignore-failures')) {
169
                        $aborted = true;
170 2
                        break;
171
                    }
172
                }
173
174
            } else {
175
176
                try {
177 47
                    $this->executeMigrationInProcess($migrationDefinition, $force, $migrationService, $input);
178
179 39
                    $executed++;
180 8
                } catch (\Exception $e) {
181 8
                    $failed++;
182
183 8
                    $this->writeErrorln("\n<error>Migration failed! Reason: " . $e->getMessage() . "</error>");
184
185 8
                    if (!$input->getOption('ignore-failures')) {
186 8
                        $aborted = true;
187 49
                        break;
188
                    }
189
                }
190
191
            }
192
        }
193
194 58
        if ($aborted) {
195 8
            $this->writeErrorln("\n<error>Migration execution aborted</error>");
196
        } else {
197 50
            if ($input->getOption('clear-cache')) {
198
                $command = $this->getApplication()->find('cache:clear');
199
                $inputArray = new ArrayInput(array('command' => 'cache:clear'));
200
                $command->run($inputArray, $output);
201
            }
202
        }
203
204 58
        $missed = $total - $executed - $failed - $skipped;
205 58
        $this->writeln("\nExecuted $executed migrations, failed $failed, skipped $skipped" . ($missed ? ", missed $missed" : ''));
206
207 58
        $time = microtime(true) - $start;
208 58
        if ($input->getOption('separate-process')) {
209
            // in case of using subprocesses, we can not measure max memory used
210 2
            $this->writeln("<info>Time taken: ".sprintf('%.2f', $time)." secs</info>");
211
        } else {
212 56
            $this->writeln("<info>Time taken: ".sprintf('%.2f', $time)." secs, memory: ".sprintf('%.2f', (memory_get_peak_usage(true) / 1000000)). ' MB</info>');
213
        }
214
215 58
        return $failed;
216
    }
217
218
    /**
219
     * @param MigrationDefinition $migrationDefinition
220
     * @param bool $force
221
     * @param MigrationService $migrationService
222
     * @param InputInterface $input
223
     */
224 47
    protected function executeMigrationInProcess($migrationDefinition, $force, $migrationService, $input)
225
    {
226 47
        $migrationService->executeMigration(
227 47
            $migrationDefinition,
228 47
            !$input->getOption('no-transactions'),
229 47
            $input->getOption('default-language'),
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('default-language') can also be of type string[]; however, parameter $defaultLanguageCode of Kaliop\eZMigrationBundle...ice::executeMigration() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

229
            /** @scrutinizer ignore-type */ $input->getOption('default-language'),
Loading history...
230 47
            $input->getOption('admin-login'),
231 47
            $force
232
        );
233 39
    }
234
235
    /**
236
     * @param MigrationDefinition $migrationDefinition
237
     * @param MigrationService $migrationService
238
     * @param ProcessBuilder $builder
239
     * @param array $builderArgs
240
     * @param bool $feedback
241
     */
242 2
    protected function executeMigrationInSeparateProcess($migrationDefinition, $migrationService, $builder, $builderArgs, $feedback = true)
243
    {
244
        $process = $builder
245 2
            ->setArguments(array_merge($builderArgs, array('--path=' . $migrationDefinition->path)))
246 2
            ->getProcess();
247
248 2
        if ($feedback) {
249 2
            $this->writeln('<info>Executing: ' . $process->getCommandLine() . '</info>', OutputInterface::VERBOSITY_VERBOSE);
250
        }
251
252 2
        $this->subProcessErrorString = '';
253
254
        // allow long migrations processes by default
255 2
        $process->setTimeout($this->subProcessTimeout);
256
257
        // and give immediate feedback to the user...
258
        // NB: if the subprocess writes to stderr then terminates with non-0 exit code, this will lead us to echoing the
259
        // error text twice, once here and once at the end of execution of this command.
260
        // In order to avoid that, since we can not know at this time what the subprocess exit code will be, we should
261
        // somehow still print the error text now, and compare it to what we gt at the end...
262 2
        $process->run(
263 2
            $feedback ?
264 2
                function($type, $buffer) {
265 2
                    if ($type == 'err') {
266
                        $this->subProcessErrorString .= $buffer;
267
                    }
268 2
                    $this->errOutput->write($buffer, OutputInterface::OUTPUT_RAW);
0 ignored issues
show
Bug introduced by
Symfony\Component\Consol...utInterface::OUTPUT_RAW of type integer is incompatible with the type boolean expected by parameter $newline of Symfony\Component\Consol...utputInterface::write(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

268
                    $this->errOutput->write($buffer, /** @scrutinizer ignore-type */ OutputInterface::OUTPUT_RAW);
Loading history...
269 2
                }
270
                :
271 2
                function($type, $buffer) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

271
                function(/** @scrutinizer ignore-unused */ $type, $buffer) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $buffer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

271
                function($type, /** @scrutinizer ignore-unused */ $buffer) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
272 2
                }
273
        );
274
275 2
        if (!$process->isSuccessful()) {
276
            $errorOutput = $process->getErrorOutput();
277
            /// @todo should we always add the exit code to the error message, even when $errorOutput is not null ?
278
            if ($errorOutput === '') {
279
                $errorOutput = "(separate process used to execute migration failed with no stderr output. Its exit code was: " . $process->getExitCode();
280
                if ($process->getExitCode() == -1) {
281
                    $errorOutput .= ". If you are using Debian or Ubuntu linux, please consider using the --force-sigchild-enabled option.";
282
                }
283
                $errorOutput .= ")";
284
            }
285
            throw new \Exception($errorOutput);
286
        }
287
288
        // There are cases where the separate process dies halfway but does not return a non-zero code.
289
        // That's why we double-check here if the migration is still tagged as 'started'...
290
        /** @var Migration $migration */
291 2
        $migration = $migrationService->getMigration($migrationDefinition->name);
292
293 2
        if (!$migration) {
0 ignored issues
show
introduced by
$migration is of type Kaliop\eZMigrationBundle\API\Value\Migration, thus it always evaluated to true.
Loading history...
294
            // q: shall we add the migration to the db as failed? In doubt, we let it become a ghost, disappeared without a trace...
295
            throw new \Exception("After the separate process charged to execute the migration finished, the migration can not be found in the database any more.");
296 2
        } else if ($migration->status == Migration::STATUS_STARTED) {
297
            $errorMsg = "The separate process charged to execute the migration left it in 'started' state. Most likely it died halfway through execution.";
298
            $migrationService->endMigration(New Migration(
299
                $migration->name,
300
                $migration->md5,
301
                $migration->path,
302
                $migration->executionDate,
303
                Migration::STATUS_FAILED,
304
                ($migration->executionError != '' ? ($errorMsg . ' ' . $migration->executionError) : $errorMsg)
305
            ));
306
            throw new \Exception($errorMsg);
307
        }
308 2
    }
309
310
    /**
311
     * @param string[] $paths
312
     * @param MigrationService $migrationService
313
     * @param bool $force when true, look not only for TODO migrations, but also DONE, SKIPPED, FAILED ones (we still omit STARTED and SUSPENDED ones)
314
     * @return MigrationDefinition[]
315
     *
316
     * @todo this does not scale well with many definitions or migrations
317
     */
318 58
    protected function buildMigrationsList($paths, $migrationService, $force = false)
319
    {
320 58
        $migrationDefinitions = $migrationService->getMigrationsDefinitions($paths);
321 58
        $migrations = $migrationService->getMigrations();
322
323 58
        $allowedStatuses = array(Migration::STATUS_TODO);
324 58
        if ($force) {
325 2
            $allowedStatuses = array_merge($allowedStatuses, array(Migration::STATUS_DONE, Migration::STATUS_FAILED, Migration::STATUS_SKIPPED));
326
        }
327
328
        // filter away all migrations except 'to do' ones
329 58
        $toExecute = array();
330 58
        foreach ($migrationDefinitions as $name => $migrationDefinition) {
331 58
            if (!isset($migrations[$name]) || (($migration = $migrations[$name]) && in_array($migration->status, $allowedStatuses))) {
332 58
                $toExecute[$name] = $migrationService->parseMigrationDefinition($migrationDefinition);
333
            }
334
        }
335
336
        // if user wants to execute 'all' migrations: look for some which are registered in the database even if not
337
        // found by the loader
338 58
        if (empty($paths)) {
339
            foreach ($migrations as $migration) {
340
                if (in_array($migration->status, $allowedStatuses) && !isset($toExecute[$migration->name])) {
341
                    $migrationDefinitions = $migrationService->getMigrationsDefinitions(array($migration->path));
342
                    if (count($migrationDefinitions)) {
343
                        $migrationDefinition = reset($migrationDefinitions);
344
                        $toExecute[$migration->name] = $migrationService->parseMigrationDefinition($migrationDefinition);
345
                    } else {
346
                        // q: shall we raise a warning here ?
347
                    }
348
                }
349
            }
350
        }
351
352 58
        ksort($toExecute);
353
354 58
        return $toExecute;
355
    }
356
357
    /**
358
     * @param MigrationDefinition[] $toExecute
359
     * @param InputInterface $input
360
     * @param OutputInterface $output
361
     *
362
     * @todo use a more compact output when there are *many* migrations
363
     */
364 58
    protected function printMigrationsList($toExecute, InputInterface $input, OutputInterface $output)
365
    {
366 58
        $data = array();
367 58
        $i = 1;
368 58
        foreach ($toExecute as $name => $migrationDefinition) {
369 58
            $notes = '';
370 58
            if ($migrationDefinition->status != MigrationDefinition::STATUS_PARSED) {
371 9
                $notes = '<error>' . $migrationDefinition->parsingError . '</error>';
372
            }
373 58
            $data[] = array(
374 58
                $i++,
375 58
                $name,
376 58
                $notes
377
            );
378
        }
379
380 58
        if (!$input->getOption('child')) {
381 58
            $table = new Table($output);
382
            $table
383 58
                ->setHeaders(array('#', 'Migration', 'Notes'))
384 58
                ->setRows($data);
385 58
            $table->render();
386
        }
387
388 58
        $this->writeln('');
389 58
    }
390
391 58
    protected function askForConfirmation(InputInterface $input, OutputInterface $output, $nonIteractiveOutput = "=============================================\n")
392
    {
393 58
        if ($input->isInteractive() && !$input->getOption('no-interaction')) {
394
            $dialog = $this->getHelperSet()->get('question');
395
            if (!$dialog->ask(
0 ignored issues
show
Bug introduced by
The method ask() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Symfony\Component\Console\Helper\QuestionHelper or Symfony\Component\Console\Helper\DialogHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

395
            if (!$dialog->/** @scrutinizer ignore-call */ ask(
Loading history...
396
                $input,
397
                $output,
398
                new ConfirmationQuestion('<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>', false)
399
            )
400
            ) {
401
                $output->writeln('<error>Migration execution cancelled!</error>');
402
                return 0;
403
            }
404
        } else {
405 58
            if ($nonIteractiveOutput != '') {
406 58
                $this->writeln("$nonIteractiveOutput");
407
            }
408
        }
409
410 58
        return 1;
411
    }
412
413
    /**
414
     * Returns the command-line arguments needed to execute a migration in a separate subprocess
415
     * (except path, which should be added after this call)
416
     * @param InputInterface $input
417
     * @return array
418
     * @todo check if it is a good idea to pass on the current verbosity
419
     */
420 2
    protected function createChildProcessArgs(InputInterface $input)
421
    {
422 2
        $kernel = $this->getContainer()->get('kernel');
423
424
        // mandatory args and options
425
        $builderArgs = array(
426 2
            $this->getConsoleFile(), // sf console
427 2
            self::COMMAND_NAME, // name of sf command. Can we get it from the Application instead of hardcoding?
428 2
            '--env=' . $kernel->getEnvironment(), // sf env
429 2
            '--child'
430
        );
431
        // sf/ez env options
432 2
        if (!$kernel->isDebug()) {
433 2
            $builderArgs[] = '--no-debug';
434
        }
435 2
        if ($input->getOption('siteaccess')) {
436
            $builderArgs[]='--siteaccess='.$input->getOption('siteaccess');
437
        }
438
        // 'optional' options
439
        // note: options 'clear-cache', 'ignore-failures', 'no-interaction', 'path', 'separate-process' and 'survive-disconnected-tty' we never propagate
440 2
        if ($input->getOption('admin-login')) {
441
            $builderArgs[] = '--admin-login=' . $input->getOption('admin-login');
442
        }
443 2
        if ($input->getOption('default-language')) {
444
            $builderArgs[] = '--default-language=' . $input->getOption('default-language');
445
        }
446 2
        if ($input->getOption('force')) {
447
            $builderArgs[] = '--force';
448
        }
449 2
        if ($input->getOption('no-transactions')) {
450
            $builderArgs[] = '--no-transactions';
451
        }
452
        // useful in case the subprocess has a migration step of type process/run
453 2
        if ($input->getOption('force-sigchild-enabled')) {
454
            $builderArgs[] = '--force-sigchild-enabled';
455
        }
456
457 2
        return $builderArgs;
458
    }
459
460
    /**
461
     * Returns the file-path of the symfony console in use, based on simple heuristics
462
     * @return string
463
     * @todo improve how we look for the console: we could fe. scan all of the files in the kernel dir, or look up the full process info based on its pid
464
     */
465 2
    protected function getConsoleFile()
466
    {
467 2
        if (strpos($_SERVER['argv'][0], 'phpunit') !== false) {
468 2
            $kernelDir = $this->getContainer()->get('kernel')->getRootDir();
469 2
            if (is_file("$kernelDir/console")) {
470 2
                return "$kernelDir/console";
471
            }
472
            throw new \Exception("Can not determine the name of the symfony console file in use for running ");
473
        }
474
475
        return $_SERVER['argv'][0]; // sf console
476
    }
477
}
478