php - Asynchronous events -


i'm trying setup background processing system events. i'm using tutorial : https://informulate.com/blog/symfony/high-performance-through-asynch-operations-using-symfony-background-processing/

i followed steps have 2 issues :

  • in command, don't know if event object given dispatcher event entity or event default class. think should default class if is, how can data event entity in subscriber ?
  • my subscriber doesn't seem event.

subscriber :

class qcmodifiedsubscriber implements eventsubscriberinterface { public function __construct(entitymanagerinterface $em, correctionreponseservice $correctionreponse) {     $this->em = $em;     $this->correctionreponse = $correctionreponse; }  public static function getsubscribedevents() {     // liste des évènements écoutés et méthodes à appeler     return array(         'qc.modified' => 'calculstats'     ); }  public function calculstats(event $event) {     $data = $event->getdata();     $qc = $this->em->getrepository(qc::class)->find($data['qcid']);     $this->correctionreponse->correctionreponsesofqc($qc); } 

}

services.yml :

services:     paces\collebundle\eventlistener\qcmodifiedsubscriber:         tags:             - { name: 'kernel.event_subscriber'} 

command :

protected function execute(inputinterface $input, outputinterface $output) {     $em = $this->getcontainer()->get('doctrine')->getmanager();     $dispatcher = new eventdispatcher();      while (true) {         $processid = rand(0, 999999999);         # attempts claim single event, returns if successful. may return multiple events.         // note: $processid unique id process, helps prevent race conditions (see below)         $events = $em->getrepository(event::class)->claimevent($processid);          # no events process, break out of loop.         if (count($events) === 0) {             break;         }          # iterate on each event processed, typically 1.         foreach ($events $evententity) {             $output->write("processing id: {$evententity->getid()}" . php_eol);              # create event...             $event = new \symfony\component\eventdispatcher\event($evententity);              try {                 # dispatch event!                 $dispatcher->dispatch($evententity->getname(), $event);                 # if made here successful, mark processed                 $evententity->setprocessed(1);              } catch (\exception $e) {                 $evententity->seterror((string)$e);             }              $em->persist($evententity);             $em->flush();         }     } } 

event entity same in tutorial.

since you're within symfony framework , looking kernel.event_subscriber, need dispatch event using symfony's eventdispatcher rather creating new instance yourself.

make sure command extends containerawarecommand:

use symfony\bundle\frameworkbundle\command\containerawarecommand;  class mycommand extends containerawarecommand 

and change line:

$dispatcher = new eventdispatcher(); 

to this:

$dispatcher = $this->getcontainer()->get('event_dispatcher'); 

to answer question had, event object given dispatcher event class, not event entity. have not going work there ; if look, event class not take in constructor arguments. symfony's documentation states:

if don't need pass additional data event listeners, can use default event class. in such case, can document event , name in generic storeevents class, similar kernelevents class.

what should create event specific event entity, so:

namespace appbundle\event;  use symfony\component\eventdispatcher\event; use appbundle\entity\event evententity;  class entityevent extends event {     const name = 'entity.event';      /**      * @var evententity      */     protected $evententity;      public function __construct(evententity $evententity)     {         $this->evententity = $evententity;     }      public function getevententity()     {         return $this->evententity;     } } 

now, create , dispatch event this:

use appbundle\event\entityevent;  // ...  $event = new entityevent($evententity);  // ...  $dispatcher->dispatch(entityevent::name, $event); 

then when subscribe event can event entity via $event->getevententity(); , operate on event would.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -