/** * 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; }
/** * 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)); }
/** * {@inheritDoc} */ public function getId() { if ($this->__isInitialized__ === false) { return (int) parent::getId(); } $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', array()); return parent::getId(); }