Ejemplo n.º 1
0
 /**
  * the singleton pattern
  *
  * @return Tinebase_AsyncJob
  */
 public static function getInstance()
 {
     if (self::$instance === NULL) {
         self::$instance = new Tinebase_AsyncJob();
     }
     return self::$instance;
 }
Ejemplo n.º 2
0
 /**
  * testGetNextSequence
  */
 public function testGetNextSequence()
 {
     $async = Tinebase_AsyncJob::getInstance();
     $job = $async->startJob('Test_Job1', 5);
     sleep(3);
     $this->assertFalse($async->getNextSequence('Test_Job1'));
     $async->finishJob($job);
 }
 /**
  * nagios monitoring for tine 2.0 async cronjob run
  *
  * @return integer
  * 
  * @see http://nagiosplug.sourceforge.net/developer-guidelines.html#PLUGOUTPUT
  * @see 0008038: monitoringCheckCron -> check if cron did run in the last hour
  */
 public function monitoringCheckCron()
 {
     $message = 'CRON FAIL';
     try {
         $lastJob = Tinebase_AsyncJob::getInstance()->getLastJob('Tinebase_Event_Async_Minutely');
         if ($lastJob === NULL) {
             $message .= ': NO LAST JOB FOUND';
             $result = 1;
         } else {
             if ($lastJob->end_time instanceof Tinebase_DateTime) {
                 $duration = $lastJob->end_time->getTimestamp() - $lastJob->start_time->getTimestamp();
                 $valueString = ' | duration=' . $duration . 's;;;;';
                 $valueString .= ' end=' . $lastJob->end_time->getIso() . ';;;;';
             } else {
                 $valueString = '';
             }
             if ($lastJob->status === Tinebase_Model_AsyncJob::STATUS_RUNNING && Tinebase_DateTime::now()->isLater($lastJob->end_time)) {
                 $message .= ': LAST JOB TOOK TOO LONG';
                 $result = 1;
             } else {
                 if ($lastJob->status === Tinebase_Model_AsyncJob::STATUS_FAILURE) {
                     $message .= ': LAST JOB FAILED';
                     $result = 1;
                 } else {
                     if (Tinebase_DateTime::now()->isLater($lastJob->start_time->addHour(1))) {
                         $message .= ': NO JOB IN THE LAST HOUR';
                         $result = 1;
                     } else {
                         $message = 'CRON OK';
                         $result = 0;
                     }
                 }
             }
             $message .= $valueString;
         }
     } catch (Exception $e) {
         $message .= ': ' . $e->getMessage();
         $result = 2;
     }
     echo $message . "\n";
     return $result;
 }
 /**
  * send pending alarms
  *
  * @param mixed $_eventName
  * @return void
  * 
  * @todo sort alarms (by model/...)?
  * @todo what to do about Tinebase_Model_Alarm::STATUS_FAILURE alarms?
  */
 public function sendPendingAlarms($_eventName)
 {
     $eventName = is_array($_eventName) ? $_eventName['eventName'] : $_eventName;
     $job = Tinebase_AsyncJob::getInstance()->startJob($eventName);
     if (!$job) {
         return;
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' No ' . $eventName . ' is running. Starting new one.');
     }
     try {
         // get all pending alarms
         $filter = new Tinebase_Model_AlarmFilter(array(array('field' => 'alarm_time', 'operator' => 'before', 'value' => Tinebase_DateTime::now()->subMinute(1)->get(Tinebase_Record_Abstract::ISO8601LONG)), array('field' => 'sent_status', 'operator' => 'equals', 'value' => Tinebase_Model_Alarm::STATUS_PENDING)));
         $limit = Tinebase_Config::getInstance()->get(Tinebase_Config::ALARMS_EACH_JOB, 100);
         $pagination = $limit > 0 ? new Tinebase_Model_Pagination(array('limit' => $limit)) : null;
         $alarms = $this->_backend->search($filter, $pagination);
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Sending ' . count($alarms) . ' alarms (limit: ' . $limit . ').');
         }
         // loop alarms and call sendAlarm in controllers
         foreach ($alarms as $alarm) {
             list($appName, $i, $itemName) = explode('_', $alarm->model);
             $appController = Tinebase_Core::getApplicationInstance($appName, $itemName);
             if ($appController instanceof Tinebase_Controller_Alarm_Interface) {
                 $alarm->sent_time = Tinebase_DateTime::now();
                 try {
                     // NOTE: we set the status here, so controller can adopt the status itself
                     $alarm->sent_status = Tinebase_Model_Alarm::STATUS_SUCCESS;
                     $appController->sendAlarm($alarm);
                 } catch (Exception $e) {
                     Tinebase_Exception::log($e);
                     $alarm->sent_message = $e->getMessage();
                     $alarm->sent_status = Tinebase_Model_Alarm::STATUS_FAILURE;
                 }
                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                     Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Updating alarm status: ' . $alarm->sent_status);
                 }
                 $this->update($alarm);
             }
         }
         $job = Tinebase_AsyncJob::getInstance()->finishJob($job);
     } catch (Exception $e) {
         // save new status 'failure'
         $job = Tinebase_AsyncJob::getInstance()->finishJob($job, Tinebase_Model_AsyncJob::STATUS_FAILURE, $e->getMessage());
         if (Tinebase_Core::isLogLevel(Zend_Log::WARN)) {
             Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Job failed: ' . $e->getMessage());
         }
         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $e->getTraceAsString());
         }
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Job ' . $eventName . ' finished.');
     }
 }
 /**
  * testMonitoringCheckCron
  */
 public function testMonitoringCheckCron()
 {
     ob_start();
     $result = $this->_cli->monitoringCheckCron();
     $out = ob_get_clean();
     $lastJob = Tinebase_AsyncJob::getInstance()->getLastJob('Tinebase_Event_Async_Minutely');
     if ($lastJob) {
         $this->assertContains('CRON OK', $out);
         $this->assertEquals(0, $result);
     } else {
         $this->assertEquals("CRON FAIL: NO LAST JOB FOUND\n", $out);
         $this->assertEquals(1, $result);
     }
 }