/** * An executed/executing instance of a choreography. * * @param Temboo_Session $session The session that owns this resource. * @param Temboo_Choreography $choreo The choregraphy object for the execution. * @param Temboo_Inputs|array $inputs (optional) Inputs as Temboo_Inputs or associative array. * @param bool $async Whether to execute in asynchronous mode. Default false. * @param bool $store_results Whether to store results of asynchronous execution. Default true. * @return Temboo_Choreography_Execution New execution. * @throws Temboo_Exception_Authentication if session authentication fails. * @throws Temboo_Exception_Execution if runtime errors occur in synchronous execution or execution fails to start. NOT thrown for post-launch errors in asynchronous execution -- check status or results to determine asynchronous success. * @throws Temboo_Exception_Notfound if choreography does not exist. */ public function __construct(Temboo_Session $session, Temboo_Choreography $choreo, $inputs = array(), $async = false, $store_results = true) { parent::__construct($session, $choreo->getUri()); if (!$inputs instanceof Temboo_Inputs) { $inputs = $choreo->newInputs($inputs); } if ($async) { $this->_async = true; $this->uri->setParameter('mode', 'async'); if ($store_results) { $this->uri->setParameter('store_results', 'true'); } else { $this->_store_results = false; } } $response = $this->session->post($this, (string) $inputs); /* * Both synchronous asynchronous execution return a Temboo_Execution object. * * For sync, we execute immediately, throw exceptions on any runtime errors, * and otherwise store the response in the execution object immediately. * * For async, we launch the remote execution and get back an ID to check for * running status and (eventually) retrieve the results. We throw exceptions * on transport or authentication errors, but cannot tell if a runtime error * will occur -- instead, check $execution->getStatus() and * $execution->getError() to handle runtime errors without an exception. * * Calling $execution->getResults() on a failed (or still running) async execution * will throw an exception. * */ if ($this->_async) { // Error checking and set ID for asynchronous execution... if ($response->getStatus() == '404') { throw new Temboo_Exception_Notfound('Choreography execution not found.', array('response' => $response)); } $response_object = $response->getObject(); if (!$response_object || !isset($response_object->id)) { if ($response_object && isset($response_object->error) && isset($response_object->error->message)) { $this->_error = $response_object->error->message; throw new Temboo_Exception_Execution('Choreography execution failed. ' . $this->_error, array('response' => $response)); } throw new Temboo_Exception_Execution('Choreography execution returned invalid result.', array('response' => $response)); } if ($response->error()) { throw new Temboo_Exception_Execution('Choreography execution failed.', array('response' => $response)); } $this->_id = $response_object->id; } else { // Error checking and immediate packaging of response for synchronous execution... if ($response->getStatus() == '404') { throw new Temboo_Exception_Notfound('Choreography not found.', array('response' => $response)); } $response_array = $response->getArray(); if (!$response_array || !isset($response_array['execution'])) { if ($response_array && isset($response_array['error']) && isset($response_array['error']['message'])) { $this->_error = $response_array['error']['message']; throw new Temboo_Exception_Execution('Choreography execution failed. ' . $this->_error, array('response' => $response)); } throw new Temboo_Exception_Execution('Choreography execution returned invalid result.', array('response' => $response)); } if ($response->error()) { if (isset($response_array['execution']['lasterror'])) { $this->_error = $response_array['execution']['lasterror']; throw new Temboo_Exception_Execution('Choreography execution failed. ' . $this->_error, array('response' => $response)); } throw new Temboo_Exception_Execution('Choreography execution failed.', array('response' => $response)); } if (isset($response_array['execution']['status'])) { $this->_status = $response_array['execution']['status']; } if (isset($response_array['execution']['lasterror'])) { $this->_error = $response_array['execution']['lasterror']; } if (isset($response_array['output'])) { $this->_results = $this->wrapResults($response_array['output']); } } }