Exemple #1
0
 /**
  * Executes this pulse. Checks for any pending reports and runs them.
  */
 public static function execute()
 {
     // Reschedule the next run of this process
     /*
      $sp = $this->makeClone();
      $sp->execute_at = strtotime("+1 minutes");
      $sp->save();
     * 
     */
     // Load necessary classes
     PHPWS_Core::initModClass('hms', 'UserStatus.php');
     PHPWS_Core::initModClass('hms', 'ReportFactory.php');
     PHPWS_Core::initModCLass('hms', 'HMS_Email.php');
     // Fake a user, in case we need that
     UserStatus::wearMask('HMS System');
     // Check for any pending reports (scheduled for any time up until now)
     $db = new PHPWS_DB('hms_report');
     $db->addWhere('completed_timestamp', null, 'IS');
     // not completed
     $db->addWhere('began_timestamp', null, 'IS');
     // not already running somewhere
     $db->addWhere('scheduled_exec_time', time(), '<=');
     // scheduled exec time is now or before
     $db->addOrder('scheduled_exec_time ASC');
     // Run in order scheduled
     $results = $db->select();
     // If there's nothing to do, quite nicely
     if (!isset($results) || is_null($results) || empty($results)) {
         UserStatus::removeMask();
         return 'No reports waiting.';
     }
     // Run each report
     foreach ($results as $row) {
         $report = null;
         try {
             // Load the proper controller for this report
             $reportCtrl = ReportFactory::getControllerById($row['id']);
             // Load this report's params
             $reportCtrl->loadParams();
             // Generate the report
             $reportCtrl->generateReport();
             $report = $reportCtrl->getReport();
         } catch (Exception $e) {
             // handle the exception nicely
             self::emailError(self::formatException($e));
             exit;
         }
         // Send success notification
         $username = $report->getCreatedBy();
         if ($username == 'jbooker') {
             $username = '******';
         }
         HMS_Email::sendReportCompleteNotification($username, $report->getFriendlyName());
     }
     // Remove the mask
     UserStatus::removeMask();
     // Exit cleanly
     return;
 }