You are here

abstract class UltimateCronLauncher in Ultimate Cron 7.2

Abstract class for Ultimate Cron launchers.

A launcher is responsible for locking and launching/running a job.

Abstract methods: lock($job)

  • Lock a job. This method must return the lock_id on success or FALSE on failure.

unlock($lock_id, $manual = FALSE)

  • Release a specific lock id. If $manual is set, then the release was triggered manually by a user.

isLocked($job)

  • Check if a job is locked. This method must return the current
  • lock_id for the given job, or FALSE if it is not locked.

launch($job)

  • This method launches/runs the given job. This method must handle the locking of job before launching it. Returns TRUE on successful launch, FALSE if not.

Important methods: isLockedMultiple($jobs)

  • Check locks for multiple jobs. Each launcher should implement an optimized version of this method if possible.

launchJobs($jobs)

  • Launches the jobs provided to it. A default implementation of this exists, but can be overridden. It is assumed that this function checks the jobs schedule before launching and that it also handles locking wrt concurrency for the launcher itself.

launchPoorman()

  • Launches all scheduled jobs via the proper launcher for each jobs. This method only needs to be implemented if the launcher wishes to provide a poormans cron launching mechanism. It is assumed that the poormans cron launcher handles locking wrt concurrency, etc.

Hierarchy

Expanded class hierarchy of UltimateCronLauncher

3 string references to 'UltimateCronLauncher'
background_process_legacy.inc in plugins/ultimate_cron/launcher/background_process_legacy.inc
serial.inc in plugins/ultimate_cron/launcher/serial.inc
ultimate_cron_ctools_plugin_type in ./ultimate_cron.module
Implements hook_ctools_plugin_type().

File

./ultimate_cron.plugin.inc, line 907
Plugin framework for Ultimate Cron.

View source
abstract class UltimateCronLauncher extends UltimateCronPlugin {

  /**
   * Default settings.
   */
  public function defaultSettings() {
    return array();
  }

  /**
   * Lock job.
   *
   * @param UltimateCronJob $job
   *   The job to lock.
   *
   * @return string
   *   Lock ID or FALSE.
   */
  public abstract function lock($job);

  /**
   * Unlock a lock.
   *
   * @param string $lock_id
   *   The lock id to unlock.
   * @param bool $manual
   *   Whether this is a manual unlock or not.
   *
   * @return bool
   *   TRUE on successful unlock.
   */
  public abstract function unlock($lock_id, $manual = FALSE);

  /**
   * Check if a job is locked.
   *
   * @param UltimateCronJob $job
   *   The job to check.
   *
   * @return string
   *   Lock ID of the locked job, FALSE if not locked.
   */
  public abstract function isLocked($job);

  /**
   * Launch job.
   *
   * @param UltimateCronJob $job
   *   The job to launch.
   *
   * @return bool
   *   TRUE on successful launch.
   */
  public abstract function launch($job);

  /**
   * Fallback implementation of multiple lock check.
   *
   * Each launcher should implement an optimized version of this method
   * if possible.
   *
   * @param array $jobs
   *   Array of UltimateCronJob to check.
   *
   * @return array
   *   Array of lock ids, keyed by job name.
   */
  public function isLockedMultiple($jobs) {
    $lock_ids = array();
    foreach ($jobs as $name => $job) {
      $lock_ids[$name] = $this
        ->isLocked($job);
    }
    return $lock_ids;
  }

  /**
   * Run the job.
   *
   * @param UltimateCronJob $job
   *   The job to run.
   */
  public function run($job) {

    // Prevent session information from being saved while cron is running.
    $original_session_saving = drupal_save_session();
    drupal_save_session(FALSE);

    // Force the current user to anonymous to ensure consistent permissions on
    // cron runs.
    $original_user = $GLOBALS['user'];
    $GLOBALS['user'] = drupal_anonymous_user();
    $php_self = NULL;
    try {

      // Signal to whomever might be listening, that we're cron!
      // @investigate Is this safe? (He asked knowingly ...)
      $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : '';
      $_SERVER['PHP_SELF'] = 'cron.php';
      $job
        ->invoke();

      // Restore state.
      $_SERVER['PHP_SELF'] = $php_self;
    } catch (Throwable $e) {

      // Restore state.
      if (isset($php_self)) {
        $_SERVER['PHP_SELF'] = $php_self;
      }
      ultimate_cron_watchdog_throwable('ultimate_cron', $e, 'Error running @name: @error', array(
        '@name' => $job->name,
        '@error' => (string) $e,
      ), WATCHDOG_ERROR);
    } catch (Exception $e) {

      // Restore state.
      if (isset($php_self)) {
        $_SERVER['PHP_SELF'] = $php_self;
      }
      watchdog_exception('ultimate_cron', $e, 'Error running @name: @error', array(
        '@name' => $job->name,
        '@error' => (string) $e,
      ), WATCHDOG_ERROR);
    }

    // Restore the user.
    $GLOBALS['user'] = $original_user;
    drupal_save_session($original_session_saving);
  }

  /**
   * Default implementation of jobs launcher.
   *
   * @param array $jobs
   *   Array of UltimateCronJob to launch.
   */
  public function launchJobs($jobs) {
    foreach ($jobs as $job) {
      if ($job
        ->isScheduled()) {
        $job
          ->launch();
      }
    }
  }

  /**
   * Format running state.
   */
  public function formatRunning($job) {
    $file = drupal_get_path('module', 'ultimate_cron') . '/icons/hourglass.png';
    $status = theme('image', array(
      'path' => $file,
    ));
    $title = t('running');
    return array(
      $status,
      $title,
    );
  }

  /**
   * Format unfinished state.
   */
  public function formatUnfinished($job) {
    $file = drupal_get_path('module', 'ultimate_cron') . '/icons/lock_open.png';
    $status = theme('image', array(
      'path' => $file,
    ));
    $title = t('unfinished but not locked?');
    return array(
      $status,
      $title,
    );
  }

  /**
   * Default implementation of formatProgress().
   *
   * @param UltimateCronJob $job
   *   Job to format progress for.
   *
   * @return string
   *   Formatted progress.
   */
  public function formatProgress($job, $progress) {
    $progress = $progress ? sprintf("(%d%%)", round($progress * 100)) : '';
    return $progress;
  }

  /**
   * Default implementation of initializeProgress().
   *
   * @param UltimateCronJob $job
   *   Job to initialize progress for.
   */
  public function initializeProgress($job) {
    $class = _ultimate_cron_get_class('progress');
    return $class::factory($job->name)
      ->setProgress(FALSE);
  }

  /**
   * Default implementation of finishProgress().
   *
   * @param UltimateCronJob $job
   *   Job to finish progress for.
   */
  public function finishProgress($job) {
    $class = _ultimate_cron_get_class('progress');
    return $class::factory($job->name)
      ->setProgress(FALSE);
  }

  /**
   * Default implementation of getProgress().
   *
   * @param UltimateCronJob $job
   *   Job to get progress for.
   *
   * @return float
   *   Progress for the job.
   */
  public function getProgress($job) {
    $class = _ultimate_cron_get_class('progress');
    return $class::factory($job->name)
      ->getProgress();
  }

  /**
   * Default implementation of getProgressMultiple().
   *
   * @param UltimateCronJob $jobs
   *   Jobs to get progresses for, keyed by job name.
   *
   * @return array
   *   Progresses, keyed by job name.
   */
  public function getProgressMultiple($jobs) {
    $class = _ultimate_cron_get_class('progress');
    return $class::getProgressMultiple(array_keys($jobs));
  }

  /**
   * Default implementation of setProgress().
   *
   * @param UltimateCronJob $job
   *   Job to set progress for.
   * @param float $progress
   *   Progress (0-1).
   */
  public function setProgress($job, $progress) {
    $class = _ultimate_cron_get_class('progress');
    return $class::factory($job->name)
      ->setProgress($progress);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UltimateCronLauncher::defaultSettings public function Default settings. Overrides UltimateCronPlugin::defaultSettings 2
UltimateCronLauncher::finishProgress public function Default implementation of finishProgress(). 1
UltimateCronLauncher::formatProgress public function Default implementation of formatProgress().
UltimateCronLauncher::formatRunning public function Format running state. 1
UltimateCronLauncher::formatUnfinished public function Format unfinished state.
UltimateCronLauncher::getProgress public function Default implementation of getProgress(). 1
UltimateCronLauncher::getProgressMultiple public function Default implementation of getProgressMultiple(). 1
UltimateCronLauncher::initializeProgress public function Default implementation of initializeProgress(). 1
UltimateCronLauncher::isLocked abstract public function Check if a job is locked. 2
UltimateCronLauncher::isLockedMultiple public function Fallback implementation of multiple lock check. 2
UltimateCronLauncher::launch abstract public function Launch job. 2
UltimateCronLauncher::launchJobs public function Default implementation of jobs launcher. 2
UltimateCronLauncher::lock abstract public function Lock job. 2
UltimateCronLauncher::run public function Run the job.
UltimateCronLauncher::setProgress public function Default implementation of setProgress(). 1
UltimateCronLauncher::unlock abstract public function Unlock a lock. 2
UltimateCronPlugin::$description public property
UltimateCronPlugin::$globalOptions public static property
UltimateCronPlugin::$instances public static property
UltimateCronPlugin::$multiple public static property 1
UltimateCronPlugin::$name public property
UltimateCronPlugin::$plugin public property
UltimateCronPlugin::$settings public property
UltimateCronPlugin::$title public property
UltimateCronPlugin::$weight public property 1
UltimateCronPlugin::build_operations_alter public function Allow plugins to alter the allowed operations for a job. 2
UltimateCronPlugin::cleanForm public function Clean form of empty fallback values.
UltimateCronPlugin::cronapi public function A hook_cronapi() for plugins. 1
UltimateCronPlugin::cron_alter public function A hook_cron_alter() for plugins. 3
UltimateCronPlugin::cron_post_invoke public function A hook_cron_post_invoke() for plugins.
UltimateCronPlugin::cron_post_launch public function A hook_cron_post_launch() for plugins.
UltimateCronPlugin::cron_post_run public function A hook_cron_post_run() for plugins.
UltimateCronPlugin::cron_post_schedule public function A hook_cron_post_schedule() for plugins. 1
UltimateCronPlugin::cron_pre_invoke public function A hook_cron_pre_invoke() for plugins.
UltimateCronPlugin::cron_pre_launch public function A hook_cron_pre_launch() for plugins.
UltimateCronPlugin::cron_pre_run public function A hook_cron_pre_run() for plugins.
UltimateCronPlugin::cron_pre_schedule public function A hook_cron_pre_schedule() for plugins. 2
UltimateCronPlugin::defaultSettingsForm public static function Default settings form. 1
UltimateCronPlugin::drupal_array_remove_nested_value public function Modified version drupal_array_get_nested_value().
UltimateCronPlugin::factory public static function Singleton factoryLogEntry.
UltimateCronPlugin::fallbackalize public function Process fallback form parameters.
UltimateCronPlugin::formatLabel public function Format label for the plugin. 1
UltimateCronPlugin::formatLabelVerbose public function Format verbose label for the plugin. 1
UltimateCronPlugin::getDefaultSettings public function Get default settings. 1
UltimateCronPlugin::getGlobalOption public static function Get global plugin option.
UltimateCronPlugin::getGlobalOptions public static function Get all global plugin options.
UltimateCronPlugin::hook_cron_alter final public static function Invoke hook_cron_alter() on plugins.
UltimateCronPlugin::hook_cron_post_invoke final public static function Invoke hook_cron_post_invoke() on plugins.
UltimateCronPlugin::hook_cron_post_launch final public static function Invoke hook_cron_post_launch() on plugins.
UltimateCronPlugin::hook_cron_post_run final public static function Invoke hook_cron_post_run() on plugins.
UltimateCronPlugin::hook_cron_post_schedule final public static function Invoke hook_cron_post_schedule() on plugins.
UltimateCronPlugin::hook_cron_pre_invoke final public static function Invoke hook_cron_pre_invoke() on plugins.
UltimateCronPlugin::hook_cron_pre_launch final public static function Invoke hook_cron_pre_launch() on plugins.
UltimateCronPlugin::hook_cron_pre_run final public static function Invoke hook_cron_pre_run() on plugins.
UltimateCronPlugin::hook_cron_pre_schedule final public static function Invoke hook_cron_pre_schedule() on plugins.
UltimateCronPlugin::isValid public function Default plugin valid for all jobs. 2
UltimateCronPlugin::jobSettingsForm public static function Job settings form. 1
UltimateCronPlugin::jobSettingsFormSubmit public static function Job settings form submit handler. 1
UltimateCronPlugin::jobSettingsFormValidate public static function Job settings form validate handler. 1
UltimateCronPlugin::setGlobalOption public static function Set global plugin option.
UltimateCronPlugin::setSettings public function Save settings to db.
UltimateCronPlugin::settingsForm public function Settings form. 8
UltimateCronPlugin::settingsFormSubmit public function Settings form submit handler. 3
UltimateCronPlugin::settingsFormValidate public function Settings form validate handler. 1
UltimateCronPlugin::settingsLabel public function Get label for a specific setting. 3
UltimateCronPlugin::signal public function Signal page for plugins. 2
UltimateCronPlugin::unsetGlobalOption public static function Remove a global plugin option.
UltimateCronPlugin::unsetGlobalOptions public static function Remove all global plugin options.
UltimateCronPlugin::__construct public function Constructor. 1