You are here

public static function BackgroundProcess::lock in Background Process 7.2

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

Create BackgroundProcess object and lock it.

Parameters

string $handle (optional): If specified, $handle will be used as key for the lock. Otherwise a unique handle will be generated.

Return value

BackgroundProcess object

1 call to BackgroundProcess::lock()
BackgroundProcess::start in ./background_process.inc

File

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

Class

BackgroundProcess
@file

Code

public static function lock($handle = NULL) {
  $process = new BackgroundProcess($handle);
  $process->token = uniqid('BGP', TRUE);
  try {
    $process->exec_status = BACKGROUND_PROCESS_STATUS_LOCKED;
    $process->created = $process->start_stamp = microtime(TRUE);
    $process->pid = db_insert('background_process', array(
      'target' => 'background_process',
    ))
      ->fields(array(
      'token' => $process->token,
      'handle' => $process->handle,
      'created' => $process->created,
      'start_stamp' => $process->start_stamp,
      'exec_status' => $process->exec_status,
    ))
      ->execute();
    $process
      ->sendMessage('locked');
    $process
      ->logDebug(__FUNCTION__);
    $process
      ->ensureCleanup(FALSE, TRUE);
    return $process;
  } catch (Exception $e) {
    if ($e
      ->getCode() == '23000') {
      throw new BackgroundProcessException(t('@handle already locked', array(
        '@handle' => $handle,
      )), BACKGROUND_PROCESS_ERROR_NO_LOCK);
    }
    throw $e;
  }
}