title: Asynchronous events
body: <p>I'm trying to setup a background processing system for events. I'm using this tutorial : <a href="https://informulate.com/blog/symfony/high-performance-through-asynch-operations-using-symfony-background-processing/" rel="nofollow noreferrer">https://informulate.com/blog/symfony/high-performance-through-asynch-operations-using-symfony-background-processing/</a></p>
  
  <p>I followed all the steps but I have 2 issues :</p>
  
  <ul>
  <li>In the command, I don't know if the Event object given to the dispatcher is the Event entity or the Event default class. I think it should be the default class but if it is, how can I get the data from the Event entity in my Subscriber ?</li>
  <li>My subscriber doesn't seem to get the event.</li>
  </ul>
  
  <p>Subscriber : </p>
  
  <pre><code>class QCModifiedSubscriber implements EventSubscriberInterface
  {
  public function __construct(EntityManagerInterface $em, CorrectionReponseService $correctionReponse)
  {
      $this-&gt;em = $em;
      $this-&gt;correctionReponse = $correctionReponse;
  }
  
  public static function getSubscribedEvents()
  {
      // Liste des évènements écoutés et méthodes à appeler
      return array(
          'qc.modified' =&gt; 'calculStats'
      );
  }
  
  public function calculStats(Event $event)
  {
      $data = $event-&gt;getData();
      $QC = $this-&gt;em-&gt;getRepository(QC::class)-&gt;find($data['qcId']);
      $this-&gt;correctionReponse-&gt;correctionReponsesOfQC($QC);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>services.yml :</p>
  
  <pre><code>services:
      PACES\ColleBundle\EventListener\QCModifiedSubscriber:
          tags:
              - { name: 'kernel.event_subscriber'}
  </code></pre>
  
  <p>Command :</p>
  
  <pre><code>protected function execute(InputInterface $input, OutputInterface $output)
  {
      $em = $this-&gt;getContainer()-&gt;get('doctrine')-&gt;getManager();
      $dispatcher = new EventDispatcher();
  
      while (true) {
          $processId = rand(0, 999999999);
          # attempts to claim a single event, returns it if successful. may return multiple events.
          // note: $processId is some unique id to this process, helps prevent race conditions (see below)
          $events = $em-&gt;getRepository(Event::class)-&gt;claimEvent($processId);
  
          # no events to process, so break out of loop.
          if (count($events) === 0) {
              break;
          }
  
          # iterate over each event to be processed, typically just 1.
          foreach ($events as $eventEntity) {
              $output-&gt;write("Processing id: {$eventEntity-&gt;getId()}" . PHP_EOL);
  
              # create the event...
              $event = new \Symfony\Component\EventDispatcher\Event($eventEntity);
  
              try {
                  # dispatch the event!
                  $dispatcher-&gt;dispatch($eventEntity-&gt;getName(), $event);
                  # if we made it here we were successful, mark as processed
                  $eventEntity-&gt;setProcessed(1);
  
              } catch (\Exception $e) {
                  $eventEntity-&gt;setError((string)$e);
              }
  
              $em-&gt;persist($eventEntity);
              $em-&gt;flush();
          }
      }
  }
  </code></pre>
  
  <p>Event entity is exactly the same as in the tutorial.</p>