title: Symfony run code after response was sent
body: <p>I took a look at <a href="https://stackoverflow.com/questions/18297161/symfony-code-after-returning-response">this other question</a>. I am looking for a way to do what the OP of that question wants as well, and that is to <a href="https://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response">continue processing php after sending http response</a>, but in Symfony2. </p>
  
  <p>I implemented an event that fires after every kernel termination. So far so good, but what I want is for it to fire after CERTAIN terminations, in specific controller actions, for instance after a form was sent, not every single time at every request. That is because I want to do some heavy tasks at certain times and don't want the end user to wait for the page to load. </p>
  
  <p>Any idea how can I do that?</p>
  
  <pre><code>&lt;?php
  
  
  namespace MedAppBundle\Event;
  
  use JMS\DiExtraBundle\Annotation\InjectParams;
  use JMS\DiExtraBundle\Annotation\Service;
  use JMS\DiExtraBundle\Annotation\Tag;
  use Psr\Log\LoggerInterface;
  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Symfony\Component\HttpKernel\KernelEvents;
  use Symfony\Component\Console\ConsoleEvents;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  use JMS\DiExtraBundle\Annotation\Inject;
  /**
   * Class MedicListener
   * @package MedAppBundle\EventListener
   * @Service("medapp_test.listener")
   * @Tag(name="kernel.event_subscriber")
   */
  class TestListener implements EventSubscriberInterface
  {
      private $container;
  
      private $logger;
  
      /**
       * Constructor.
       *
       * @param ContainerInterface $container A ContainerInterface instance
       * @param LoggerInterface $logger A LoggerInterface instance
       * @InjectParams({
       *     "container" = @Inject("service_container"),
       *     "logger" = @Inject("logger")
       * })
       */
      public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
      {
          $this-&gt;container = $container;
          $this-&gt;logger = $logger;
      }
  
      public function onTerminate()
      {
        $this-&gt;logger-&gt;notice('fired');
      }
  
      public static function getSubscribedEvents()
      {
          $listeners = array(KernelEvents::TERMINATE =&gt; 'onTerminate');
  
          if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
              $listeners[ConsoleEvents::TERMINATE] = 'onTerminate';
          }
  
          return $listeners;
      }
  }
  </code></pre>
  
  <p>So far I've subscribed the event to the kernel.terminate one, but obviously this fires it at each request. I made it similar to Swiftmailer's <a href="https://github.com/symfony/swiftmailer-bundle/blob/master/EventListener/EmailSenderListener.php" rel="nofollow noreferrer">EmailSenderListener</a></p>
  
  <p>It feels kind of strange that the kernel must listen each time for this event even when it's not triggered. I'd rather have it fired only when needed, but not sure how to do that.</p>