You are here

ultimate_cron.install in Ultimate Cron 7

Installation file for Ultimate Cron

File

ultimate_cron.install
View source
<?php

/**
 * @file
 *
 * Installation file for Ultimate Cron
 */

/**
 * Implements hook_schema().
 */
function ultimate_cron_schema() {
  $schema = array();
  $schema['ultimate_cron'] = array(
    'description' => 'Cron job function list',
    'export' => array(
      'key' => 'name',
      'key name' => 'Function name',
      'primary key' => 'fid',
      'identifier' => 'name',
      'default hook' => 'default_ultimate_cron_function',
      'save callback' => 'ultimate_cron_crud_save',
      'api' => array(
        'owner' => 'ultimate_cron',
        'api' => 'default_ultimate_cron_functions',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'fid' => array(
        'description' => 'Function ID',
        'type' => 'serial',
        'size' => 'normal',
        'not null' => TRUE,
        'no export' => TRUE,
      ),
      'name' => array(
        'description' => 'Function name',
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
      'settings' => array(
        'description' => 'Settings',
        'type' => 'text',
        'serialize' => TRUE,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'fid',
    ),
    'unique keys' => array(
      'uniq_name' => array(
        'name',
      ),
    ),
  );
  $schema['ultimate_cron_log'] = array(
    'description' => 'Logs',
    'fields' => array(
      'lid' => array(
        'description' => 'Log ID',
        'type' => 'serial',
        'size' => 'normal',
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Function name',
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
      'start_stamp' => array(
        'description' => 'Timstamp of execution start',
        'type' => 'float',
        'size' => 'big',
        'not null' => TRUE,
        'default' => 0,
      ),
      'end_stamp' => array(
        'description' => 'Timstamp of execution end',
        'type' => 'float',
        'size' => 'big',
        'not null' => TRUE,
        'default' => 0,
      ),
      'service_host' => array(
        'description' => 'Service host',
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
      'exec_status' => array(
        'description' => 'Status of the execution',
        'type' => 'int',
        'size' => 'normal',
        'not null' => FALSE,
        'default' => NULL,
      ),
      'severity' => array(
        'description' => 'Log level severity',
        'type' => 'int',
        'size' => 'normal',
        'not null' => TRUE,
        'default' => -1,
      ),
      'msg' => array(
        'description' => 'Message',
        'type' => 'text',
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'lid',
    ),
    'indexes' => array(
      'idx_last' => array(
        'name',
        'start_stamp',
      ),
      'idx_count' => array(
        'name',
        'end_stamp',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_enable().
 */
function ultimate_cron_enable() {

  // Disable built-in poor mans cron
  variable_set('cron_safe_threshold', 0);
}

/**
 * Implements hook_uninstall().
 */
function ultimate_cron_uninstall() {
  variable_del('ultimate_cron_simultaneous_connections');
  variable_del('ultimate_cron_rule');
  variable_del('ultimate_cron_cleanup_log');
  variable_del('ultimate_cron_catch_up');
  variable_del('ultimate_cron_queue_polling_latency');
  variable_del('ultimate_cron_queue_lease_time');
  variable_del('ultimate_cron_poorman');
  variable_del('ultimate_cron_launch_window');
  variable_del('ultimate_cron_max_execution_time');
}

/**
 * Implements hook_requirements().
 */
function ultimate_cron_requirements($phase) {
  $response = array();
  switch ($phase) {
    case 'install':
      return $response;
    case 'runtime':
      $t = get_t();
      $response['title'] = 'Ultimate Cron';
      $response['value'] = $t('OK');
      $response['severity'] = REQUIREMENT_OK;
      if ($modules = _ultimate_cron_incompatible_modules()) {
        $response['severity'] = REQUIREMENT_ERROR;
        $response['value'] = $t('Ultimate Cron is DISABLED!');
        $response['description'] = $t('%modules is installed on the system, but is incompatible with Ultimate Cron.<br/>Please disable the modules %modules.<br/>You might want to !url settings first.', array(
          '%modules' => join(', ', $modules),
          '!url' => l(t('import'), 'admin/settings/cron/import'),
        ));
      }
      if (ini_get('safe_mode')) {
        $desc = $t('Safe mode enabled. Ultimate Cron is unable to control maximum execution time for cron jobs. This may cause cron jobs to end prematurely.');
        if ($response['severity'] < REQUIREMENT_WARNING) {
          $response['severity'] = REQUIREMENT_WARNING;
          $response['value'] = $t('Safe mode enabled');
          $response['description'] = $desc;
        }
        else {
          $response['description'] .= '<br/>' . $desc;
        }
      }
      $result = array();
      $result['ultimate_cron'] = $response;
      return $result;
  }
}

/**
 * Major version upgrade of Drupal
 */
function ultimate_cron_update_7000() {
}

/**
 * Move messages to log table.
 */
function ultimate_cron_update_7101() {
  if (!db_field_exists('ultimate_cron_log', 'msg')) {

    // 'msg' doesn't exist, so this update has never run and was never run when
    // it was previously called ultimate_cron_update_6000() or
    // ultimate_cron_update_7000() - hence, it needs to be run.
    db_add_field('ultimate_cron_log', 'msg', array(
      'description' => 'Message',
      'type' => 'text',
      'not null' => FALSE,
    ));
    db_query("UPDATE {ultimate_cron_log} l JOIN {ultimate_cron_log_message} m ON l.lid = m.lid SET l.msg = m.msg");
    db_drop_table('ultimate_cron_log_message');
  }
}

/**
 * Convert polling latenct from microseconds to miliseconds.
 */
function ultimate_cron_update_7102() {

  // There's no safe way to check if this update has been run before or not,
  // as it could have run when this update was previously called
  // ultimate_cron_update_6001() or ultimate_cron_update_7001() - all we can do
  // is run it anyway and issue a message asking the admin to check the polling
  // latency.
  // Convert polling latency from microseconds to miliseconds.
  $polling_latency = variable_get('ultimate_cron_queue_polling_latency', NULL);
  if ($polling_latency) {
    $polling_latency /= 1000;
    variable_set('ultimate_cron_queue_polling_latency', $polling_latency);
    return t('Please double check your Ultimate Cron <a href="@admin">Queue polling latency</a>', array(
      '@admin' => url('admin/config/system/cron/settings'),
    ));
  }
}

/**
 * Merge ultimate_cron_function and ultimate_cron_configuration into ultimate_cron
 */
function ultimate_cron_update_7103() {
  if (!db_table_exists('ultimate_cron')) {

    // 'ultimate_cron' doesn't exist, so this update has never run and was
    // never run when it was previously called ultimate_cron_update_6002() or
    // ultimate_cron_update_7002() - hence, it needs to be run.
    // NOTE - Include the schema here, as referring to ultimate_cron_schema()
    // will cause errors in ultimate_cron_update_7105().
    $schema = array();
    $schema['ultimate_cron'] = array(
      'description' => 'Cron job function list',
      'export' => array(
        'key' => 'function',
        'key name' => 'Function name',
        'primary key' => 'fid',
        'identifier' => 'function',
        'default hook' => 'default_ultimate_cron_function',
        'api' => array(
          'owner' => 'ultimate_cron',
          'api' => 'default_ultimate_cron_functions',
          'minimum_version' => 1,
          'current_version' => 1,
        ),
      ),
      'fields' => array(
        'fid' => array(
          'description' => 'Function ID',
          'type' => 'serial',
          'size' => 'normal',
          'not null' => TRUE,
          'no export' => TRUE,
        ),
        'function' => array(
          'description' => 'Function name',
          'type' => 'varchar',
          'length' => 127,
          'not null' => TRUE,
        ),
        'settings' => array(
          'description' => 'Settings',
          'type' => 'text',
          'serialize' => TRUE,
          'not null' => FALSE,
        ),
      ),
      'primary key' => array(
        'fid',
      ),
      'unique key' => array(
        'function',
      ),
    );
    db_create_table('ultimate_cron', $schema['ultimate_cron']);
    db_query("INSERT INTO {ultimate_cron} SELECT f.fid, f.function, c.configuration AS settings FROM ultimate_cron_function f LEFT JOIN {ultimate_cron_configuration} c ON f.fid = c.fid");
    db_drop_table('ultimate_cron_function');
    db_drop_table('ultimate_cron_configuration');
  }
}

/**
 * Switch from fid to function name in ultimate_cron_log
 */
function ultimate_cron_update_7104() {
  if (!db_field_exists('ultimate_cron_log', 'name') && !db_field_exists('ultimate_cron_log', 'function')) {

    // 'name' doesn't exist, so update ultimate_cron_update_7106() has never
    // run and was never run when it was previously called
    // ultimate_cron_update_6106(). If one of these updates had run, we would
    // have already done this update and could safely skip it.
    // 'function' also does not exist, so this update has never run and was never run when
    // it was previously called ultimate_cron_update_6003() or
    // ultimate_cron_update_7003() - hence, it needs to be run
    db_add_field('ultimate_cron_log', 'function', array(
      'description' => 'Function name',
      'type' => 'varchar',
      'length' => 127,
      'not null' => TRUE,
      'default' => '',
      'initial' => 'function',
    ));
    $fids = db_select('ultimate_cron_log', 'u')
      ->fields('u', array(
      'fid',
    ))
      ->distinct()
      ->execute();
    while ($fid = $fids
      ->fetchObject()) {
      $function = db_select('ultimate_cron', 'u')
        ->fields('u', array(
        'function',
      ))
        ->condition('fid', $fid->fid, '=')
        ->execute()
        ->fetchObject();
      db_update('ultimate_cron_log')
        ->fields(array(
        'function' => $function->function,
      ))
        ->condition('fid', $fid->fid, '=')
        ->execute();
    }
    db_drop_field('ultimate_cron_log', 'fid');
    db_drop_index('ultimate_cron_log', 'idx_last');
    db_drop_index('ultimate_cron_log', 'idx_count');
    db_add_index('ultimate_cron_log', 'idx_last', array(
      'function',
      'start',
    ));
    db_add_index('ultimate_cron_log', 'idx_count', array(
      'function',
      'end',
    ));
  }
}

/**
 * Add missing index to database
 */
function ultimate_cron_update_7105() {
  if (!db_field_exists('ultimate_cron_log', 'name') && !db_index_exists('ultimate_cron', 'uniq_function')) {

    // 'name' doesn't exist, so update ultimate_cron_update_7106() has never
    // run and was never run when it was previously called
    // ultimate_cron_update_6106(). If one of these updates had run, we would
    // have already done this update and could safely skip it.
    // 'uniq_function' also does not exist, so this update has never run and
    // was never run when it was previously called ultimate_cron_update_6105()
    // - hence, it needs to be run
    $items = db_select('ultimate_cron')
      ->fields('ultimate_cron', array(
      'fid',
      'function',
    ))
      ->orderBy('fid', 'ASC')
      ->execute()
      ->fetchAllAssoc('fid', PDO::FETCH_ASSOC);
    $fids = array();
    $keep = array();
    foreach ($items as $item) {
      if (isset($keep[$item['function']])) {
        $fids[] = $keep[$item['function']];
      }
      $keep[$item['function']] = $item['fid'];
    }
    if ($fids) {
      db_delete('ultimate_cron')
        ->condition('fid', $fids)
        ->execute();
    }
    db_add_unique_key('ultimate_cron', 'uniq_function', array(
      'function',
    ));
  }
}

/**
 * Change schema to SQL 99 compliance
 */
function ultimate_cron_update_7106() {

  // Check each change in case it was previously run when this udpate was
  // called ultimate_cron_update_6106().
  if (db_index_exists('ultimate_cron', 'idx_function')) {
    db_drop_unique_key('ultimate_cron', 'idx_function');
  }
  if (db_field_exists('ultimate_cron', 'function')) {
    db_change_field('ultimate_cron', 'function', 'name', array(
      'description' => 'Function name',
      'type' => 'varchar',
      'length' => 127,
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (!db_index_exists('ultimate_cron', 'idx_name')) {
    db_add_unique_key('ultimate_cron', 'idx_name', array(
      'name',
    ));
  }
  if (db_field_exists('ultimate_cron_log', 'function')) {
    db_change_field('ultimate_cron_log', 'function', 'name', array(
      'description' => 'Function name',
      'type' => 'varchar',
      'length' => 127,
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (db_field_exists('ultimate_cron_log', 'start')) {
    db_change_field('ultimate_cron_log', 'start', 'start_stamp', array(
      'description' => 'Timstamp of execution start',
      'type' => 'float',
      'size' => 'big',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
  if (db_field_exists('ultimate_cron_log', 'end')) {
    db_change_field('ultimate_cron_log', 'end', 'end_stamp', array(
      'description' => 'Timstamp of execution end',
      'type' => 'float',
      'size' => 'big',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
  if (db_field_exists('ultimate_cron_log', 'status')) {
    db_change_field('ultimate_cron_log', 'status', 'exec_status', array(
      'description' => 'Status of the execution',
      'type' => 'int',
      'size' => 'normal',
      'not null' => FALSE,
      'default' => NULL,
    ));
  }
}

/**
 * Add service host to log.
 */
function ultimate_cron_update_7107() {

  // This update is part of the the Drupal 6.x branch, so we don't need to
  // safety check it.
  db_add_field('ultimate_cron_log', 'service_host', array(
    'description' => 'Service host',
    'type' => 'varchar',
    'length' => 127,
    'not null' => TRUE,
    'default' => '',
  ));
}

/**
 * Add severity level to log.
 */
function ultimate_cron_update_7108() {
  if (!db_field_exists('ultimate_cron_log', 'severity')) {

    // 'severity' does not exist, so this update was not run when it was called
    // ultimate_cron_update_6107() - hence it needs to be run.
    db_add_field('ultimate_cron_log', 'severity', array(
      'description' => 'Log level severity',
      'type' => 'int',
      'size' => 'normal',
      'not null' => TRUE,
      'default' => -1,
    ));
  }
}

/**
 * Rename uniq_function to uniq_name to match the schema
 */
function ultimate_cron_update_7109() {

  // ultimate_cron_update_7105() adds unique key 'uniq_function', but
  // ultimate_cron_schema() creates 'uniq_name' - so we must correct that here.
  if (db_index_exists('ultimate_cron', 'uniq_function')) {
    db_drop_unique_key('ultimate_cron', 'uniq_function');
    db_add_unique_key('ultimate_cron', 'uniq_name', array(
      'name',
    ));
  }
}

Functions

Namesort descending Description
ultimate_cron_enable Implements hook_enable().
ultimate_cron_requirements Implements hook_requirements().
ultimate_cron_schema Implements hook_schema().
ultimate_cron_uninstall Implements hook_uninstall().
ultimate_cron_update_7000 Major version upgrade of Drupal
ultimate_cron_update_7101 Move messages to log table.
ultimate_cron_update_7102 Convert polling latenct from microseconds to miliseconds.
ultimate_cron_update_7103 Merge ultimate_cron_function and ultimate_cron_configuration into ultimate_cron
ultimate_cron_update_7104 Switch from fid to function name in ultimate_cron_log
ultimate_cron_update_7105 Add missing index to database
ultimate_cron_update_7106 Change schema to SQL 99 compliance
ultimate_cron_update_7107 Add service host to log.
ultimate_cron_update_7108 Add severity level to log.
ultimate_cron_update_7109 Rename uniq_function to uniq_name to match the schema