Example #1
0
    /**
     * Displays all available log entries according to the set filters.
     *
     * @param int $page Which page to display
     */
    public function index_action($page = 1)
    {
        $filter = $_SESSION['cronlog-filter'];
        
        $this->max_per_page   = Config::get()->ENTRIES_PER_PAGE;
        $this->total          = CronjobLog::countBySql();
        $this->total_filtered = CronjobLog::countBySql($filter['where']);
        $this->page           = max(1, min($page, ceil($this->total_filtered / $this->max_per_page)));

        $order = " ORDER BY executed DESC";
        $limit = sprintf(" LIMIT %u, %u", ($this->page - 1) * $this->max_per_page, $this->max_per_page);
        $this->logs = CronjobLog::findBySQL($filter['where'] . $order . $limit);

        // Filters
        $this->schedules  = CronjobSchedule::findBySql('1');
        $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/
        // Aktionen
        $sidebar = Sidebar::Get();
        $sidebar->setTitle(_('Cronjobs'));
        $sidebar->setImage('sidebar/admin-sidebar.png');

        $actions = new ViewsWidget();
        $actions->addLink(_('Cronjobs verwalten'),$this->url_for('admin/cronjobs/schedules'));
        $actions->addLink(_('Aufgaben verwalten'),$this->url_for('admin/cronjobs/tasks'));
        $actions->addLink(_('Logs anzeigen'),$this->url_for('admin/cronjobs/logs'))->setActive(true);
        $sidebar->addWidget($actions);
    }
Example #2
0
$argv = $_SERVER['argv'];
$opts = getopt('hl', array('help', 'list'));
if (isset($opts['l']) || isset($opts['list'])) {
    $tasks = CronjobTask::findBySql('1');
    foreach ($tasks as $task) {
        $description = call_user_func(array($task->class, 'getDescription'));
        fwrite(STDOUT, sprintf('%s %s' . PHP_EOL, $task->id, studip_utf8encode($description)));
    }
    exit(0);
}
if ($argc < 2 || isset($opts['h']) || isset($opts['help'])) {
    fwrite(STDOUT, 'Usage: ' . basename(__FILE__) . ' [--help] [--list] <task_id> [last_result]' . PHP_EOL);
    exit(0);
}
$id = $_SERVER['argv'][1];
$last_result = $argc > 2 ? $_SERVER['argv'][2] : null;
$task = CronjobTask::find($id);
if (!$task) {
    fwrite(STDOUT, 'Unknown task id' . PHP_EOL);
    exit(0);
}
if (!file_exists($GLOBALS['STUDIP_BASE_PATH'] . '/' . $task->filename)) {
    fwrite(STDOUT, 'Invalid task, unknown filename "' . $task->filename . '"' . PHP_EOL);
    exit(0);
}
require_once $task->filename;
if (!class_exists($task->class)) {
    fwrite(STDOUT, 'Invalid task, unknown class "' . $task->class . '"' . PHP_EOL);
    exit(0);
}
$task->engage($last_result);
Example #3
0
 /**
  * Unregisters a previously registered task.
  *
  * @param String $task_id Id of the task to be unregistered
  * @return CronjobScheduler to allow chaining
  * @throws InvalidArgumentException when no task with the given id exists
  */
 public function unregisterTask($task_id)
 {
     $task = CronjobTask::find($task_id);
     if ($task === null) {
         $message = sprintf('A task with the id "%s" does not exist.', $task_id);
         throw new InvalidArgumentException($message);
     }
     $task->delete();
     return $this;
 }
Example #4
0
    /**
     * Performs a bulk operation on a set of tasks. Operation can be either
     * activating, deactivating or deleting.
     *
     * @param int    $page Return to this page afterwarsd (optional)
     */
    public function bulk_action($page = 1)
    {
        $action = Request::option('action');
        $ids    = Request::optionArray('ids');
        $tasks  = CronjobTask::findMany($ids);

        if ($action === 'activate') {
            $tasks = array_filter($tasks, function ($item) { return !$item->active; });
            foreach ($tasks as $task) {
                $task->active = 1;
                $task->store();
            }

            $n = count($tasks);
            $message = sprintf(ngettext('%u Aufgabe wurde aktiviert.', '%u Aufgaben wurden aktiviert.', $n), $n);
            PageLayout::postMessage(MessageBox::success($message));
        } else if ($action === 'deactivate') {
            $tasks = array_filter($tasks, function ($item) { return $item->active; });
            foreach ($tasks as $task) {
                $task->active = 0;
                $task->store();
            }

            $n = count($tasks);
            $message = sprintf(ngettext('%u Aufgabe wurde deaktiviert.', '%u Aufgaben wurden deaktiviert.', $n), $n);
            PageLayout::postMessage(MessageBox::success($message));
        } else if ($action === 'delete') {
            foreach ($tasks as $task) {
                $task->delete();
            }

            $n = count($tasks);
            $message = sprintf(ngettext('%u Aufgabe wurde gelöscht.', '%u Aufgaben wurden gelöscht.', $n), $n);
            PageLayout::postMessage(MessageBox::success($message));
        }

        $this->redirect('admin/cronjobs/tasks/index/' . $page);
    }
Example #5
0
 /**
  * Edits a schedule.
  *
  * @param String $id   Id of the schedule in question (null to create)
  * @param int    $page Return to this page after editing (optional)
  */
 public function edit_action($id = null, $page = 1)
 {
     if (Request::submitted('store')) {
         $parameters = Request::getArray('parameters');
         $schedule = CronjobSchedule::find($id) ?: new CronjobSchedule();
         $schedule->title = Request::get('title');
         $schedule->description = Request::get('description');
         $schedule->active = Request::int('active', 0);
         if ($schedule->isNew()) {
             $schedule->task_id = Request::option('task_id');
         }
         $schedule->parameters = $parameters[$schedule->task_id];
         $schedule->type = Request::option('type') === 'once' ? 'once' : 'periodic';
         if ($schedule->type === 'once') {
             $temp = Request::getArray('once');
             $schedule->next_execution = strtotime($temp['date'] . ' ' . $temp['time']);
         } else {
             $temp = Request::getArray('periodic');
             $schedule->minute = $this->extractCronItem($temp['minute']);
             $schedule->hour = $this->extractCronItem($temp['hour']);
             $schedule->day = $this->extractCronItem($temp['day']);
             $schedule->month = $this->extractCronItem($temp['month']);
             $schedule->day_of_week = strlen($temp['day_of_week']['value']) ? (int) $temp['day_of_week']['value'] : null;
             if ($schedule->active) {
                 $schedule->next_execution = $schedule->calculateNextExecution();
             }
         }
         $schedule->store();
         PageLayout::postMessage(MessageBox::success(_('Die Änderungen wurden gespeichert.')));
         $this->redirect('admin/cronjobs/schedules/index/' . $page);
         return;
     }
     PageLayout::setTitle(_('Cronjob-Verwaltung') . ' - ' . _('Cronjob bearbeiten'));
     // Infobox image was produced from an image by Robbert van der Steeg
     // http://www.flickr.com/photos/robbie73/5924985913/
     $sidebar = Sidebar::Get();
     $sidebar->setImage('sidebar/date-sidebar.png');
     $sidebar->setTitle(_('Cronjobs'));
     $actions = new ActionsWidget();
     $actions->addLink(_('Zurück zur Übersicht'), $this->url_for('admin/cronjobs/schedules/index/' . $page), Icon::create('link-intern', 'clickable'));
     $sidebar->addWidget($actions);
     $this->page = $page;
     $this->tasks = CronjobTask::findBySql('1');
     $this->schedule = CronjobSchedule::find($id) ?: new CronjobSchedule();
 }
 public function down()
 {
     $task_id = CronjobTask::findOneByFilename($this->getFilename())->task_id;
     CronjobScheduler::unregisterTask($task_id);
 }
Example #7
0
 /**
  * @return bool
  */
 function gc()
 {
     if ($this->module == 'user') {
         //bail out if cronjob activated and not called in cli context
         if (Config::getInstance()->getValue('CRONJOBS_ENABLE') && ($task = array_pop(CronjobTask::findByClass('SessionGcJob'))) && count($task->schedules->findBy('active', 1)) && PHP_SAPI !== 'cli') {
             return false;
         }
         if (empty($this->gc_time)) {
             $this->gc_time = ini_get("session.gc_maxlifetime");
         }
         return $this->that->ac_gc($this->gc_time, $this->name);
     }
 }
Example #8
0
 /**
  * Unregisters a previously registered task.
  *
  * @param String $task_id Id of the task to be unregistered
  * @return CronjobScheduler to allow chaining
  * @throws InvalidArgumentException when no task with the given id exists
  */
 public static function unregister()
 {
     $class_name = get_called_class();
     $task = CronjobTask::findOneByClass($class_name);
     if ($task !== null) {
         CronjobScheduler::getInstance()->unregisterTask($task->id);
     }
 }