You are here

function radioactivity_cron in Radioactivity 6

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. 7.2 radioactivity.module \radioactivity_cron()
  5. 7 radioactivity.module \radioactivity_cron()
  6. 4.0.x radioactivity.module \radioactivity_cron()

File

./radioactivity.module, line 104

Code

function radioactivity_cron() {
  if (radioactivity_get_memcached_enable()) {
    radioactivity_process_memcached_entries();
  }
  $timestamp = time();

  // last cron
  $last_cron_timestamp = (int) variable_get('radioactivity_last_cron_timestamp', 0);
  $granularity = (int) _radioactivity_get_decay_granularity();
  $threshold_timestamp = $last_cron_timestamp - $last_cron_timestamp % $granularity + $granularity;
  if ($timestamp < $threshold_timestamp) {
    return;
  }

  // don't update yet
  foreach (radioactivity_get_decay_profiles() as $dpid => $decay_profile) {
    $half_life = (double) $decay_profile["half_life"];
    $cut_off_energy = abs((double) $decay_profile["cut_off_energy"]);

    // the formula is:
    // E=E_0 * 2^(- delta_time / half_life)
    db_query("UPDATE {radioactivity} SET energy=energy * pow(2, (last_emission_timestamp*1.0-%d)/%f), last_emission_timestamp=%d " . "WHERE decay_profile=%d AND last_emission_timestamp<%d", $timestamp, $half_life, $timestamp, $dpid, $timestamp);
    if ($cut_off_energy == 0) {
      continue;
    }

    // cut-off disabled
    // Delete radioactivity info from objects that have less absolute energy than cut_off_energy.
    // We do (energy<cut_off AND energy>-cut_off) instead of (abs(energy)<cut_off), as supposedly
    // that requires less cleverness from a SQL engine to do a decent job with indexes.
    db_query("DELETE FROM {radioactivity} WHERE decay_profile=%d AND energy < %f AND energy > %f", $dpid, $cut_off_energy, -$cut_off_energy);
  }
  variable_set('radioactivity_last_cron_timestamp', $timestamp);
}