You are here

public static function UltimateCronBackgroundProcessLegacyLauncher::job_callback in Ultimate Cron 7.2

Background Process legacy callback for running cron jobs.

Parameters

string $name: The name of the job.

string $lock_id: The lock id.

File

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

Class

UltimateCronBackgroundProcessLegacyLauncher
Ultimate Cron launcher plugin class.

Code

public static function job_callback($name, $lock_id, $recheck = FALSE) {
  $job = _ultimate_cron_job_load($name);
  $log_entry = $job
    ->resumeLog($lock_id);

  // If set, $recheck contains the timestamp of the last schedule check.
  if ($recheck) {

    // Simulate schedule check by setting a mock log entry object with the
    // recheck timestamp.
    $job->log_entry = $job
      ->getPlugin('logger')
      ->factoryLogEntry($job->name);
    $job->log_entry->start_time = $recheck;

    // Now we can check the scheduler.
    if (!$job
      ->getPlugin('scheduler')
      ->isScheduled($job)) {
      watchdog('bgpl_launcher', 'Recheck failed at @time', array(
        '@time' => format_date(time(), 'custom', 'Y-m-d H:i:s'),
      ), WATCHDOG_ERROR);
      $job
        ->sendSignal('background_process_legacy_dont_log');
      $log_entry
        ->finish();
      $job
        ->unlock($lock_id);
      return;
    }
    unset($job->log_entry);
  }

  // Run job.
  try {
    if ($job
      ->getPlugin('launcher')->name != 'background_process_legacy') {

      // Launcher has changed, end job/daemon.
      $log_entry
        ->finish();
      $job
        ->unlock($lock_id);
      return;
    }
    $settings = $job
      ->getSettings('launcher');
    if ($settings['daemonize']) {
      $keepalive = TRUE;
      $expire = microtime(TRUE) + (double) $settings['daemonize_interval'];
      do {
        $job
          ->run();
        if ($settings['daemonize_delay']) {
          usleep((double) $settings['daemonize_delay'] * 1000000);
        }
        if ($job
          ->getSignal('end_daemonize')) {
          watchdog('bgpl_launcher', 'end daemonize signal received', array(), WATCHDOG_NOTICE);
          $keepalive = FALSE;
          break;
        }
      } while (microtime(TRUE) < $expire);

      // Refresh disabled value.
      $job = _ultimate_cron_job_load($name, TRUE);
      $settings = $job
        ->getSettings('launcher');
      $keepalive &= empty($job->disabled);
      $keepalive &= !empty($settings['daemonize']);
      $keepalive &= !$job
        ->getSignal('end_daemonize');
      if ($keepalive) {

        // Make sure recheck isn't kept alive, as this does not make
        // any sense.
        background_process_keepalive($name, $lock_id);

        // Save a copy of the log.
        $log_entry->lid = $lock_id . '-' . uniqid('', TRUE);
        $job
          ->sendSignal('background_process_legacy_dont_log');
        $log_entry
          ->finish();

        // Restart log for keepalive.
        $log_entry->lid = $lock_id;
        $handle = background_process_current_handle();
        $process = background_process_get_process($handle);
        $log_entry->init_message = t('Re-launched at service host @name', array(
          '@name' => $process->service_host,
        ));
        $log_entry->message = '';
        $log_entry->end_time = 0;
        $log_entry->start_time = microtime(TRUE);
        $log_entry
          ->save();
      }
      else {
        $job
          ->sendSignal('background_process_legacy_dont_log');
        $log_entry
          ->finish();
        $job
          ->unlock($lock_id);
      }
    }
    else {
      $job
        ->run();
      $job
        ->sendSignal('background_process_legacy_dont_log');
      $log_entry
        ->finish();
      $job
        ->unlock($lock_id);
    }
  } catch (Throwable $e) {
    ultimate_cron_watchdog_throwable('bgpl_launcher', $e, 'Error executing %job: @error', array(
      '%job' => $job->name,
      '@error' => (string) $e,
    ), WATCHDOG_ERROR);
    $job
      ->sendSignal('background_process_legacy_dont_log');
    $log_entry
      ->finish();
    $job
      ->unlock($lock_id);
  } catch (Exception $e) {
    watchdog_exception('bgpl_launcher', $e, 'Error executing %job: @error', array(
      '%job' => $job->name,
      '@error' => (string) $e,
    ), WATCHDOG_ERROR);
    $job
      ->sendSignal('background_process_legacy_dont_log');
    $log_entry
      ->finish();
    $job
      ->unlock($lock_id);
  }
}