You are here

public static function UltimateCronBackgroundProcessLegacyLauncher::poormanLauncher in Ultimate Cron 7.2

Poorman launcher background process callback.

Parameters

string $lock_id: The lock id used for this process.

File

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

Class

UltimateCronBackgroundProcessLegacyLauncher
Ultimate Cron launcher plugin class.

Code

public static function poormanLauncher($lock_id) {
  $class = _ultimate_cron_get_class('lock');

  // Bail out if someone stole our lock.
  if (!$class::reLock($lock_id, 60)) {
    return;
  }
  ultimate_cron_poorman_page_flush();

  // Wait until it's our turn (0 seconds at next minute).
  $cron_last = variable_get('cron_last', 0);
  $cron_next = floor(($cron_last + 60) / 60) * 60;
  $time = time();
  if ($time < $cron_next) {
    $sleep = $cron_next - $time;
    sleep($sleep);
  }

  // Get settings, so we can determine the poormans cron service group.
  $plugin = _ultimate_cron_plugin_load('launcher', 'background_process_legacy');
  if (!$plugin) {
    $class::unlock($lock_id);
    throw new Exception(t('Failed to load launcher plugin?!?!?!?!'));
  }
  $settings = $plugin
    ->getDefaultSettings();

  // In case launchers fail, we don't want to relaunch this process
  // immediately.
  _ultimate_cron_variable_save('cron_last', time());

  // It's our turn!
  $launchers = array();
  foreach (_ultimate_cron_job_load_all() as $job) {
    $launcher = $job
      ->getPlugin('launcher');
    $launchers[$launcher->name] = $launcher->name;
  }
  foreach ($launchers as $name) {
    $process = new BackgroundProcess('_ultimate_cron_poorman_' . $name);
    $process->uid = 0;
    $process->service_group = $settings['poorman_service_group'];
    $process
      ->start('ultimate_cron_run_launchers', array(
      array(
        $name,
      ),
    ));
  }

  // Bail out if someone stole our lock.
  if (!$class::reLock($lock_id, 60)) {
    return;
  }

  // Wait until it's our turn (0 seconds at next minute).
  $cron_last = _ultimate_cron_variable_load('cron_last', 0);
  $cron_next = floor(($cron_last + 60) / 60) * 60;
  $time = time();
  if ($time < $cron_next) {
    $sleep = $cron_next - $time;
    sleep($sleep);
  }

  // Check poorman settings. If launcher has changed, we don't want
  // to keepalive.
  $poorman = _ultimate_cron_plugin_load('settings', 'poorman');
  if (!$poorman) {
    return;
  }
  $settings = $poorman
    ->getDefaultSettings();
  if (!$settings['launcher'] || $settings['launcher'] !== 'background_process_legacy') {
    return;
  }
  background_process_keepalive();
}