You are here

public function BackgroundProcess::execute in Background Process 7.2

Same name and namespace in other branches
  1. 8 background_process.class.php \BackgroundProcess::execute()
  2. 6 BackgroundProcess.class.php \BackgroundProcess::execute()
  3. 7 BackgroundProcess.class.php \BackgroundProcess::execute()

Execute the background process callback function.

File

./background_process.inc, line 513
External API short overview

Class

BackgroundProcess
@file

Code

public function execute() {
  $this
    ->logDebug(__FUNCTION__);
  if (!$this
    ->claim()) {
    watchdog('bg_process', 'Could not claim process %pid : %handle', array(
      '%pid' => $this->pid,
      '%handle' => $this->handle,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  $this
    ->ensureCleanup();
  if ($this->options['detach']) {
    $this
      ->flush();
  }
  if (!empty($this->options['include'])) {
    foreach ($this->options['include'] as $file) {
      include_once $file;
    }
  }
  if (is_callable($this->callback)) {
    $this
      ->logDebug($this->callback . ' is callable');

    // Run indefinitly...
    // @todo Make timeout configurable, perhaps through options?
    set_time_limit(0);

    // Run process as specified user
    $old_user = NULL;
    global $user;
    $old_user = $user;
    if ($this->uid && ($as_user = user_load($this->uid))) {
      $user = $as_user;
    }
    else {
      $user = drupal_anonymous_user();
    }

    // Set current process
    $old_process = self::currentProcess($this);
    try {
      $this
        ->logDebug($this->callback . ' is called now!');

      // Just to avoid endless loops on keepalive, check if arguments really is an array
      if (!is_array($this->arguments)) {
        $this
          ->keepAlive(FALSE);
        throw new BackgroundProcessException(t('Background Process arguments is not an array!'), BACKGROUND_PROCESS_ERROR_ARGUMENTS);
      }
      module_invoke_all('background_process_pre_execute', $this);
      $this->result = call_user_func_array($this->callback, $this->arguments);
      module_invoke_all('background_process_post_execute', $this);
      if ($this->options['store_result']) {
        db_insert('background_process_result', array(
          'target' => 'background_process',
        ))
          ->fields(array(
          'pid' => $this->pid,
          'result' => $this->result,
          'created' => time(),
        ))
          ->execute();
      }
      $this
        ->shutdown();
      self::currentProcess($old_process);

      // Done, restore user.
      if ($old_user) {
        $user = $old_user;
      }
      return $this;
    } catch (Exception $e) {

      // Something went wrong. Restore user and rethrow exception.
      if ($old_user) {
        $user = $old_user;
      }
      self::currentProcess($old_process);
      $this
        ->logDebug('exception thrown! ' . (string) $e);
      throw $e;
    }
  }
  else {

    // Throw exception?
    $this
      ->ensureProcess();
    $this
      ->keepAlive(FALSE)
      ->writeData();
    throw new BackgroundProcessException(t('Callback: %callback not found', array(
      '%callback' => _background_process_callback_name($process->callback),
    )), BACKGROUND_PROCESS_ERROR_INVALID_CALLBACK);
  }
}