You are here

public function UltimateCronSerialLauncher::findFreeThread in Ultimate Cron 7.2

Find a free thread for running cron jobs.

1 call to UltimateCronSerialLauncher::findFreeThread()
UltimateCronSerialLauncher::launchJobs in plugins/ultimate_cron/launcher/serial.class.php
Launch manager.

File

plugins/ultimate_cron/launcher/serial.class.php, line 249
Serial cron job launcher for Ultimate Cron.

Class

UltimateCronSerialLauncher
Ultimate Cron launcher plugin class.

Code

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

  // Find a free thread, try for 3 seconds.
  $delay = $timeout * 1000000;
  $sleep = 25000;
  $class = _ultimate_cron_get_class('lock');
  do {
    for ($thread = 1; $thread <= $settings['max_threads']; $thread++) {
      if ($thread != $this->currentThread) {
        $lock_name = 'ultimate_cron_serial_launcher_' . $thread;
        if (!$class::isLocked($lock_name)) {
          if ($lock) {
            if ($lock_id = $class::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,
  );
}