protected function createJob($label, $data = array())
 {
     $job = new Opus_Job();
     $job->setLabel($label);
     $job->setData($data);
     $this->jobIds[] = $job->store();
 }
 public function setUp()
 {
     parent::setUp();
     $config = Zend_Registry::get('Zend_Config');
     $this->__configBackup = $config;
     $config->merge(new Zend_Config(array('runjobs' => array('asynchronous' => true))));
     $this->assertEquals(0, Opus_Job::getCount(Opus_Job::STATE_FAILED), 'test data changed.');
     for ($i = 0; $i < 10; $i++) {
         $job = new Opus_Job();
         $job->setLabel('testjob' . ($i < 5 ? 1 : 2));
         $job->setData(array('documentId' => $i, 'task' => 'get-me-a-coffee'));
         $job->setState(Opus_Job::STATE_FAILED);
         $this->jobIds[] = $job->store();
     }
 }
Example #3
0
 public function createJob()
 {
     $job = new Opus_Job();
     $job->setLabel(Opus_Job_Worker_ConsistencyCheck::LABEL);
     if (!$this->featureDisabled) {
         // Queue job (execute asynchronously)
         // skip creating job if equal job already exists
         if (true === $job->isUniqueInQueue()) {
             $job->store();
             return $job->getId();
         }
         return true;
     }
     // Execute job immediately (synchronously): currently NOT supported
     try {
         $worker = new Opus_Job_Worker_ConsistencyCheck();
         $worker->setLogger($this->logger);
         $worker->work($job);
     } catch (Exception $exc) {
         $this->logger->err($exc);
     }
     return false;
 }
Example #4
0
 /**
  * Helper method to add document to index.
  *
  * @param Opus_Document $document
  * @return void
  */
 private function addDocumentToIndex(Opus_Document $document)
 {
     $log = Zend_Registry::get('Zend_Log');
     // create job if asynchronous is set
     if (isset($this->config->runjobs->asynchronous) && $this->config->runjobs->asynchronous) {
         $log->debug(__METHOD__ . ': ' . 'Adding index job for document ' . $document->getId() . '.');
         $job = new Opus_Job();
         $job->setLabel(Opus_Job_Worker_IndexOpusDocument::LABEL);
         $job->setData(array('documentId' => $document->getId(), 'task' => 'index'));
         // skip creating job if equal job already exists
         if (true === $job->isUniqueInQueue()) {
             $job->store();
         } else {
             $log->debug(__METHOD__ . ': ' . 'Indexing job for document ' . $document->getId() . ' already exists!');
         }
     } else {
         $log->debug(__METHOD__ . ': ' . 'Index document ' . $document->getId() . '.');
         try {
             $indexer = new Opus_SolrSearch_Index_Indexer();
             $indexer->addDocumentToEntryIndex($document);
             $indexer->commit();
         } catch (Opus_SolrSearch_Index_Exception $e) {
             $log->debug(__METHOD__ . ': ' . 'Indexing document ' . $document->getId() . ' failed: ' . $e->getMessage());
         } catch (InvalidArgumentException $e) {
             $log->warn(__METHOD__ . ': ' . $e->getMessage());
         }
     }
 }
Example #5
0
 private function scheduleNotification($subject, $message, $recipients)
 {
     if (empty($recipients)) {
         $this->logger->warn("No recipients could be determined for email notification: skip operation");
         return;
     }
     $addressesUsed = array();
     foreach ($recipients as $recipient) {
         if (!in_array($recipient['address'], $addressesUsed)) {
             $job = new Opus_Job();
             $job->setLabel(Opus_Job_Worker_MailNotification::LABEL);
             $job->setData(array('subject' => $subject, 'message' => $message, 'users' => array($recipient)));
             if (isset($this->config->runjobs->asynchronous) && $this->config->runjobs->asynchronous) {
                 // Queue job (execute asynchronously)
                 // skip creating job if equal job already exists
                 if (true === $job->isUniqueInQueue()) {
                     $job->store();
                 }
             } else {
                 // Execute job immediately (synchronously)
                 try {
                     $mail = new Opus_Job_Worker_MailNotification($this->logger, false);
                     $mail->work($job);
                 } catch (Exception $exc) {
                     $this->logger->err("Email notification failed: " . $exc);
                 }
             }
             array_push($addressesUsed, $recipient['address']);
         }
     }
 }
Example #6
0
 /**
  * Execute a job and remove it from the jobs table on success.
  *
  * @param Opus_Job $job Job description model.
  * @return boolean Returns true if a job is consumend false if not
  */
 protected function consume(Opus_Job $job)
 {
     $label = $job->getLabel();
     if ($job->getState() !== null) {
         return false;
     }
     if (array_key_exists($label, $this->_workers)) {
         $worker = $this->_workers[$label];
         if (null !== $this->_logger) {
             $this->_logger->info('Processing ' . $label);
         }
         $job->setState(Opus_Job::STATE_PROCESSING);
         $job->store();
         try {
             $worker->setLogger($this->_logger);
             $worker->work($job);
             $job->delete();
             sleep($this->_delay);
         } catch (Exception $ex) {
             if (null !== $this->_logger) {
                 $msg = get_class($worker) . ': ' . $ex->getMessage();
                 $this->_logger->err($msg);
             }
             $job->setErrors(json_encode(array('exception' => get_class($ex), 'message' => $ex->getMessage(), 'trace' => $ex->getTraceAsString())));
             $job->setState(Opus_Job::STATE_FAILED);
             $job->store();
             return false;
         }
         return true;
     }
     return false;
 }
 /**
  *
  * @param Matheon_Model_Document $document
  * @param array $recipient
  * @return void
  */
 private function __sendPublishNotification($document, $recipient)
 {
     $config = $this->getConfig();
     $baseUrlFiles = $this->view->serverUrl() . '/opus4-matheon/files';
     $job = new Opus_Job();
     $job->setLabel(Opus_Job_Worker_MailNotification::LABEL);
     $job->setData(array('subject' => $document->renderPublishMailSubject(), 'message' => $document->renderPublishMailBody($this->view->fullUrl(), $baseUrlFiles), 'users' => $recipient));
     //throw new Exception(var_export($job, true));
     if (isset($config->runjobs->asynchronous) && $config->runjobs->asynchronous) {
         // Queue job (execute asynchronously)
         // skip creating job if equal job already exists
         if (true === $job->isUniqueInQueue()) {
             $job->store();
         }
         return true;
     }
     // Execute job immediately (synchronously)
     try {
         $mail = new Opus_Job_Worker_MailNotification($this->getLogger());
         $mail->work($job);
     } catch (Exception $exc) {
         $this->getLogger()->err($exc);
     }
     return true;
 }