sfErrorNotifierPlugin on symfony task

你。 提交于 2020-01-14 04:03:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!