You are here

public function SerialLauncher::findFreeThread in Ultimate Cron 8.2

1 call to SerialLauncher::findFreeThread()
SerialLauncher::launchJobs in src/Plugin/ultimate_cron/Launcher/SerialLauncher.php
Default implementation of jobs launcher.

File

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

Class

SerialLauncher
Ultimate Cron launcher plugin class.

Namespace

Drupal\ultimate_cron\Plugin\ultimate_cron\Launcher

Code

public function findFreeThread($lock, $lock_timeout = NULL, $timeout = 3) {
  $configuration = $this
    ->getConfiguration();

  // Find a free thread, try for 3 seconds.
  $delay = $timeout * 1000000;
  $sleep = 25000;
  $lock_service = \Drupal::service('ultimate_cron.lock');
  do {
    for ($thread = 1; $thread <= $configuration['launcher']['max_threads']; $thread++) {
      if ($thread != $this->currentThread) {
        $lock_name = 'ultimate_cron_serial_launcher_' . $thread;
        if (!$lock_service
          ->isLocked($lock_name)) {
          if ($lock) {
            if ($lock_id = $lock_service
              ->lock($lock_name, $lock_timeout)) {
              return array(
                $thread,
                $lock_id,
              );
            }
          }
          else {
            return array(
              $thread,
              FALSE,
            );
          }
        }
      }
    }
    if ($delay > 0) {
      usleep($sleep);

      // After each sleep, increase the value of $sleep until it reaches
      // 500ms, to reduce the potential for a lock stampede.
      $delay = $delay - $sleep;
      $sleep = min(500000, $sleep + 25000, $delay);
    }
  } while ($delay > 0);
  return array(
    FALSE,
    FALSE,
  );
}