You are here

function background_process_service_execute in Background Process 8

Same name and namespace in other branches
  1. 6 background_process.module \background_process_service_execute()
  2. 7 background_process.module \background_process_service_execute()

Implements to Execute the service.

3 calls to background_process_service_execute()
background_process_service_start in ./background_process.module
Implements to Call the function requested by the service call.
DefaultController::backgroundProcessServiceStart in src/Controller/DefaultController.php
Implements Background Process Service Start.
_background_process_queue in ./background_process.module
Worker callback for processing queued function call.

File

./background_process.module, line 409
This module implements a framework for calling funtions in the background.

Code

function background_process_service_execute($handle, $return = FALSE) {

  // Access handler.
  $process = background_process_get_process($handle);
  if (!$process) {
    \Drupal::logger('bg_process')
      ->error('Process not found for handle: %handle', [
      '%handle' => $handle,
    ]);
    if ($return) {
      return;
    }
    else {
      exit;
    }
  }
  $process->start_stamp = microtime(TRUE);
  try {
    $old_db = Database::setActiveConnection('background_process');
    $claimed = db_update('background_process')
      ->fields([
      'start_stamp' => sprintf("%.06f", $process->start_stamp),
      'exec_status' => BACKGROUND_PROCESS_STATUS_RUNNING,
    ])
      ->condition('handle', $handle)
      ->condition('exec_status', [
      BACKGROUND_PROCESS_STATUS_LOCKED,
      BACKGROUND_PROCESS_STATUS_QUEUED,
    ], 'IN')
      ->execute();
    Database::setActiveConnection($old_db);
    if ($claimed) {
      $process->exec_status = BACKGROUND_PROCESS_STATUS_RUNNING;
      $process = BackgroundProcess::load($process);
      $process
        ->sendMessage('claimed');
      background_process_current_handle($handle);
    }
    else {
      if ($return) {
        return;
      }
      else {
        exit;
      }
    }
  } catch (Exception $e) {
    Database::setActiveConnection($old_db);
    throw $e;
  }

  // Make sure the process is removed when we're done.
  if (!$return) {
    drupal_register_shutdown_function('background_process_remove_process', $process->handle, $process->start_stamp);
  }
  if (is_callable($process->callback)) {
    $old_db = NULL;
    try {
      if (!$return) {
        drupal_register_shutdown_function('module_invoke_all', 'background_process_shutdown', $process);
      }
      $callback = _background_process_callback_name($process->callback);
      $old_db = Database::setActiveConnection('background_process');
      progress_initialize_progress($handle, "Background process '{$callback}' initialized");
      Database::setActiveConnection($old_db);
      call_user_func_array($process->callback, $process->args);
      $old_db = Database::setActiveConnection('background_process');
      progress_end_progress($handle, "Background process '{$callback}' finished");
      Database::setActiveConnection($old_db);
      if ($return) {
        background_process_remove_process($process->handle, $process->start_stamp);
        \Drupal::moduleHandler()
          ->invokeAll('background_process_shutdown', [
          $process,
        ]);
      }
    } catch (Exception $e) {

      // Exception occurred, switch back to proper db if necessary.
      if ($old_db) {
        Database::setActiveConnection($old_db);
      }
      if (!$return) {
        \Drupal::moduleHandler()
          ->invokeAll('background_process_shutdown', [
          $process,
          (string) $e,
        ]);
      }
      throw $e;
    }
  }
  else {

    // Function not found.
    \Drupal::logger('bg_process')
      ->error('Callback: %callback not found', [
      '%callback' => $process->callback,
    ]);
  }
  if ($return) {
    return;
  }
  else {
    exit;
  }
}