schmittjoh / JMSJobQueueBundle

Run and Schedule Symfony Console Commands as Background Jobs
http://jmsyst.com/bundles/JMSJobQueueBundle
334 stars 254 forks source link

Job will not get state 'Finished' if ->setOriginalJob is used. #201

Open SymenTimmermans opened 6 years ago

SymenTimmermans commented 6 years ago

I'm using version 1.4.2, on Symfony 2.6.

I tried implementing a way to link 'retry' jobs if previous jobs fail.

When a user requests an entity to be processed, my script checks if related jobs to the entity can be found. In this case it will find a failed job. I would like to link the failed job to a new job, but I can't seem to use 'addRetryJob', since this needs a 'running' job. So I use 'setOriginalJob'. My code looks somewhat like this:

            $job = $em->getRepository('JMSJobQueueBundle:Job')
                      ->findJobForRelatedEntity('command', $entity);

            if ($job) {
                if ($job->isRunning() ||
                    $job->isPending() ||
                    $job->isFinished()) {
                           // do nothing
                } else if ($job->isCanceled() ||
                           $job->isFailed() ||
                           $job->isIncomplete()) {
                    $retryJob = true;
                }
            } else {
                $startNewJob = true;
            }

            if ($startNewJob || $retryJob) {
                $njob = new Job('command', array($id));
                $njob->addRelatedEntity($entity);
                if ($retryJob) {
                    $njob->setOriginalJob($job);
                }
                $em->persist($njob);
                $em->flush();
            }

This results in two issues:

Maybe there's a flaw in my implementation, would love to hear about it.