Skip to content

Commit

Permalink
added retry on rate limit option
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Hepler committed Feb 23, 2017
1 parent 8740e45 commit 432e1ca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/OldTimeGuitarGuy/MechanicalTurk/Contracts/Requester.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function make($operation, array $parameters = []);
*/
public function submit($operation, array $parameters = []);

/**
* Set whether or not it should retry
* if it encounters a rate limit error
*
* @param mixed $shouldRetry
*/
public function setRetryOnRateLimit($shouldRetry = false);

/**
* Dynamically submit an operation
*
Expand Down
25 changes: 23 additions & 2 deletions src/OldTimeGuitarGuy/MechanicalTurk/Operations/Base/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OldTimeGuitarGuy\MechanicalTurk\Operations\Base;

use OldTimeGuitarGuy\MechanicalTurk\Contracts\Http\Request;
use OldTimeGuitarGuy\MechanicalTurk\Exceptions\MechanicalTurkRequestException;
use OldTimeGuitarGuy\MechanicalTurk\Exceptions\MechanicalTurkOperationException;

abstract class Operation
Expand All @@ -21,22 +22,32 @@ abstract class Operation
*/
protected $parameters;

/**
* Determine if it should retry
* after receiving a rate limit error.
*
* @var boolean
*/
protected $retryOnRateLimit;

/**
* Create new instance of CreateHIT
*
* @param Request $request
* @param array $parameters
* @param boolean $retryOnRateLimit
*
* @throws \OldTimeGuitarGuy\MechanicalTurk\Exceptions\MechanicalTurkOperationException
*/
public function __construct(Request $request, array $parameters)
public function __construct(Request $request, array $parameters, $retryOnRateLimit = false)
{
if (! $this->satisfiesRequirements($parameters)) {
throw new MechanicalTurkOperationException($this->operation());
}

$this->request = $request;
$this->parameters = $parameters;
$this->retryOnRateLimit = $retryOnRateLimit;
}

//////////////////////
Expand All @@ -60,10 +71,20 @@ abstract protected function satisfiesRequirements(array $parameters);
* Submit the request to the Mechanical Turk Requester API
*
* @return \OldTimeGuitarGuy\MechanicalTurk\Contracts\Http\Response
* @throws \OldTimeGuitarGuy\MechanicalTurk\Exceptions\MechanicalTurkRequestException
*/
public function submit()
{
return $this->request->post($this->operation(), $this->parameters);
try {
return $this->request->post($this->operation(), $this->parameters);
} catch (MechanicalTurkRequestException $e) {
if ($this->retryOnRateLimit && $e->response()->status() === 503) {
sleep(30);
return $this->request->post($this->operation(), $this->parameters);
}

throw $e;
}
}

///////////////////////
Expand Down
21 changes: 20 additions & 1 deletion src/OldTimeGuitarGuy/MechanicalTurk/Requester.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class Requester implements Contracts\Requester
*/
protected $request;

/**
* Determine if it should retry
* after receiving a rate limit error.
*
* @var boolean
*/
protected $retryOnRateLimit = false;

/**
* Create a new instance of Mechanical Turk Requester API Client.
*
Expand All @@ -66,7 +74,7 @@ public function make($operation, array $parameters = [])
throw new \BadMethodCallException("{$operation} is not a supported Mechanical Turk Requester operation.");
}

return new $this->operations[$operation]($this->request, $parameters);
return new $this->operations[$operation]($this->request, $parameters, $this->retryOnRateLimit);
}

/**
Expand All @@ -82,6 +90,17 @@ public function submit($operation, array $parameters = [])
return $this->make($operation, $parameters)->submit();
}

/**
* Set whether or not it should retry
* if it encounters a rate limit error
*
* @param boolean $shouldRetry
*/
public function setRetryOnRateLimit($shouldRetry)
{
$this->retryOnRateLimit = $shouldRetry;
}

/**
* Dynamically submit an operation
*
Expand Down

0 comments on commit 432e1ca

Please sign in to comment.