You are here

function drush_ultimate_cron_cron_run in Ultimate Cron 7

Same name and namespace in other branches
  1. 8.2 ultimate_cron.drush.inc \drush_ultimate_cron_cron_run()
  2. 8 ultimate_cron.drush.inc \drush_ultimate_cron_cron_run()
  3. 6 ultimate_cron.drush.inc \drush_ultimate_cron_cron_run()
  4. 7.2 ultimate_cron.drush.inc \drush_ultimate_cron_cron_run()

Run cron job(s)

File

./ultimate_cron.drush.inc, line 183
Drush commands for Ultimate Cron!

Code

function drush_ultimate_cron_cron_run($function = NULL) {
  $cli = drush_get_option('cli');
  $check_rule = drush_get_option('check-rule');
  $logfile = drush_get_option('logfile');
  $logfile = is_string($logfile) ? $logfile : '/dev/null';

  // Default to run all jobs if not specified.
  if (is_null($function)) {
    $function = 'all';
  }

  // Get global options
  $options = drush_get_context('cli');
  $cmd_options = '';

  // Determine new parameter string for sub-requests
  $passthru = array(
    'root',
    'php',
    'uri',
    'simulate',
  );
  foreach ($options as $key => $option) {
    if (in_array($key, $passthru)) {
      $cmd_options .= ' --' . $key . '=' . escapeshellarg($option);
    }
  }
  if ($function == 'all') {
    if ($cli) {
      $hooks = ultimate_cron_get_hooks();
      $schedule = ultimate_cron_get_schedule($hooks);
      foreach ($schedule as $function => $hook) {
        if (!empty($options['simulate'])) {

          // Dry-run ...
          drush_print(dt('[!function]: Simulated launch @ !ts', array(
            '!ts' => date('Y-m-d H:i:s'),
            '!function' => $function,
          )));
          continue;
        }
        drush_print(dt('[!function]: Launching @ !ts', array(
          '!ts' => date('Y-m-d H:i:s'),
          '!function' => $function,
        )));

        // Launch the sub-request
        $cmd = $_SERVER['SCRIPT_FILENAME'] . " {$cmd_options} cron-run {$function} --cli " . ($check_rule ? '--check-rule' : '');
        exec("{$cmd} >> " . escapeshellarg($logfile) . " 2>&1 &");
      }
      drush_print(dt('[!ts] Launced !jobs jobs', array(
        '!ts' => date('Y-m-d H:i:s'),
        '!jobs' => count($schedule),
      )));

      // Update drupals cron timestamp, but don't clear the cache for all variables!
      if (empty($options['simulate'])) {
        $name = 'cron_last';
        $value = time();
        global $conf;
        db_merge('variable')
          ->key(array(
          'name' => $name,
        ))
          ->fields(array(
          'value' => serialize($value),
        ))
          ->execute();
        $conf[$name] = $value;
      }
      return;
    }
    else {
      if (empty($options['simulate'])) {
        ultimate_cron_cron_run(TRUE);
      }
      $messages = drupal_get_messages();
      if (!empty($messages['status'])) {
        foreach ($messages['status'] as $message) {
          drush_print(strip_tags($message));
        }
      }
      return;
    }
  }
  $hooks = ultimate_cron_get_hooks();
  if (!isset($hooks[$function])) {
    return drush_set_error(dt('[!function]: not found', array(
      '!function' => $function,
    )));
  }
  $hook =& $hooks[$function];

  // When run manually don't double check the rules
  if (drush_get_option('check-rule')) {
    $hook['log'] = ultimate_cron_get_log($function);
    if (!ultimate_cron_hook_should_run($hook)) {
      drush_print(dt("[!function]: not sceduled to run at this time", array(
        '!function' => $function,
      )));
      return;
    }
  }
  else {
    $hook['skip_catch_up'] = TRUE;
  }
  if (!empty($options['simulate'])) {

    // Dry-run ...
    drush_print("[{$function}]: Simulated run");
    return;
  }
  if (!empty($options['simulate'])) {

    // Dry-run ...
    drush_print("[{$function}]: Simulated run");
    return;
  }
  if ($cli) {
    $start = microtime(TRUE);
    $result = ultimate_cron_run_hook_cli($function, $hook);
  }
  else {
    $result = ultimate_cron_run_hook($function, $hook);
  }
  if ($result === FALSE) {
    return drush_set_error(dt('[!function]: could not start (already running?)', array(
      '!function' => $function,
    )));
  }
  if ($cli) {
    $log = ultimate_cron_get_log($function);
    if ($log['start'] >= $start && !empty($log['msg'])) {
      drush_print("[{$function}]: " . $log['msg']);
    }
    return $result ? NULL : drush_set_error(dt('[!function]: could not start (service unavailable)', array(
      '!function' => $function,
    )));
  }
  if ($result === NULL) {
    return drush_set_error(dt('[!function]: could not start (service unavailable)', array(
      '!function' => $function,
    )));
  }
  else {
    drush_print(dt('!function started', array(
      '!function' => $function,
    )));
  }
}