JMSJobQueueBundle icon indicating copy to clipboard operation
JMSJobQueueBundle copied to clipboard

Event Listener not working on ending states

Open clofab opened this issue 10 years ago • 5 comments

Hello,

I use this bundle to mass process files into database and I would like to use Event Listener to mark a file processing as failed as soon as the associated job state is set to failed as well, but it never happen. There is the code :

services : 
loading.job_status_change:
        class: MyBundle\EventListener\JobStatusChangeListener
        tags:
            - { name: kernel.event_listener, event: jms_job_queue.job_state_change, method: onStateChange }

And the event Listener (which for the moment write in a log file for debug):

class JobStatusChangeListener {

    public function onStateChange(StateChangeEvent $event)
    {
        $adress = "%kernel.root_dir%/../web/files/log.txt";
        $file = fopen($adress, "a+");
        $job = $event->getJob();
        $fileId = implode("|",$job->getArgs());
        fwrite($file, PHP_EOL."Job : ".$job->getId()." - File : ".$fileId." - Status : New : ".$event->getNewState()."/ Old :".$job->getState());
        fclose($file);

    }
}

log.txt

Job : 149 - File : 10 - Status : New : running/ Old :pending
Job : 150 - File : 11 - Status : New : running/ Old :pending
Job : 151 - File : 12 - Status : New : running/ Old :pending
Job : 152 - File : 13 - Status : New : running/ Old :pending
Job : 153 - File : 14 - Status : New : running/ Old :pending
Job : 154 - File : 15 - Status : New : running/ Old :pending
Job : 155 - File : 16 - Status : New : running/ Old :pending
Job : 156 - File : 17 - Status : New : running/ Old :pending
Job : 157 - File : 18 - Status : New : running/ Old :pending

So its seems that onStateChange is never triggered when the job will be closed. Any idea why?

clofab avatar Dec 08 '15 09:12 clofab

I've managed to sort of fix it by listening a postUpdate Event. But the onStateChange event still seems to not be triggered when the job state change from running state to any ending state (failed, succes or incomplete).

clofab avatar Dec 09 '15 13:12 clofab

I could make it work:

app.listener.job:
    class: App\CoreBundle\Listener\JobListener
    arguments:
        - @doctrine.orm.default_entity_manager
    tags:
        - { name: kernel.event_listener, event: jms_job_queue.job_state_change, method: onStateChange }
<?php
namespace App\CoreBundle\Listener;

use App\CoreBundle\Entity\Queue;
use Doctrine\Common\Persistence\ObjectManager;
use JMS\JobQueueBundle\Event\JobEvent;

class JobListener {

    protected $em;

    /**
     * @param ObjectManager $em
     */
    public function __construct(ObjectManager $em)
    {
        $this->em = $em;
    }

    public function onStateChange(JobEvent $event)
    {
        $job = $event->getJob();

        $queue = $job->findRelatedEntity('App\CoreBundle\Entity\Queue');
        if ($queue instanceof Queue) {
            $queue->setJobState($event->getNewState());

            $this->em->persist($queue);
            $this->em->flush();
        }
    }
}

eveyrat avatar Dec 10 '15 10:12 eveyrat

Hi, I have the same problem.

The final event is never triggered.

Here, the first condition return false, $this->dispatcher is null, unless when finish Job, thus the event is not dispatched.

Why is null?

anacona16 avatar Apr 19 '16 21:04 anacona16

Any ideas to fix this ?

anthony-launay avatar May 25 '16 14:05 anthony-launay

I have solved as follows:

In app/AppKernel.php

new JMS\DiExtraBundle\JMSDiExtraBundle($this),
new JMS\AopBundle\JMSAopBundle(),

anacona16 avatar Jul 13 '16 01:07 anacona16