Differential D18707 Diff 44912 src/infrastructure/storage/management/workflow/PhabricatorStorageManagementDumpWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/storage/management/workflow/PhabricatorStorageManagementDumpWorkflow.php
| Show First 20 Lines • Show All 209 Lines • ▼ Show 20 Lines | foreach ($targets as $target) { | ||||
| } else { | } else { | ||||
| $command = csprintf( | $command = csprintf( | ||||
| 'mysqldump %Ls -- %R %R', | 'mysqldump %Ls -- %R %R', | ||||
| $target_argv, | $target_argv, | ||||
| $target['database'], | $target['database'], | ||||
| $target['table']); | $target['table']); | ||||
| } | } | ||||
| $commands[] = $command; | $commands[] = array( | ||||
| 'command' => $command, | |||||
| 'database' => $target['database'], | |||||
| ); | |||||
| } | } | ||||
| // Decrease the CPU priority of this process so it doesn't contend with | // Decrease the CPU priority of this process so it doesn't contend with | ||||
| // other more important things. | // other more important things. | ||||
| if (function_exists('proc_nice')) { | if (function_exists('proc_nice')) { | ||||
| proc_nice(19); | proc_nice(19); | ||||
| } | } | ||||
| Show All 12 Lines | public function didExecute(PhutilArgumentParser $args) { | ||||
| if (($output_file !== null) && !$file) { | if (($output_file !== null) && !$file) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Failed to open file "%s" for writing.', | 'Failed to open file "%s" for writing.', | ||||
| $file)); | $file)); | ||||
| } | } | ||||
| $created = array(); | |||||
| try { | try { | ||||
| foreach ($commands as $command) { | foreach ($commands as $spec) { | ||||
| $future = new ExecFuture('%C', $command); | // Because we're dumping database-by-database, we need to generate our | ||||
| // own CREATE DATABASE and USE statements. | |||||
| $database = $spec['database']; | |||||
| $preamble = array(); | |||||
| if (!isset($created[$database])) { | |||||
| $preamble[] = | |||||
| "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `{$database}` ". | |||||
| "/*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin */;\n"; | |||||
| $created[$database] = true; | |||||
| } | |||||
| $preamble[] = "USE `{$database}`;\n"; | |||||
| $preamble = implode('', $preamble); | |||||
| $this->writeData($preamble, $file, $is_compress, $output_file); | |||||
| $future = new ExecFuture('%C', $spec['command']); | |||||
| $iterator = id(new FutureIterator(array($future))) | $iterator = id(new FutureIterator(array($future))) | ||||
| ->setUpdateInterval(0.100); | ->setUpdateInterval(0.100); | ||||
| foreach ($iterator as $ready) { | foreach ($iterator as $ready) { | ||||
| list($stdout, $stderr) = $future->read(); | list($stdout, $stderr) = $future->read(); | ||||
| $future->discardBuffers(); | $future->discardBuffers(); | ||||
| if (strlen($stderr)) { | if (strlen($stderr)) { | ||||
| fwrite(STDERR, $stderr); | fwrite(STDERR, $stderr); | ||||
| } | } | ||||
| if (strlen($stdout)) { | $this->writeData($stdout, $file, $is_compress, $output_file); | ||||
| if (!$file) { | |||||
| $ok = fwrite(STDOUT, $stdout); | |||||
| } else if ($is_compress) { | |||||
| $ok = gzwrite($file, $stdout); | |||||
| } else { | |||||
| $ok = fwrite($file, $stdout); | |||||
| } | |||||
| if ($ok !== strlen($stdout)) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Failed to write %d byte(s) to file "%s".', | |||||
| new PhutilNumber(strlen($stdout)), | |||||
| $output_file)); | |||||
| } | |||||
| } | |||||
| if ($ready !== null) { | if ($ready !== null) { | ||||
| $ready->resolvex(); | $ready->resolvex(); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| if (!$file) { | if (!$file) { | ||||
| Show All 23 Lines | try { | ||||
| } | } | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| private function writeData($data, $file, $is_compress, $output_file) { | |||||
| if (!strlen($data)) { | |||||
| return; | |||||
| } | |||||
| if (!$file) { | |||||
| $ok = fwrite(STDOUT, $data); | |||||
| } else if ($is_compress) { | |||||
| $ok = gzwrite($file, $data); | |||||
| } else { | |||||
| $ok = fwrite($file, $data); | |||||
| } | |||||
| if ($ok !== strlen($data)) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Failed to write %d byte(s) to file "%s".', | |||||
| new PhutilNumber(strlen($data)), | |||||
| $output_file)); | |||||
| } | |||||
| } | |||||
| } | } | ||||