You are here

public function SerialLauncher::launchJobs in Ultimate Cron 8.2

Default implementation of jobs launcher.

Parameters

\Drupal\ultimate_cron\CronJobInterface[] $jobs: Array of UltimateCronJobs to launch.

Overrides LauncherBase::launchJobs

File

src/Plugin/ultimate_cron/Launcher/SerialLauncher.php, line 260

Class

SerialLauncher
Ultimate Cron launcher plugin class.

Namespace

Drupal\ultimate_cron\Plugin\ultimate_cron\Launcher

Code

public function launchJobs(array $jobs) {
  $lock = \Drupal::service('ultimate_cron.lock');
  $configuration = $this
    ->getConfiguration();

  // Set proper max execution time.
  $max_execution_time = ini_get('max_execution_time');
  $lock_timeout = max($max_execution_time, $configuration['timeouts']['max_execution_time']);

  // We only lock for 55 seconds at a time, to give room for other cron
  // runs.
  // @todo: Why hard-code this?
  $lock_timeout = 55;
  if (!empty($_GET['thread'])) {
    self::setGlobalOption('thread', $_GET['thread']);
  }
  if ($thread = intval(self::getGlobalOption('thread'))) {
    if ($thread < 1 || $thread > $configuration['launcher']['max_threads']) {
      \Drupal::logger('serial_launcher')
        ->warning("Invalid thread available for starting launch thread");
      return;
    }
    $lock_name = 'ultimate_cron_serial_launcher_' . $thread;
    $lock_id = NULL;
    if (!$lock
      ->isLocked($lock_name)) {
      $lock_id = $lock
        ->lock($lock_name, $lock_timeout);
    }
    if (!$lock_id) {
      \Drupal::logger('serial_launcher')
        ->warning("Thread @thread is already running", array(
        '@thread' => $thread,
      ));
    }
  }
  else {
    $timeout = 1;
    list($thread, $lock_id) = $this
      ->findFreeThread(TRUE, $lock_timeout, $timeout);
  }
  $this->currentThread = $thread;
  if (!$thread) {
    \Drupal::logger('serial_launcher')
      ->warning("No free threads available for launching jobs");
    return;
  }
  if ($max_execution_time && $max_execution_time < $configuration['timeouts']['max_execution_time']) {
    set_time_limit($configuration['timeouts']['max_execution_time']);
  }
  $this
    ->runThread($lock_id, $thread, $jobs);
  $lock
    ->unlock($lock_id);
}