You are here

function background_process_service_execute in Background Process 7

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

Execute the service.

Parameters

string $handle: Handle of process

bool $return: Whether or not the function should return or exit.

2 calls to background_process_service_execute()
background_process_service_start in ./background_process.module
Call the function requested by the service call.
_background_process_queue in ./background_process.module
Worker callback for processing queued function call.

File

./background_process.module, line 526

Code

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

  // @todo Add static caching? We've already loaded this previously in the
  // access handler.
  $process = background_process_get_process($handle);
  if (!$process) {
    watchdog('bg_process', 'Process not found for handle: %handle', array(
      '%handle' => $handle,
    ), WATCHDOG_ERROR);
    if ($return) {
      return;
    }
    else {
      exit;
    }
  }
  $process->start_stamp = microtime(TRUE);
  try {
    $old_db = db_set_active('background_process');
    $claimed = db_update('background_process')
      ->fields(array(
      'start_stamp' => sprintf("%.06f", $process->start_stamp),
      'exec_status' => BACKGROUND_PROCESS_STATUS_RUNNING,
    ))
      ->condition('handle', $handle)
      ->condition('exec_status', array(
      BACKGROUND_PROCESS_STATUS_LOCKED,
      BACKGROUND_PROCESS_STATUS_QUEUED,
    ), 'IN')
      ->execute();
    db_set_active($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) {
    db_set_active($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 = db_set_active('background_process');
      progress_initialize_progress($handle, "Background process '{$callback}' initialized");
      db_set_active($old_db);
      call_user_func_array($process->callback, $process->args);
      $old_db = db_set_active('background_process');
      progress_end_progress($handle, "Background process '{$callback}' finished");
      db_set_active($old_db);
      if ($return) {
        background_process_remove_process($process->handle, $process->start_stamp);
        module_invoke_all('background_process_shutdown', $process);
      }
    } catch (Exception $e) {

      // Exception occurred, switch back to proper db if necessary
      // and inform shutdown handlers.
      if ($old_db) {
        db_set_active($old_db);
      }
      if (!$return) {
        module_invoke_all('background_process_shutdown', $process, (string) $e);
      }
      throw $e;
    }
  }
  else {

    // Function not found.
    watchdog('bg_process', 'Callback: %callback not found', array(
      '%callback' => $process->callback,
    ), WATCHDOG_ERROR);
  }
  if ($return) {
    return;
  }
  else {
    exit;
  }
}