/** * Check if this job should stop. * * Typically called from within an iteration and followed by whatever logic * is needed to gracefully clean up the job, in turn followed by a break out * of the iteration and no further work. * * Queries the database for the Job object since the process that sets * STATUS_STOPPING is not necessarily the same process that this job is * running on. We're not using the entity manager's refresh method because * we can't assume a static Job state during the course of the job. * * @return bool */ public function shouldStop() { $entityManager = $this->getServiceLocator()->get('Omeka\\EntityManager'); $dql = 'SELECT j.status FROM Omeka\\Entity\\Job j WHERE j.id = :id'; $status = $entityManager->createQuery($dql)->setParameter('id', $this->job->getId())->getSingleScalarResult(); $this->job->setStatus($status); return Job::STATUS_STOPPING === $status; }
/** * {@inheritDoc} */ public function send(Job $job) { $entityManager = $this->getServiceLocator()->get('Omeka\\EntityManager'); $job->setStatus(Job::STATUS_IN_PROGRESS); $entityManager->flush(); $class = $job->getClass(); $jobClass = new $class($job, $this->getServiceLocator()); $jobClass->perform(); if (Job::STATUS_STOPPING == $job->getStatus()) { $job->setStatus(Job::STATUS_STOPPED); } else { $job->setStatus(Job::STATUS_COMPLETED); } $job->setEnded(new DateTime('now')); $entityManager->flush(); }
/** * Dispatch a job. * * Composes a Job entity and uses the configured strategy if no strategy is * passed. * * @param string $class * @param mixed $args * @param StrategyInterface $strategy * @return null|Job $job */ public function dispatch($class, $args = null, StrategyInterface $strategy = null) { if (!is_subclass_of($class, 'Omeka\\Job\\JobInterface')) { throw new Exception\InvalidArgumentException(sprintf('The job class "%s" does not implement Omeka\\Job\\JobInterface.', $class)); } $entityManager = $this->getServiceLocator()->get('Omeka\\EntityManager'); $auth = $this->getServiceLocator()->get('Omeka\\AuthenticationService'); $job = new Job(); $job->setStatus(Job::STATUS_STARTING); $job->setClass($class); $job->setArgs($args); $job->setOwner($auth->getIdentity()); $entityManager->persist($job); $entityManager->flush(); if (!$strategy) { $strategy = $this->getDispatchStrategy(); } $this->send($job, $strategy); return $job; }
/** * {@inheritDoc} */ public function setStatus($status) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setStatus', array($status)); return parent::setStatus($status); }