You are here

function computing_claim in Drupal Computing 7.2

Return an object and mark it as being processed. Use lock system to make sure status is not changed during the process. Another approach is to use while(TRUE) following the logic from SystemQueue:claimItem().

Parameters

$app_name string: the name of the application to claim an record on.:

$record_id int: an optional record ID to specify whether to claim a specific record or any record of an application.:

Return value

object: one computing record from the application in "RDY" status, or FALSE if not found.

1 call to computing_claim()
ComputingQueue::claimItem in ./computing.queue.inc
Claim an item in the queue for processing.
1 string reference to 'computing_claim'
computing_services_resources in ./computing.services.inc
Implements hook_services_resources().

File

./computing.module, line 340

Code

function computing_claim($app_name, $record_id = NULL) {
  while (TRUE) {
    if (lock_acquire('computing_claim', 10)) {
      $query = db_select('computing_record')
        ->fields('computing_record', array(
        'id',
      ))
        ->condition('application', $app_name)
        ->condition('status', 'RDY')
        ->orderBy('weight')
        ->orderBy('created')
        ->range(0, 1);
      if ($record_id) {
        $query
          ->condition('id', $record_id);
      }
      $candidate_id = $query
        ->execute()
        ->fetchField();
      if ($candidate_id) {
        computing_update_field($candidate_id, 'status', 'RUN');
        $return = computing_load($candidate_id);
        rules_invoke_event('computing_event_claimed', $return);
      }
      else {
        $return = FALSE;
      }

      // always release lock after operations.
      lock_release('computing_claim');
      return $return;
    }
    else {

      // if lock_acquire fails, wait a few seconds and try again.
      lock_wait('computing_claim', 5);
    }
  }

  // end of while(TRUE)
}