Skip to content

Commit

Permalink
Remove vestiges of Backend invoke system (#4964)
Browse files Browse the repository at this point in the history
* remove vestiges of Backend invoke system.

* PHPCS
  • Loading branch information
weitzman committed Dec 31, 2021
1 parent fbd6184 commit 21eb502
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 238 deletions.
118 changes: 0 additions & 118 deletions includes/backend.inc

This file was deleted.

5 changes: 0 additions & 5 deletions includes/batch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ class DrushBatchContext extends ArrayObject {
/**
* Process a Drupal batch by spawning multiple Drush processes.
*
* This function will include the correct batch engine for the current
* major version of Drupal, and will make use of the drush_backend_invoke
* system to spawn multiple worker threads to handle the processing of
* the current batch, while keeping track of available memory.
*
* The batch system will process as many batch sets as possible until
* the entire batch has been completed or 60% of the available memory
* has been used.
Expand Down
8 changes: 0 additions & 8 deletions src/Config/DrushConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ public function simulate()
return $this->get(\Robo\Config\Config::SIMULATE);
}

/**
* Return 'true' if we are in backend mode.
*/
public function backend()
{
return $this->get(PreflightArgs::BACKEND);
}

/**
* Return the list of paths to active Drush configuration files.
* @return array
Expand Down
5 changes: 0 additions & 5 deletions src/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
*
* This logger is designed such that it can be provided to
* other libraries that log to a Psr\Log\LoggerInterface.
* As such, it takes responsibility for passing log messages
* to backend invoke, as necessary (c.f. drush_backend_packet()).
*
* Drush supports all of the required log levels from Psr\Log\LogLevel,
* and also defines its own. See Drush\Log\LogLevel.
*
* Those who may wish to change the way logging works in Drush
* should therefore NOT attempt to replace this logger with their
Expand Down
1 change: 0 additions & 1 deletion src/Preflight/LegacyPreflight.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public static function includeCode($drushBasePath): void
require_once $drushBasePath . '/includes/preflight.inc';
require_once $drushBasePath . '/includes/bootstrap.inc';
require_once $drushBasePath . '/includes/environment.inc';
require_once $drushBasePath . '/includes/backend.inc';
require_once $drushBasePath . '/includes/drush.inc';
require_once $drushBasePath . '/includes/batch.inc';
require_once $drushBasePath . '/includes/drupal.inc';
Expand Down
123 changes: 22 additions & 101 deletions src/Preflight/PreflightArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
class PreflightArgs extends Config implements PreflightArgsInterface
{

/**
* @var array $args Remaining arguments not handled by the preprocessor
*/
Expand All @@ -42,18 +43,29 @@ public function setHomeDir(string $homeDir): void
}

const DRUSH_CONFIG_PATH_NAMESPACE = 'drush.paths';

const DRUSH_RUNTIME_CONTEXT_NAMESPACE = 'runtime.contxt';

const ALIAS = 'alias';

const ALIAS_PATH = 'alias-path';

const COMMAND_PATH = 'include';

const CONFIG_PATH = 'config';

const COVERAGE_FILE = 'coverage-file';

const LOCAL = 'local';

const ROOT = 'root';

const URI = 'uri';

const SIMULATE = 'simulate';
const BACKEND = 'backend';

const STRICT = 'strict';

const DEBUG = 'preflight-debug';

/**
Expand Down Expand Up @@ -86,7 +98,6 @@ public function optionsWithValues(): array
'--local' => 'setLocal',
'--simulate' => 'setSimulate',
'-s' => 'setSimulate',
'--backend=' => 'setBackend',
'--drush-coverage=' => 'setCoverageFile',
'--strict=' => 'setStrict',
'--help' => 'adjustHelpOption',
Expand All @@ -113,9 +124,8 @@ public function adjustHelpOption(): void
protected function optionConfigMap(): array
{
return [
self::SIMULATE => \Robo\Config\Config::SIMULATE,
self::BACKEND => self::BACKEND,
self::LOCAL => self::DRUSH_RUNTIME_CONTEXT_NAMESPACE . '.' . self::LOCAL,
self::SIMULATE => \Robo\Config\Config::SIMULATE,
self::LOCAL => self::DRUSH_RUNTIME_CONTEXT_NAMESPACE . '.' . self::LOCAL,
];
}

Expand All @@ -127,9 +137,9 @@ protected function optionConfigMap(): array
protected function optionConfigPathMap(): array
{
return [
self::ALIAS_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::ALIAS_PATH,
self::CONFIG_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::CONFIG_PATH,
self::COMMAND_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::COMMAND_PATH,
self::ALIAS_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::ALIAS_PATH,
self::CONFIG_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::CONFIG_PATH,
self::COMMAND_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::COMMAND_PATH,
];
}

Expand All @@ -147,7 +157,7 @@ public function applyToConfig(ConfigInterface $config): void
// Merging as they are lists.
foreach ($this->optionConfigPathMap() as $option_key => $config_key) {
$cli_paths = $this->get($option_key, []);
$config_paths = (array) $config->get($config_key, []);
$config_paths = (array)$config->get($config_key, []);

$merged_paths = array_unique(array_merge($cli_paths, $config_paths));
$config->set($config_key, $merged_paths);
Expand Down Expand Up @@ -193,6 +203,7 @@ public function setCommandName($commandName): void
{
$this->commandName = $commandName;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -410,27 +421,6 @@ public function setSimulate($simulate): self
return $this->set(self::SIMULATE, $simulate);
}

/**
* Determine whether Drush was placed in simulated mode.
*/
public function isBackend()
{
return $this->get(self::BACKEND);
}

/**
* Set backend mode
*/
public function setBackend(bool $backend)
{
if ($backend == 'json') {
// Remap to --format. See \Drush\Commands\sql\SqlSyncCommands::dump.
$this->addArg('--format=json');
} else {
return $this->set(self::BACKEND, true);
}
}

/**
* Get the path to the coverage file.
*/
Expand Down Expand Up @@ -470,6 +460,7 @@ public function setStrict(bool $strict): self
* just the option name of any item that is an option.
*
* @param array $argv e.g. ['foo', '--bar=baz', 'boz']
*
* @return string[] e.g. ['bar']
*/
protected function getOptionNameList(array $argv): array
Expand Down Expand Up @@ -500,81 +491,11 @@ public function createInput(): \Symfony\Component\Console\Input\InputInterface
{
// In strict mode (the default), create an ArgvInput. When
// strict mode is disabled, create a more forgiving input object.
if ($this->isStrict() && !$this->isBackend()) {
if ($this->isStrict()) {
return new DrushArgvInput($this->args());
}

// If in backend mode, read additional options from stdin.
// TODO: Maybe reading stdin options should be the responsibility of some
// backend manager class? Could be called from preflight and injected here.
$input = new LessStrictArgvInput($this->args());
$input->injectAdditionalOptions($this->readStdinOptions());

return $input;
}

/**
* Read options fron STDIN during POST requests.
*
* This function will read any text from the STDIN pipe,
* and attempts to generate an associative array if valid
* JSON was received.
*
* @return
* An associative array of options, if successfull. Otherwise an empty array.
*/
protected function readStdinOptions()
{
// If we move this method to a backend manager, then testing for
// backend mode will be the responsibility of the caller.
if (!$this->isBackend()) {
return [];
}

$fp = fopen('php://stdin', 'r');
// Windows workaround: we cannot count on stream_get_contents to
// return if STDIN is reading from the keyboard. We will therefore
// check to see if there are already characters waiting on the
// stream (as there always should be, if this is a backend call),
// and if there are not, then we will exit.
// This code prevents drush from hanging forever when called with
// --backend from the commandline; however, overall it is still
// a futile effort, as it does not seem that backend invoke can
// successfully write data to that this function can read,
// so the argument list and command always come out empty. :(
// Perhaps stream_get_contents is the problem, and we should use
// the technique described here:
// http://bugs.php.net/bug.php?id=30154
// n.b. the code in that issue passes '0' for the timeout in stream_select
// in a loop, which is not recommended.
// Note that the following DOES work:
// drush ev 'print(json_encode(array("test" => "XYZZY")));' | drush status --backend
// So, redirecting input is okay, it is just the proc_open that is a problem.
if (drush_is_windows()) {
// Note that stream_select uses reference parameters, so we need variables (can't pass a constant NULL)
$read = [$fp];
$write = null;
$except = null;
// Question: might we need to wait a bit for STDIN to be ready,
// even if the process that called us immediately writes our parameters?
// Passing '100' for the timeout here causes us to hang indefinitely
// when called from the shell.
$changed_streams = stream_select($read, $write, $except, 0);
// Return on error or no changed streams (0).
// Oh, according to http://php.net/manual/en/function.stream-select.php,
// stream_select will return FALSE for streams returned by proc_open.
// That is not applicable to us, is it? Our stream is connected to a stream
// created by proc_open, but is not a stream returned by proc_open.
if ($changed_streams < 1) {
return [];
}
}
stream_set_blocking($fp, false);
$string = stream_get_contents($fp);
fclose($fp);
if (trim($string)) {
return json_decode($string, true);
}
return [];
}
}

0 comments on commit 21eb502

Please sign in to comment.