Example #1
0
 /**
  * Displays all available schedules according to the set filters.
  *
  * @param int $page Which page to display
  */
 public function index_action($page = 1)
 {
     $filter = $_SESSION['cronjob-filter'];
     $this->max_per_page = Config::get()->ENTRIES_PER_PAGE;
     $this->total = CronjobSchedule::countBySql('1');
     $this->total_filtered = CronjobSchedule::countBySql($filter['where']);
     $this->page = max(1, min($page, ceil($this->total_filtered / $this->max_per_page)));
     $limit = sprintf(" LIMIT %u, %u", ($this->page - 1) * $this->max_per_page, $this->max_per_page);
     $this->schedules = CronjobSchedule::findBySQL($filter['where'] . $limit);
     // Filters
     $this->tasks = CronjobTask::findBySql('1');
     $this->filter = $filter['values'];
     // Infobox image was produced from an image by Robbert van der Steeg
     // http://www.flickr.com/photos/robbie73/5924985913/
     $sidebar = Sidebar::Get();
     $sidebar->setTitle(_('Cronjobs'));
     $sidebar->setImage('sidebar/date-sidebar.png');
     // Aktionen
     $views = new ViewsWidget();
     $views->addLink(_('Cronjobs verwalten'), $this->url_for('admin/cronjobs/schedules'))->setActive(true);
     $views->addLink(_('Aufgaben verwalten'), $this->url_for('admin/cronjobs/tasks'));
     $views->addLink(_('Logs anzeigen'), $this->url_for('admin/cronjobs/logs'));
     $sidebar->addWidget($views);
     $actions = new ActionsWidget();
     $actions->addLink(_('Neuen Cronjob registrieren'), $this->url_for('admin/cronjobs/schedules/edit'), Icon::create('add', 'clickable'));
     $sidebar->addWidget($actions);
 }
Example #2
0
 /**
  * Executes the available schedules if they are to be executed.
  * This method can only be run once - even if one execution takes more
  * than planned. This is ensured by a locking mechanism.
  */
 public function run()
 {
     if (!Config::get()->CRONJOBS_ENABLE) {
         return;
     }
     $escalation_time = Config::get()->CRONJOBS_ESCALATION;
     // Check whether a previous cronjob worker is still running.
     if ($this->lock->isLocked($data)) {
         // Running but not yet escalated -> let it run
         if ($data['timestamp'] + $escalation_time > time()) {
             return;
         }
         // Load locked schedule
         $schedule = CronjobSchedule::find($data['schedule_id']);
         // If we discovered a deadlock release it
         if ($schedule) {
             // Deactivate schedule
             $schedule->deactivate();
             // Adjust log
             $log = CronjobLog::find($data['log_id']);
             $log->duration = time() - $data['timestamp'];
             $log->exception = new Exception('Cronjob has escalated');
             $log->store();
             // Inform roots about the escalated cronjob
             $subject = sprintf('[Cronjobs] %s: %s', _('Eskalierte Ausführung'), $schedule->title);
             $message = sprintf(_('Der Cronjob "%s" wurde deaktiviert, da ' . 'seine Ausführungsdauer die maximale ' . 'Ausführungszeit von %u Sekunden ' . 'überschritten hat.') . "\n", $schedule->title, $escalation_time);
             $this->sendMailToRoots($subject, $message);
         }
         // Release lock
         $this->lock->release();
     }
     // Find all schedules that are due to execute and which task is active
     $temp = CronjobSchedule::findBySQL('active = 1 AND next_execution <= UNIX_TIMESTAMP() ' . 'ORDER BY priority DESC, next_execution ASC');
     #        $temp = SimpleORMapCollection::createFromArray($temp);
     $schedules = array_filter($temp, function ($schedule) {
         return $schedule->task->active;
     });
     if (count($schedules) === 0) {
         return;
     }
     foreach ($schedules as $schedule) {
         $log = new CronjobLog();
         $log->schedule_id = $schedule->schedule_id;
         $log->scheduled = $schedule->next_execution;
         $log->executed = time();
         $log->exception = null;
         $log->duration = -1;
         $log->store();
         set_time_limit($escalation_time);
         // Activate the file lock and store the current timestamp,
         // schedule id and according log id in it
         $this->lock->lock(array('schedule_id' => $schedule->schedule_id, 'log_id' => $log->log_id));
         // Start capturing output and measuring duration
         ob_start();
         $start_time = microtime(true);
         try {
             $schedule->execute();
         } catch (Exception $e) {
             $log->exception = $e;
             // Deactivate schedule
             $schedule->deactivate();
             // Send mail to root accounts
             $subject = sprintf('[Cronjobs] %s: %s', _('Fehlerhafte Ausführung'), $schedule->title);
             $message = sprintf(_('Der Cronjob "%s" wurde deaktiviert, da bei der Ausführung ein Fehler aufgetreten ist.'), $schedule->title) . "\n";
             $message .= "\n";
             $message .= display_exception($e) . "\n";
             $message .= _('Für weiterführende Informationen klicken Sie bitten den folgenden Link:') . "\n";
             $message .= $GLOBALS['ABSOLUTE_URI_STUDIP'] . URLHelper::getURL('dispatch.php/admin/cronjobs/logs/schedule/' . $schedule->schedule_id);
             $this->sendMailToRoots($subject, $message);
         }
         // Actually capture output and duration
         $end_time = microtime(true);
         $output = ob_get_clean();
         // Complete log
         $log->output = $output;
         $log->duration = $end_time - $start_time;
         $log->store();
     }
     // Release lock
     $this->lock->release();
 }