/** * Test Workload retry strategy * * @expectedException \Brightzone\GremlinDriver\ServerException * * @return void */ public function testRetry() { $db = new Connection(['host' => 'localhost', 'port' => 8182, 'graph' => 'graph', 'retryAttempts' => 5]); $db->open(); $count = 0; $workload = new Workload(function (&$count) { $count++; throw new \Brightzone\GremlinDriver\ServerException("test error", 500); }, [&$count]); try { $response = $workload->linearRetry($db->retryAttempts); } catch (\Exception $e) { $this->assertEquals(5, $count, "incorrect number of attempts executed"); throw $e; } }
/** * Run a callback in a transaction. * The advantage of this is that it allows for fail-retry strategies * * @param callable $callback the code to execute within the scope of this transaction * @param array $params The params to pass to the callback * * @return mixed the return value of the provided callable */ public function transaction(callable $callback, $params = []) { //create a callback from the callback to introduce transaction handling $workload = new Workload(function (&$db, $callback, $params) { try { $db->transactionStart(); $result = call_user_func_array($callback, $params); if ($db->_inTransaction) { $db->transactionStop(TRUE); } return $result; } catch (\Exception $e) { /** * We need to catch the exception again before the workload catches it * this allows us to terminate the transaction properly before each retry attempt */ if ($db->_inTransaction) { $db->transactionStop(FALSE); } throw $e; } }, [&$this, $callback, $params]); $result = $workload->linearRetry($this->retryAttempts); return $result; }