"Fossies" - the Fresh Open Source Software Archive

Member "4.6.1/db/migrations/104_setup_cronjobs.php" (8 Apr 2021, 6673 Bytes) of package /linux/www/studip-4.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 class SetupCronjobs extends Migration
    3 {
    4     function description()
    5     {
    6         return 'Creates cronjob tables in database and according config entries';
    7     }
    8 
    9     function up()
   10     {
   11         DBManager::get()->query("CREATE TABLE IF NOT EXISTS `cronjobs_tasks` (
   12             `task_id` CHAR(32) NOT NULL DEFAULT '',
   13             `filename` VARCHAR(255) NOT NULL,
   14             `class` VARCHAR(255) NOT NULL,
   15             `active` TINYINT(1) NOT NULL DEFAULT 0,
   16             `execution_count` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
   17             `assigned_count` INT(11) UNSIGNED NOT NULL DEFAULT 0,
   18             PRIMARY KEY (`task_id`)
   19         ) ENGINE=MyISAM");
   20 
   21         DBManager::get()->query("CREATE TABLE IF NOT EXISTS `cronjobs_schedules` (
   22             `schedule_id` CHAR(32) NOT NULL DEFAULT '',
   23             `task_id` CHAR(32) NOT NULL DEFAULT '',
   24             `active` TINYINT(1) NOT NULL DEFAULT 0,
   25             `title` VARCHAR(255) NULL DEFAULT NULL,
   26             `description` VARCHAR(4096) DEFAULT NULL,
   27             `parameters` TEXT,
   28             `priority` ENUM('low','normal','high') NOT NULL DEFAULT 'normal',
   29             `type` ENUM('periodic','once') NOT NULL DEFAULT 'periodic',
   30             `minute` TINYINT(2) DEFAULT NULL,
   31             `hour` TINYINT(2) DEFAULT NULL,
   32             `day` TINYINT(2) DEFAULT NULL,
   33             `month` TINYINT(2) DEFAULT NULL,
   34             `day_of_week` TINYINT(1) UNSIGNED DEFAULT NULL,
   35             `next_execution` INT(11) UNSIGNED NOT NULL DEFAULT 0,
   36             `last_execution` INT(11) UNSIGNED DEFAULT NULL,
   37             `last_result` TEXT,
   38             `execution_count` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
   39             `mkdate` INT(11) UNSIGNED NOT NULL,
   40             `chdate` INT(11) UNSIGNED NOT NULL,
   41             PRIMARY KEY (`schedule_id`),
   42             KEY `task_id` (`task_id`)
   43         ) ENGINE=MyISAM");
   44 
   45         DBManager::get()->query("CREATE TABLE IF NOT EXISTS `cronjobs_logs` (
   46             `log_id` CHAR(32) NOT NULL DEFAULT '',
   47             `schedule_id` CHAR(32) NOT NULL DEFAULT '',
   48             `scheduled` INT(11) UNSIGNED NOT NULL,
   49             `executed` INT(11) UNSIGNED NOT NULL,
   50             `exception` TEXT DEFAULT NULL,
   51             `output` TEXT,
   52             `duration` FLOAT NOT NULL,
   53             PRIMARY KEY (`log_id`),
   54             KEY `schedule_id` (`schedule_id`)
   55         ) ENGINE=MyISAM");
   56 
   57         // Add config entries
   58         $query = "INSERT IGNORE INTO `config`
   59                     (`config_id`, `field`, `value`, `is_default`, `type`, `range`, `section`,
   60                      `mkdate`, `chdate`, `description`)
   61                   VALUES (MD5(:field), :field, :value, 1, :type, 'global', 'global',
   62                           UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :description)";
   63         $statement = DBManager::get()->prepare($query);
   64 
   65         $statement->execute([
   66             ':field' => 'CRONJOBS_ENABLE',
   67             ':value' => (int)false,
   68             ':type'  => 'boolean',
   69             ':description' => 'Schaltet die Cronjobs an',
   70         ]);
   71 
   72         $statement->execute([
   73             ':field' => 'CRONJOBS_ESCALATION',
   74             ':value' => 300,
   75             ':type'  => 'integer',
   76             ':description' => 'Gibt an, nach wievielen Sekunden ein Cronjob als steckengeblieben angesehen wird',
   77         ]);
   78 
   79         // Add default cron tasks and schedules
   80         $default_data = [
   81             [
   82                 'filename'    => 'lib/cronjobs/cleanup_log.class.php',
   83                 'class'       => 'CleanupLogJob',
   84                 'priority'    => 'normal',
   85                 'hour'        => 2,
   86                 'minute'      => 13,
   87             ],
   88             [
   89                 'filename'    => 'lib/cronjobs/purge_cache.class.php',
   90                 'class'       => 'PurgeCacheJob',
   91                 'priority'    => 'low',
   92                 'hour'        => null,
   93                 'minute'      => -30,
   94             ],
   95             [
   96                 'filename'    => 'lib/cronjobs/send_mail_notifications.class.php',
   97                 'class'       => 'SendMailNotificationsJob',
   98                 'priority'    => 'high',
   99                 'hour'        => 1,
  100                 'minute'      => 7,
  101             ],
  102             [
  103                 'filename'    => 'lib/cronjobs/check_admission.class.php',
  104                 'class'       => 'CheckAdmissionJob',
  105                 'priority'    => 'normal',
  106                 'hour'        => null,
  107                 'minute'      => -30,
  108             ],
  109             [
  110                 'filename'    => 'lib/cronjobs/garbage_collector.class.php',
  111                 'class'       => 'GarbageCollectorJob',
  112                 'priority'    => 'normal',
  113                 'hour'        => 2,
  114                 'minute'      => 33,
  115             ],
  116             [
  117                 'filename'    => 'lib/cronjobs/session_gc.class.php',
  118                 'class'       => 'SessionGcJob',
  119                 'priority'    => 'normal',
  120                 'hour'        => 3,
  121                 'minute'      => 13,
  122             ],
  123         ];
  124 
  125         $query = "INSERT IGNORE INTO `cronjobs_tasks`
  126                     (`task_id`, `filename`, `class`, `active`)
  127                   VALUES (:task_id, :filename, :class, 1)";
  128         $task_statement = DBManager::get()->prepare($query);
  129 
  130         $query = "INSERT IGNORE INTO `cronjobs_schedules`
  131                     (`schedule_id`, `task_id`, `parameters`, `priority`,
  132                      `type`, `minute`, `hour`, `mkdate`, `chdate`,
  133                      `last_result`)
  134                   VALUES (:schedule_id, :task_id, '[]', :priority, 'periodic',
  135                           :minute, :hour, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(),
  136                           NULL)";
  137         $schedule_statement = DBManager::get()->prepare($query);
  138 
  139         foreach ($default_data as $row) {
  140             $task_id = md5(uniqid('task', true));
  141 
  142             $task_statement->execute([
  143                 ':task_id'  => $task_id,
  144                 ':filename' => $row['filename'],
  145                 ':class'    => $row['class'],
  146             ]);
  147 
  148             $schedule_id = md5(uniqid('schedule', true));
  149             $schedule_statement->execute([
  150                 ':schedule_id' => $schedule_id,
  151                 ':task_id'     => $task_id,
  152                 ':priority'    => $row['priority'],
  153                 ':hour'        => $row['hour'],
  154                 ':minute'      => $row['minute'],
  155             ]);
  156         }
  157     }
  158 
  159     function down()
  160     {
  161         DBManager::get()->query("DROP TABLE IF EXISTS `cronjobs_tasks`, `cronjobs_schedules`, `cronjobs_logs`");
  162 
  163         DBManager::get()->query("DELETE FROM config WHERE field IN ('CRONJOB_ENABLE', 'CRONJOBS_ESCALATION')");
  164     }
  165 }