You are here

public static function BackgroundProcess::waitForFinish in Background Process 7.2

Finish the request by storing result and invoking callback finish?

File

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

Class

BackgroundProcess
@file

Code

public static function waitForFinish($processes, $options = array()) {
  if (empty($processes)) {
    return 0;
  }
  $keyed = array();
  foreach ($processes as $process) {
    $keyed[$process->pid] = $process;
  }
  $options += array(
    'interval' => 1,
    'timeout' => 10,
  );
  $interval = $options['interval'] * 1000000;
  $expire = microtime(TRUE) + $options['timeout'];
  do {
    $results = db_select('background_process_result', 'r', array(
      'target' => 'background_process',
    ))
      ->fields('r')
      ->condition('r.pid', array_keys($keyed), 'IN')
      ->execute()
      ->fetchAll(PDO::FETCH_OBJ);
    usleep($interval);
  } while (microtime(TRUE) < $expire && count($results) < count($processes));
  foreach ($results as $result) {
    $keyed[$result->pid]->result = $result->result;
  }
  return count($results);
}