Skip to content

Add runcommand_defaults hook#5611

Closed
rebeccahum wants to merge 9849 commits intowp-cli:masterfrom
rebeccahum:add/run_command_hook_default
Closed

Add runcommand_defaults hook#5611
rebeccahum wants to merge 9849 commits intowp-cli:masterfrom
rebeccahum:add/run_command_hook_default

Conversation

@rebeccahum
Copy link
Copy Markdown

It would be nice to be able to change the $defaults set for $options in WP_CLI::runcommand():

wp-cli/php/class-wp-cli.php

Lines 1291 to 1296 in 0c427c5

$defaults = [
'launch' => true, // Launch a new process, or reuse the existing.
'exit_error' => true, // Exit on error by default.
'return' => false, // Capture and return output, or render in realtime.
'parse' => false, // Parse returned output as a particular format.
];

@rebeccahum rebeccahum requested a review from a team as a code owner January 28, 2022 20:49
@schlessera
Copy link
Copy Markdown
Member

@rebeccahum Thanks for introducing a PR!

I'm not yet sure what specific problem this is meant to solve. For individual call, you can already provide a custom array of values to adapt the behavior.

Adding a filter like this would only make sense if you want to modify the behavior of all calls, or calls outside of your direct control.

I think this would likely introduce many bugs, as some calls might rely on the fact that the defaults are not filterable yet and would only override the values that need changing. If other values changed as well in this unexpected way, it might cause these calls to break.

Can you tell me a bit more about the use case you had in mind?

@rebeccahum
Copy link
Copy Markdown
Author

rebeccahum commented Mar 17, 2022

@schlessera Thanks for taking a look. The specific use case for this would be to set the launch parameter to false by default, as we're seeing that WP_CLI::runcommand() does not work when using php-fpm for handling cron events, as it shells out to php for running a wp command (and now that we are in a chrooted fpm environment, there are no binaries for it to shell out to). Since the launch parameter is set to true by default and rather than having one pass it in manually each time, we want to default to false (especially for those unaware of the use case requiring that).

@schlessera
Copy link
Copy Markdown
Member

Which particular error condition is being triggered in that case?

I think it might be safer to add an opt-in mechanism instead to have a launch=true command that fails on preparing the environment to retry again as a launch=false command.

This would not change any existing behavior unless it was already broken in the first place.

I'm thinking about setting an environment variable that lets you opt in to this mechanism, like WP_CLI_LAUNCH_INLINE_FOR_FAILED_PROCESS_CREATION or something like that.

Do you think that would solve your use case?

@rebeccahum
Copy link
Copy Markdown
Author

rebeccahum commented Mar 29, 2022

@schlessera When WP_CLI::runcommand does proc_open(/usr/local/bin/php-fpm) that binary is not found (since there are really no binaries in the chroot environment), we get back a 127 error code. Based on that error, would that opt-in mechanism you've briefly described work for that use case?

@danielbachhuber
Copy link
Copy Markdown
Member

Proceeding with #5594 for this repository. I've captured this PR to https://gist.github.com/danielbachhuber/b45248df9e02eddb297935b0c6157eb1 in case this PR is auto-closed or broken in some way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.