| Conditions | 31 |
| Paths | 3400 |
| Total Lines | 253 |
| Code Lines | 144 |
| Lines | 20 |
| Ratio | 7.91 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | protected function execute(InputInterface $input, OutputInterface $output) |
||
|
|
|||
| 56 | { |
||
| 57 | $start = microtime(true); |
||
| 58 | |||
| 59 | $this->setOutput($output); |
||
| 60 | $this->setVerbosity($output->getVerbosity()); |
||
| 61 | |||
| 62 | $isChild = $input->getOption('child'); |
||
| 63 | |||
| 64 | if ($isChild) { |
||
| 65 | $this->setVerbosity(self::VERBOSITY_CHILD); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->getContainer()->get('ez_migration_bundle.step_executed_listener.tracing')->setOutput($output); |
||
| 69 | |||
| 70 | // q: is it worth declaring a new, dedicated migration service ? |
||
| 71 | $migrationService = $this->getMigrationService(); |
||
| 72 | $migrationService->setLoader($this->getContainer()->get('ez_migration_bundle.loader.filesystem_recursive')); |
||
| 73 | |||
| 74 | $toExecute = $this->buildMigrationsList($input->getOption('path'), $migrationService, $isChild); |
||
| 75 | |||
| 76 | if (!count($toExecute)) { |
||
| 77 | $this->writeln('<info>No migrations to execute</info>'); |
||
| 78 | return 0; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (!$isChild) { |
||
| 82 | |||
| 83 | $paths = $this->groupMigrationsByPath($toExecute); |
||
| 84 | $this->printMigrationsList($toExecute, $input, $output, $paths); |
||
| 85 | |||
| 86 | // ask user for confirmation to make changes |
||
| 87 | if (!$this->askForConfirmation($input, $output)) { |
||
| 88 | return 0; |
||
| 89 | } |
||
| 90 | |||
| 91 | $concurrency = $input->getOption('concurrency'); |
||
| 92 | $this->writeln("Executing migrations using ".count($paths)." processes with a concurrency of $concurrency"); |
||
| 93 | |||
| 94 | $kernel = $this->getContainer()->get('kernel'); |
||
| 95 | $builder = new ProcessBuilder(); |
||
| 96 | $executableFinder = new PhpExecutableFinder(); |
||
| 97 | if (false !== ($php = $executableFinder->find())) { |
||
| 98 | $builder->setPrefix($php); |
||
| 99 | } |
||
| 100 | // mandatory args and options |
||
| 101 | $builderArgs = array( |
||
| 102 | $_SERVER['argv'][0], // sf console |
||
| 103 | self::COMMAND_NAME, // name of sf command. Can we get it from the Application instead of hardcoding? |
||
| 104 | '--env=' . $kernel-> getEnvironment(), // sf env |
||
| 105 | '--child' |
||
| 106 | ); |
||
| 107 | // 'optional' options |
||
| 108 | // note: options 'clear-cache' we never propagate |
||
| 109 | if (!$kernel->isDebug()) { |
||
| 110 | $builderArgs[] = '--no-debug'; |
||
| 111 | } |
||
| 112 | if ($input->getOption('default-language')) { |
||
| 113 | $builderArgs[]='--default-language='.$input->getOption('default-language'); |
||
| 114 | } |
||
| 115 | if ($input->getOption('no-transactions')) { |
||
| 116 | $builderArgs[]='--no-transactions'; |
||
| 117 | } |
||
| 118 | if ($input->getOption('siteaccess')) { |
||
| 119 | $builderArgs[]='--siteaccess='.$input->getOption('siteaccess'); |
||
| 120 | } |
||
| 121 | if ($input->getOption('ignore-failures')) { |
||
| 122 | $builderArgs[]='--ignore-failures'; |
||
| 123 | } |
||
| 124 | if ($input->getOption('separate-process')) { |
||
| 125 | $builderArgs[]='--separate-process'; |
||
| 126 | } |
||
| 127 | $processes = array(); |
||
| 128 | /** @var MigrationDefinition $migrationDefinition */ |
||
| 129 | foreach($paths as $path => $count) { |
||
| 130 | $this->writeln("<info>Queueing processing of: $path ($count migrations)</info>", OutputInterface::VERBOSITY_VERBOSE); |
||
| 131 | |||
| 132 | $process = $builder |
||
| 133 | ->setArguments(array_merge($builderArgs, array('--path=' . $path))) |
||
| 134 | ->getProcess(); |
||
| 135 | |||
| 136 | $this->writeln('<info>Command: ' . $process->getCommandLine() . '</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 137 | |||
| 138 | // allow long migrations processes by default |
||
| 139 | $process->setTimeout(86400); |
||
| 140 | $processes[] = $process; |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->writeln("Starting queued processes..."); |
||
| 144 | |||
| 145 | $this->migrationsDone = array(0, 0, 0); |
||
| 146 | |||
| 147 | $processManager = new ProcessManager(); |
||
| 148 | $processManager->runParallel($processes, $concurrency, 500, array($this, 'onSubProcessOutput')); |
||
| 149 | |||
| 150 | $failed = 0; |
||
| 151 | foreach ($processes as $i => $process) { |
||
| 152 | if (!$process->isSuccessful()) { |
||
| 153 | $output->writeln("\n<error>Subprocess $i failed! Reason: " . $process->getErrorOutput() . "</error>\n"); |
||
| 154 | $failed++; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | View Code Duplication | if ($input->getOption('clear-cache')) { |
|
| 159 | $command = $this->getApplication()->find('cache:clear'); |
||
| 160 | $inputArray = new ArrayInput(array('command' => 'cache:clear')); |
||
| 161 | $command->run($inputArray, $output); |
||
| 162 | } |
||
| 163 | |||
| 164 | $time = microtime(true) - $start; |
||
| 165 | |||
| 166 | $this->writeln('<info>'.$this->migrationsDone[0].' migrations executed, '.$this->migrationsDone[1].' failed, '.$this->migrationsDone[2].' skipped</info>'); |
||
| 167 | $this->writeln("<info>Import finished</info>\n"); |
||
| 168 | |||
| 169 | // since we use subprocesses, we can not measure max memory used |
||
| 170 | $this->writeln("Time taken: ".sprintf('%.2f', $time)." secs"); |
||
| 171 | |||
| 172 | return $failed; |
||
| 173 | |||
| 174 | } else { |
||
| 175 | |||
| 176 | // @todo disable signal slots that are harmful during migrations, if any |
||
| 177 | |||
| 178 | if ($input->getOption('separate-process')) { |
||
| 179 | $builder = new ProcessBuilder(); |
||
| 180 | $executableFinder = new PhpExecutableFinder(); |
||
| 181 | if (false !== $php = $executableFinder->find()) { |
||
| 182 | $builder->setPrefix($php); |
||
| 183 | } |
||
| 184 | // mandatory args and options |
||
| 185 | $builderArgs = array( |
||
| 186 | $_SERVER['argv'][0], // sf console |
||
| 187 | MigrateCommand::COMMAND_NAME, // name of sf command |
||
| 188 | '--env=' . $this->getContainer()->get('kernel')->getEnvironment(), // sf env |
||
| 189 | '--child' |
||
| 190 | ); |
||
| 191 | // 'optional' options |
||
| 192 | // note: options 'clear-cache', 'ignore-failures' and 'no-transactions' we never propagate |
||
| 193 | if ($input->getOption('default-language')) { |
||
| 194 | $builderArgs[] = '--default-language=' . $input->getOption('default-language'); |
||
| 195 | } |
||
| 196 | if ($input->getOption('no-transactions')) { |
||
| 197 | $builderArgs[] = '--no-transactions'; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | $failed = 0; |
||
| 202 | $executed = 0; |
||
| 203 | $skipped = 0; |
||
| 204 | $total = count($toExecute); |
||
| 205 | |||
| 206 | foreach ($toExecute as $name => $migrationDefinition) { |
||
| 207 | // let's skip migrations that we know are invalid - user was warned and he decided to proceed anyway |
||
| 208 | if ($migrationDefinition->status == MigrationDefinition::STATUS_INVALID) { |
||
| 209 | $this->writeln("<comment>Skipping migration (invalid definition?) Path: ".$migrationDefinition->path."</comment>", self::VERBOSITY_CHILD); |
||
| 210 | $skipped++; |
||
| 211 | continue; |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($input->getOption('separate-process')) { |
||
| 215 | |||
| 216 | $process = $builder |
||
| 217 | ->setArguments(array_merge($builderArgs, array('--path=' . $migrationDefinition->path))) |
||
| 218 | ->getProcess(); |
||
| 219 | |||
| 220 | // allow long migrations processes by default |
||
| 221 | $process->setTimeout(86400); |
||
| 222 | // and give no feedback to the user |
||
| 223 | $process->run( |
||
| 224 | function($type, $buffer) { |
||
| 225 | //echo $buffer; |
||
| 226 | } |
||
| 227 | ); |
||
| 228 | |||
| 229 | try { |
||
| 230 | |||
| 231 | if (!$process->isSuccessful()) { |
||
| 232 | throw new \Exception($process->getErrorOutput()); |
||
| 233 | } |
||
| 234 | |||
| 235 | // There are cases where the separate process dies halfway but does not return a non-zero code. |
||
| 236 | // That's why we should double-check here if the migration is still tagged as 'started'... |
||
| 237 | /** @var Migration $migration */ |
||
| 238 | $migration = $migrationService->getMigration($migrationDefinition->name); |
||
| 239 | |||
| 240 | View Code Duplication | if (!$migration) { |
|
| 241 | // q: shall we add the migration to the db as failed? In doubt, we let it become a ghost, disappeared without a trace... |
||
| 242 | throw new \Exception("After the separate process charged to execute the migration finished, the migration can not be found in the database any more."); |
||
| 243 | } else if ($migration->status == Migration::STATUS_STARTED) { |
||
| 244 | $errorMsg = "The separate process charged to execute the migration left it in 'started' state. Most likely it died halfway through execution."; |
||
| 245 | $migrationService->endMigration(New Migration( |
||
| 246 | $migration->name, |
||
| 247 | $migration->md5, |
||
| 248 | $migration->path, |
||
| 249 | $migration->executionDate, |
||
| 250 | Migration::STATUS_FAILED, |
||
| 251 | ($migration->executionError != '' ? ($errorMsg . ' ' . $migration->executionError) : $errorMsg) |
||
| 252 | )); |
||
| 253 | throw new \Exception($errorMsg); |
||
| 254 | } |
||
| 255 | |||
| 256 | $executed++; |
||
| 257 | |||
| 258 | } catch (\Exception $e) { |
||
| 259 | if ($input->getOption('ignore-failures')) { |
||
| 260 | $output->writeln("\n<error>Migration failed! Reason: " . $e->getMessage() . "</error>\n"); |
||
| 261 | $failed++; |
||
| 262 | continue; |
||
| 263 | } |
||
| 264 | $output->writeln("\n<error>Migration aborted! Path: " . $migrationDefinition->path . ", Reason: " . $e->getMessage() . "</error>"); |
||
| 265 | |||
| 266 | $missed = $total - $executed - $failed - $skipped; |
||
| 267 | $this->writeln("Migrations executed: $executed, failed: $failed, skipped: $skipped, to do: $missed"); |
||
| 268 | |||
| 269 | return 1; |
||
| 270 | } |
||
| 271 | |||
| 272 | } else { |
||
| 273 | |||
| 274 | try { |
||
| 275 | |||
| 276 | $migrationService->executeMigration( |
||
| 277 | $migrationDefinition, |
||
| 278 | !$input->getOption('no-transactions'), |
||
| 279 | $input->getOption('default-language') |
||
| 280 | ); |
||
| 281 | |||
| 282 | $executed++; |
||
| 283 | } catch(\Exception $e) { |
||
| 284 | $failed++; |
||
| 285 | if ($input->getOption('ignore-failures')) { |
||
| 286 | $this->writeln("<error>Migration failed! Path: " . $migrationDefinition->path . ", Reason: " . $e->getMessage() . "</error>", self::VERBOSITY_CHILD); |
||
| 287 | continue; |
||
| 288 | } |
||
| 289 | |||
| 290 | $this->writeln("<error>Migration aborted! Path: " . $migrationDefinition->path . ", Reason: " . $e->getMessage() . "</error>", self::VERBOSITY_CHILD); |
||
| 291 | |||
| 292 | $missed = $total - $executed - $failed - $skipped; |
||
| 293 | $this->writeln("Migrations executed: $executed, failed: $failed, skipped: $skipped, to do: $missed"); |
||
| 294 | |||
| 295 | return 1; |
||
| 296 | } |
||
| 297 | |||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | $this->writeln("Migrations executed: $executed, failed: $failed, skipped: $skipped", self::VERBOSITY_CHILD); |
||
| 302 | |||
| 303 | // We do not return an error code > 0 if migrations fail, but only on proper fatals. |
||
| 304 | // The parent will analyze the output of the child process to gather the number of executed/failed migrations anyway |
||
| 305 | //return $failed; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 437 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: