You are here

public function UltimateCronBackgroundProcessLegacyLauncher::launchJobs in Ultimate Cron 7.2

Launch manager.

Overrides UltimateCronLauncher::launchJobs

File

plugins/ultimate_cron/launcher/background_process_legacy.class.php, line 443
Background Process 1.x launcher for Ultimate Cron.

Class

UltimateCronBackgroundProcessLegacyLauncher
Ultimate Cron launcher plugin class.

Code

public function launchJobs($jobs) {
  $this->scheduledLaunch = TRUE;
  $settings = $this
    ->getDefaultSettings();

  // Don't use more than 55 seconds for launching jobs.
  // If we fail, we will try again next time.
  $timeout = 55;
  $expire = microtime(TRUE) + 55;
  while ($jobs && microtime(TRUE) < $expire) {
    $threads = $this
      ->numberOfProcessesRunning();
    foreach ($jobs as $job) {
      if (!$job
        ->isScheduled()) {
        unset($jobs[$job->name]);
        continue;
      }
      if (empty($job->hook['override_congestion_protection'])) {

        // Skip if we're congested.
        if ($threads >= $settings['max_threads']) {
          continue;
        }
      }

      // Everything's good. Launch job!
      $job_settings = $job
        ->getSettings($this->type);
      $job->recheck = !self::getGlobalOption('bypass_schedule') && $job_settings['recheck'];
      $job
        ->launch();
      unset($jobs[$job->name]);
      $threads = $this
        ->numberOfProcessesRunning();
    }

    // If there are still jobs left to be launched, wait a little.
    if ($jobs) {
      sleep(1);
    }
  }

  // Bail out if we expired.
  if (microtime(TRUE) >= $expire) {
    watchdog('bgpl_launcher', 'Background Process launcher exceed time limit of @timeout seconds.', array(
      '@timeout' => $timeout,
    ), WATCHDOG_NOTICE);
  }
  if ($jobs) {
    watchdog('bgpl_launcher', '@jobs jobs missed their schedule due to congestion.', array(
      '@jobs' => count($jobs),
    ), WATCHDOG_NOTICE);
    foreach ($jobs as $name => $job) {
      if ($lock_id = $job
        ->lock()) {
        $log_entry = $jobs[$name]
          ->startLog($lock_id, 'congestion');
        $log_entry
          ->log('bgpl_launcher', 'Missed schedule due to congestion', array(), WATCHDOG_NOTICE);
        $log_entry
          ->finish();
        $job
          ->sendSignal('background_process_legacy_dont_log');
        $job
          ->unlock();
      }
    }
  }
}