You are here

function radioactivity_cron in Radioactivity 7.2

Same name and namespace in other branches
  1. 8.3 radioactivity.module \radioactivity_cron()
  2. 8.2 radioactivity.module \radioactivity_cron()
  3. 5 radioactivity.module \radioactivity_cron()
  4. 6 radioactivity.module \radioactivity_cron()
  5. 7 radioactivity.module \radioactivity_cron()
  6. 4.0.x radioactivity.module \radioactivity_cron()

Implements hook_cron().

1 call to radioactivity_cron()
drush_radioactivity_cron in ./radioactivity.drush.inc

File

./radioactivity.module, line 441
Radioactivity core functionality

Code

function radioactivity_cron() {

  // handle payload cached in a file
  module_load_include("inc", "radioactivity", "radioactivity-bootstrap");
  if (class_exists("Memcache") || class_exists("Memcached")) {

    // Expose the memcache settings for the memcache processIncident call
    if (!defined("VAR_RADIOACTIVITY_MEMCACHED_HOST")) {
      define("VAR_RADIOACTIVITY_MEMCACHED_HOST", variable_get("radioactivity_memcached_host", "localhost"));
      define("VAR_RADIOACTIVITY_MEMCACHED_PORT", variable_get("radioactivity_memcached_port", "11211"));
      define("VAR_RADIOACTIVITY_MEMCACHED_PREFIX", variable_get("radioactivity_memcached_prefix", ""));
    }
  }
  if (class_exists("Redis")) {

    // Expose the redis settings for the memcache processIncident call
    if (!defined("VAR_RADIOACTIVITY_REDIS_HOST")) {
      define("VAR_RADIOACTIVITY_REDIS_HOST", variable_get("radioactivity_redis_host", "localhost"));
      define("VAR_RADIOACTIVITY_REDIS_PORT", variable_get("radioactivity_redis_port", "6379"));
    }
  }
  $last_cron_timestamp = variable_get('radioactivity_last_cron_timestamp', REQUEST_TIME);
  $fields = field_info_fields();
  foreach ($fields as $field_name => &$field) {
    if ($field['type'] != RADIOACTIVITY_FIELD_TYPE) {
      continue;
    }
    foreach ($field['bundles'] as $entity_type => &$bundles) {
      foreach ($bundles as $bundle) {
        $profile = radioactivity_get_field_profile($entity_type, $bundle, $field_name);
        if (!($profile && $profile->storageObject)) {
          watchdog("radioactivity", "Could not load profile for @type @bundle @field", array(
            "@type" => $entity_type,
            "@bundle" => $bundle,
            "@field" => $field_name,
          ), WATCHDOG_ERROR);
          continue;
        }
        $storage = $profile->storageObject;

        // Process incidents (Note: this will process all incidents within the
        // storage, even those unrelated to THIS entity).
        $storage
          ->processIncidents();

        // Check granularity
        $threshold_timestamp = $last_cron_timestamp - $last_cron_timestamp % $profile->granularity + $profile->granularity;

        // Update field database
        $half_life = $profile->half_life;
        $cut_off = $profile->cut_off;
        $table_name = 'field_data_' . $field_name;
        $energy = $field_name . '_' . RADIOACTIVITY_FIELD_ENERGY;
        $timestamp = $field_name . '_' . RADIOACTIVITY_FIELD_TIMESTAMP;

        // grab update value from deferred values table
        // and update it to the fields table if it is used
        if ($profile->enable_decay > 0 && REQUEST_TIME > $threshold_timestamp) {

          // Switched deletion and decaying order because in
          // some cases the decaying stops working when there are sh*tloads of fields.
          // Brute deletion of the field might not be ok but the field api can handle this
          // TODO: figure out how to do this in batches...
          if (module_exists('rules')) {

            // Invoke rules event for cut off
            $items = db_select($table_name, 't')
              ->fields('t', array(
              'entity_type',
              'entity_id',
            ))
              ->condition("bundle", $bundle)
              ->condition($energy, $cut_off, '<')
              ->condition("deleted", "0")
              ->execute();
            while ($item = $items
              ->fetchObject()) {

              // Don't cache - conserves memory
              $entity = entity_load($item->entity_type, array(
                $item->entity_id,
              ), array(), TRUE);
              $entity = entity_metadata_wrapper($item->entity_type, array_shift($entity));
              rules_invoke_event('radioactivity_field_cut_off', $entity);
            }
          }
          $cuts = db_delete($table_name)
            ->condition("bundle", $bundle)
            ->condition($energy, $cut_off, '<')
            ->condition("deleted", "0")
            ->execute();

          // Run the updates
          $updated = db_update($table_name)
            ->expression($energy, $energy . ' * pow(2, (' . $timestamp . ' * 1.0 - ' . REQUEST_TIME . ') / ' . $half_life . ')')
            ->fields(array(
            $timestamp => REQUEST_TIME,
          ))
            ->condition("bundle", $bundle)
            ->condition($timestamp, REQUEST_TIME, '<')
            ->condition("deleted", "0")
            ->execute();
        }

        // Decay
        $field_info = field_info_instance($entity_type, $field_name, $bundle);
        if ($field_info['settings']['history'] == 1) {
          db_query("INSERT INTO {radioactivity_history}" . " (SELECT :id, entity_id, :time, " . $energy . " FROM {" . $table_name . "}" . " WHERE deleted = 0)", array(
            ':id' => $field_info['id'],
            ':time' => REQUEST_TIME,
          ));
          $time = REQUEST_TIME - $field_info['settings']['history_limit'] * 60 * 60;
          db_delete('radioactivity_history')
            ->condition('time', $time, '<')
            ->condition('field_instance_id', $field_info['id'])
            ->execute();
        }

        // History
      }

      // Bundles
      // Clear the entity caches.
      entity_get_controller($entity_type)
        ->resetCache();

      // Clear the entity field caches.
      cache_clear_all('field:' . $entity_type, 'cache_field', TRUE);
    }
  }

  // remove events that are twice as old as the timeout is
  $timeout2 = variable_get('radioactivity_flood_timeout', 15) * 120;

  // Groom the flood cache
  db_delete('radioactivity_flood_map')
    ->condition('time', REQUEST_TIME - $timeout2, "<")
    ->execute();
  variable_set('radioactivity_last_cron_timestamp', REQUEST_TIME);
}