问题
My application runs huge batch processing inside a Symfony task, and I want to be notified about all PHP errors and uncaught exceptions.
So I tried sfErrorNotifierPlugin, and it works great in a web context (accessing the application from the browser); the problem is that I can't make it work on my symfony tasks.
Is there any way to make it work in tasks?
回答1:
sfTask
has no exception handling like the web interface does, but you can work around it: ultimately exceptions thrown are passed to sfErrorNotifier::notifyException.
Wrap your task's execute
method in a big try-catch block:
public function execute($arguments = array(), $options = array())
{
try {
//your code here
}
catch(Exception $e) {
sfErrorNotifier::notifyException($e); //call the notifier
throw $e; //rethrow to stop execution and to avoid problems in some special cases
}
}
Keep in mind that it needs an application parameter to run correctly (uses settings from app.yml).
回答2:
in ProjectConfiguration.class.php
public function setup()
{
if ('cli' == php_sapi_name()) $this->disablePlugins('sfErrorNotifierPlugin');
}
回答3:
Thank for your help @Maerlyn, my solution is not much different from yours.
I solved the problem overriding the doRun method on my tasks this way:
protected function doRun(sfCommandManager $commandManager, $options)
{
try
{
return parent::doRun($commandManager, $options);
}
catch (Exception $e)
{
$this->dispatcher->notifyUntil(new sfEvent($e, 'application.throw_exception'));
throw $e;
}
}
This solves the problem.
来源:https://stackoverflow.com/questions/5962230/sferrornotifierplugin-on-symfony-task