Closed (fixed)
Project:
Drush
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Reporter:
Created:
13 Dec 2009 at 04:48 UTC
Updated:
17 Feb 2010 at 00:50 UTC
I wrote these 2 functions for migrate module's drush integration. This module needs to spawn a subshell when it looks like php is running out of memory. I imagine that might be a common requirement. The 2 functions re-issue the current command and options in a subshell.
I have not tried this while issueing a remote command.
Do folks think this is useful enough to add to drush itself? I'm inclined to say yes.
/*
* Spawn a subshell which runs the same command we are currently running.
*/
function drush_migrate_backend_invoke() {
$args = drush_get_arguments();
$options = drush_migrate_get_options();
drush_backend_invoke(implode(' ', $args), $options);
}
/**
* Get the value of all migrate related options. Used when spawning a subshell.
*
* @return
* An array of command specific options and their values.
*/
function drush_migrate_get_options() {
$options = array();
$command = drush_parse_command();
foreach ($command['options'] as $key => $value) {
// Strip leading --
$key = ltrim($key, '-');
$value = drush_get_option($key);
if (isset($value)) {
$options[$key] = $value;
}
}
return $options;
}
Comments
Comment #1
greg.1.anderson commentedThat
drush_backend_invoke(implode(' ', $args), $options);doesn't work very well. drush_backend_invoke will explode things apart again, but the splits won't always come at the right place if there are spaces embedded inside of the command. There's another flavor of backend invoke that keeps that args in array form to avoid this problem. When the site alias code uses this function, it assumes that the first arg is the command, which as you mention in #658432: drush_backend_invoke() assumes single word command names is wrong, but works, because backend invoke puts things back together correctly in the end.Comment #2
greg.1.anderson commented(Above, I meant to say "if there are spaces embedded inside of some of the args", of course.)
In #628996: Concurrently execute a single drush command on multiple sites #6 I posted a new patch that comes closer to unifying the re-dispatch that I'm doing with the version you have above. My code currently reads like this:
For the get_options routines, there are two differences.
$options = drush_get_context('options');instead of an empty array. This insures that all of the options from the command line will be passed along no matter what. This is important because any command-line arg that allowed you to bootstrap and dispatch to the command the first time is going to also be necessary the second time. This is also a fallback position for difference #2, below.drush_parse_command(), then I just skip the code that takes options from any context if they relate to the current command. This, combined with difference #1, allows remote commands to be executed even if the remote command exists only on the remote system, and not on the system it is being called from.There are also two differences in the re-dispatch code.
drush_get_arguments();and calls through to my routine.Other differences I either removed, or migrated elsewhere. The site alias code is now careful to put the 'root' and 'uri' options into the 'options' context instead of the 'alias' context, insuring that these are always picked up by the command re-dispatch function. This is important because backend invoke will pick up the values of root and uri from their bootstrapped values if they are not explicitly set in $data. The site alias code re-dispatches right after the drush bootstrap, so the drupal root and site variables have not been set yet, which would mess up the re-dispatch and cause backend invoke to fail if root and uri were not found inside of $data.
Comment #3
greg.1.anderson commentedIn addition to
drush_do_command_redispatch, we also have the handydrush_do_site_command($site_record, $command, $args = array(), $data = array())for calling a local or remote command given a site record.