-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.3] Fire events when command starts and terminates #16355
Conversation
Related to #15613 Thought I'd piggy back on the contributions by @themsaid and @lucasmichot to have events fired with a console command is starting and terminating. One use case for this would be logging a message when a command starts and ends. I have scenarios where multiple commands are run throughout the night and this would be an easy way to have these messages logged. |
@@ -102,6 +112,28 @@ public function call($command, array $parameters = []) | |||
} | |||
|
|||
/** | |||
* Runs the command container. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be just fine:
/**
* {@inheritdoc}
*/
public function run(InputInterface $input = null, OutputInterface $output = null) | ||
{ | ||
$this->events->fire( | ||
new Events\CommandStarting($this->getCommandName($input), $input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->getCommandName($input)
is repeated twice and could be extracted to $commandName
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* @return void | ||
*/ | ||
public function __construct($command, InputInterface $input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing $exitCode
argument
Thanks for the feedback @lucasmichot I have updated the pull request. |
{ | ||
$this->command = $command; | ||
$this->input = $input; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$exitCode
should also be declared as a public property and set in the constructor.
Thanks again @lucasmichot |
I don't know. I'm not very sold on this PR yet. If you have things you want to log from a command just put it in the command itself. |
I see a very good use case here to avoid code duplication on very common task we use across hundreds of commands for mission critical batch processes that run all the day, some of them every 5 minutes. On Command Start:
On Command End:
Advantages:
Please consider to merge it again. Why Log and Monitor:
|
Could this not be done in a base class that all your commands extend?
|
We use micro services architecture and Inheritance is not used in our code base... And yes it can be achieved in several different ways, but if the Framework can easily handle that with events, like in the Models, than no reason to duplicate efforts across several packages or create dependencies to handle this bit of logic. |
Can this not be reconsidered @taylorotwell ? |
For those interested I ended up creating a package that should fill the need: https://github.com/bmitch/consoleEvents Any feedback is greatly appreciated. Thanks. |
No description provided.