Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Perform the job in the background.
  *
  * Jobs may need access to variables that are impossible to derive from
  * outside a web context. Here we pass the variables via shell arguments.
  * The perform-job script then sets them to the PHP-CLI context.
  *
  * @todo Pass the server URL, or compents required to set one
  * @see \Zend\View\Helper\BasePath
  * @see \Zend\View\Helper\ServerUrl
  *
  * {@inheritDoc}
  */
 public function send(Job $job)
 {
     $config = $this->getServiceLocator()->get('Config');
     $cli = $this->getServiceLocator()->get('Omeka\\Cli');
     if (isset($config['cli']['phpcli_path']) && $config['cli']['phpcli_path']) {
         $phpPath = $cli->validateCommand($config['cli']['phpcli_path']);
         if (false === $phpPath) {
             throw new Exception\RuntimeException('PHP-CLI error: invalid PHP path.');
         }
     } else {
         $phpPath = $cli->getCommandPath('php');
         if (false === $phpPath) {
             throw new Exception\RuntimeException('PHP-CLI error: cannot determine path to PHP.');
         }
     }
     $script = OMEKA_PATH . '/data/scripts/perform-job.php';
     $basePath = $this->getServiceLocator()->get('ViewHelperManager')->get('BasePath');
     $command = sprintf('%s %s --job-id %s --base-path %s', escapeshellcmd($phpPath), escapeshellarg($script), escapeshellarg($job->getId()), escapeshellarg($basePath()));
     $cli->execute(sprintf('%s > /dev/null 2>&1 &', $command));
 }
Example #3
0
 /**
  * {@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();
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 /**
  * {@inheritDoc}
  */
 public function getResourceId()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getResourceId', array());
     return parent::getResourceId();
 }
Example #6
0
 /**
  * Log to the Job entity.
  *
  * @param array $event
  */
 protected function doWrite(array $event)
 {
     $this->job->addLog($this->formatter->format($event));
 }