title: Breaking out of a Gearman loop
body: <p>I have a php application that gets requests for part numbers from our server. At that moment, we reach out to a third party API to gather pricing information to make sure we have the latest pricing for that particular request. Sometimes the third party API is slow or it might be down, so we have a database that stores the latest pricing requests for each particular part number that we can use as a fallback. I'd like to run the request to the third party API and the database in parallel using Gearman. Here is the idea:</p>
  
  <ol>
  <li>Receive request</li>
  <li>Through gearman, create two jobs:
  <ul>
  <li>Request to third party API</li>
  <li>MySQL database lookup</li>
  </ul></li>
  <li>Wait in a loop and return the results based on the following conditions:
  <ul>
  <li>If the third party API has completed return that result, return that result immediately</li>
  <li>If an elapsed time has passed, (e.g. 2 seconds) and the third party API hasn't responded, return the MySQL lookup data</li>
  </ul></li>
  </ol>
  
  <p>Using gearman, my thoughts were to either run the two tasks in the foreground and break out of runTasks() within the setCompleteCallback() call, or to run them in the background and check in on the two tasks within a separate loop and check in on the tasks using jobStatus(). </p>
  
  <p>Unfortunately, I can't get either route to work for me while still getting access to the resulting data. Is there a a better way, or are there some existing examples of how someone has made this work?</p>