You are here

function background_process_service_execute in Background Process 6

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

Execute the service

Parameters

$handle: Handle of process

$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 532

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);
  db_query("UPDATE {background_process} SET start_stamp = '%s', exec_status = %d WHERE handle = '%s' AND exec_status IN (%d, %d)", sprintf("%.06f", $process->start_stamp), BACKGROUND_PROCESS_STATUS_RUNNING, $handle, BACKGROUND_PROCESS_STATUS_LOCKED, BACKGROUND_PROCESS_STATUS_QUEUED);
  $claimed = db_affected_rows();
  if ($claimed) {
    $process->exec_status = BACKGROUND_PROCESS_STATUS_RUNNING;
    $process = BackgroundProcess::load($process);
    background_process_current_handle($handle);
  }
  else {
    if ($return) {
      return;
    }
    else {
      exit;
    }
  }

  // Make sure the process is removed when we're done
  if (!$return) {
    register_shutdown_function('background_process_remove_process', $process->handle, $process->start_stamp);
  }
  if (is_callable($process->callback)) {
    try {
      if (!$return) {
        register_shutdown_function('module_invoke_all', 'background_process_shutdown', $process);
      }
      $callback = _background_process_callback_name($process->callback);
      progress_initialize_progress($handle, "Background process '{$callback}' initialized");
      call_user_func_array($process->callback, $process->args);
      progress_end_progress($handle, "Background process '{$callback}' finished");
      if ($return) {
        background_process_remove_process($process->handle, $process->start_stamp);
        module_invoke_all('background_process_shutdown', $process);
      }
    } catch (Exception $e) {

      // Exception occurred, inform shutdown handlers.
      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;
  }
}