Skip to content
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 an event while running a console command #15613

Closed
wants to merge 2 commits into from
Closed

[5.3] Fire an event while running a console command #15613

wants to merge 2 commits into from

Conversation

themsaid
Copy link
Member

This PR fires a CommandRunning event before executing a console command.

As reported in #15610, laravel doesn't set Symfony's dispatcher while running console commands which causes Symfony's own console events not to fire.

Laravel already fires a ArtisanStarting command on Artisan start, but it doesn't contain information about the command running or the inputs given, this new event will get fired before running every console command allowing tasks like logging, sending notifications, etc...

@taylorotwell
Copy link
Member

Hmm, I'm curious of the use case for this.

@themsaid
Copy link
Member Author

I can only think of logging, if you need to keep track of when a command was run.

@mcklayin
Copy link

Some guy from stackoverflow need it. He want to fire some function, when every console command was executed, but now its impossible, php artisan --help fires event too, and he need only comands like php artisan some_command_name

@lucasmichot
Copy link
Contributor

After getting the parent $exitCode, a \Illuminate\Console\Events\CommandHasRun event would be useful too.

@taylorotwell
Copy link
Member

taylorotwell commented Sep 26, 2016

Useful for what though? We haven't even clearly identified a use case for the first event? Other than some person on StackOverflow wanted it for apparently something :)

@lucasmichot
Copy link
Contributor

lucasmichot commented Sep 26, 2016

After getting the parent $exitCode, a \Illuminate\Console\Events\CommandHasRun event would be useful too.

This would allow to run "chained" commands very easily, upon success or another command in case of a failure (based on the command name, input and/or $exitCode).

// ...
$exitCode = parent::run($input, $output);

$this->events->fire(
    new Events\CommandHasRun($this->getCommandName($input), $input, $exitCode)
);

return $exitCode;

@themsaid
Copy link
Member Author

@taylorotwell Well in my last project we had some reset console commands we used to reset client data after their demo and training before they start using the software. These commands are dangerous so we logged every attempt of using it in case we found any missing data in the DB.

The developer can always do the logging inside the command itself, I just checked the issue on this repo and saw that Symfony fires a similar event, and also laravel fires an event on Artisan start so thought it might maybe useful to have such event.

Anyway the use case is very very limited, so I guess we can close this PR :)

@taylorotwell
Copy link
Member

Going to hold off on this for now since I don't feel like we have a real solid use case at the moment.

@themsaid themsaid deleted the consoleRunningEvent branch September 27, 2016 14:51
@bmitch
Copy link
Contributor

bmitch commented Nov 10, 2016

Any chance this could be looked at again? We have a use case where we schedule multiple commands to run each night and would like to log messages when the command has started and finished.

Would be great if there were events for when a command has started and finished. Then could latch onto those events and Log a message that the command has started and finished.

Then easy enough to see logs if each command completed.

We could write these logs in each command independently but thought by using events like this, could write them in just one place.

@ajthinking
Copy link

ajthinking commented Jan 1, 2017

I have a use case for this feature: After any artisan migrate command, I would like to run a custom script to manage triggers and functions in my database extension. I really dont want to modify or duplicate the existing migrate command. This would also break all my third party stuff. I just want to do some stuff right after artisan migrate.

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.

6 participants