Continuing an interrupted batch I'm using a batch job in Drupal 8 to scrape pages from another site to import. 

I'm using httpClient, and I have it wrapped in a try/catch. 

try {
  $response = \Drupal::httpClient()-&gt;get($parts['scheme'] . '://' . $parts['host'] . $parts['path'], [
    'query' =&gt; $parts['query'],
    'headers' =&gt; array('Accept'=&gt;'text/xml'),
    'verify' =&gt; FALSE,
    'connect_timeout' =&gt; 6,
    'CURLOPT_SSL_VERIFYPEER' =&gt; false,
    'CURLOPT_CERTINFO' =&gt; false,
  ]);
  return (string) $response-&gt;getBody();
}
catch (Exception $e) {
  watchdog_exception('scrape', $e);
  return FALSE;
}


However, from time to time the remote site times out, and the guzzle exception is not caught, which causes the batch job to end. 

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /batch?id=44&amp;op=do_nojs&amp;op=do
StatusText: Internal Server Error
ResponseText: The website encountered an unexpected error. Please try again later.GuzzleHttp\Exception\ConnectException: cURL error 28: Operation timed out after 30001 milliseconds with 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in GuzzleHttp\Handler\CurlFactory::createRejection() (line 186 of vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php). GuzzleHttp\Handler\CurlFactory::finishError(Object, Object, Object) (Line: 103)


This job imports a few thousand pages, so when the error happens in the middle of the batch, it's a major pain. 

I am getting ready to write a queue for the batch processor, where the batch pops a set of items off the queue and then marks them as done, so that I can continue the job after an exception. 

But then I wondered, is there already a way to tell Drupal to continue a batch from where it left off or was interrupted? I did some googling and didn't find anything indicating that this feature already existed. I don't want to re-invent the wheel if I don't have to. 

Can I re-start an incomplete, interrupted batch job?
So I think there is no way to tell batch to restart when it's gotten an error response. What you could do is to not return a possible error but rather re-try the request using your own wrapper. So could do 

try {

  $TestRetry = new TestRetry();
  return $TestRetry-&gt;test();
}
// And I think you won't need this but handle the exception in your wrapper class.
catch (Exception $e) {
  watchdog_exception('scrape', $e);
  return FALSE;
}


and in your wrapper, you could attempt to retry the request to avoid an error. So your wrapper may look like this:

&lt;?php

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

class TestRetry {

  public function test() {
    $handlerStack = HandlerStack::create(new CurlHandler());
    $handlerStack-&gt;push(Middleware::retry($this-&gt;retryDecider(), $this-&gt;retryDelay()));
    $client = new Client(array('handler' =&gt; $handlerStack));

    $response = $client-&gt;request(
      'GET',
// @todo replace to a real url!!!
      'https://500-error-code-url'
    )-&gt;getBody()-&gt;getContents();

    return \GuzzleHttp\json_decode($response, TRUE);
  }


  public function retryDecider() {
    return function (
      $retries,
      Request $request,
      Response $response = NULL,
      RequestException $exception = NULL
    ) {
// Limit the number of retries to 5
      if ($retries &gt;= 5) {
        return FALSE;
      }

// Retry connection exceptions
      if ($exception instanceof ConnectException) {
        return TRUE;
      }

      if ($response) {
// Retry on server errors
        if ($response-&gt;getStatusCode() &gt;= 500) {
          return TRUE;
        }
      }

      return FALSE;
    };
  }

  /**
   * delay 1s 2s 3s 4s 5s
   *
   * @return Closure
   */
  public function retryDelay() {
    return function ($numberOfRetries) {
      return 1000 * $numberOfRetries;
    };
  }
}