title: Laravel 504 gateway timeout
body: <p>I have a script in Laravel platform which process thousands of records , and <strong>it's timing out issue</strong>. I want to avoid update settings on server like php.ini file.</p>
  
  <p>Is there any way to chunk all records in small pieces and use them in further processing until all records will be used.</p>
  
  <p>I have thousands of records which are getting <strong>Price</strong> from another server.</p>
  
  <p>So,</p>
  
  <p>1-10 records works fine.</p>
  
  <p>1-100 records works fine.</p>
  
  <p>More than 500 records script stuck and gives error like below.</p>
  
  <blockquote>
    <p><strong>405 nginx server not allowed</strong></p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/H3KDh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H3KDh.png" alt="enter image description here"></a></p>
  
  <p>My Route is like :-  example.com/updateAllKeywords</p>
  
  <p>Below code is getting keyword to fetch keywords'price from eBay API</p>
  
  <pre><code>        $keywordCollection = Keywords::all();
  
          Keywords::chunk(100, function ($keywordCollection) use($request,$keywordCollection,$defaultStartDate,$ignoredItems,$response) {
              foreach ($keywordCollection-&gt;toArray() as $keyword_result) {
                  $response = $this-&gt;apirequest($request, $keyword_result['id'], $keyword_result, $defaultStartDate, $ignoredItems);
              }
          });
  </code></pre>
  
  <p>This is api request function:</p>
  
  <pre><code> /**
   * Call Finding API for Active List items 
   *
   * @return API Response
   */
  public function apirequest(Request $request, $id, $keyword_result, $defaultStartDate, $ignoredItems) {
  
      try {
  
          /* Request */
          $request = new Types\FindItemsAdvancedRequest();
  
          /* 1) default 100 items being displayed without pagination */
          $request-&gt;paginationInput = new Types\PaginationInput();
          $request-&gt;paginationInput-&gt;entriesPerPage = Config::get('constants.paginationoptions.entries_per_page');
  
          /* 2) ItemFilter for three conditions NEW ,NEWOTHER,USED */
          $itemFilterForConditions = new Types\ItemFilter();
          $itemFilterForConditions-&gt;name = 'Condition';
          $itemFilterForConditions-&gt;value = ['1000', '1500', '3000'];
  
          /* 3) ItemFilter for hide duplicate items */
          $itemFilterduplicate = new Types\ItemFilter();
          $itemFilterduplicate-&gt;name = 'HideDuplicateItems';
          $itemFilterduplicate-&gt;value[] = 'true';
  
          /* 4) pull items before given date / days */
          $itemFilterStartDateFrom = new Types\ItemFilter();
          $itemFilterStartDateFrom-&gt;name = 'StartTimeFrom';
          $itemFilterStartDateFrom-&gt;value[] = date('Y-m-d\TH:i:s\Z', mktime(0, 0, 0, date('m'), date('d') - $defaultStartDate['defaultStartDate'], date('y')));
  
          /* Exclude Keywords/MPN by set Priced Percentage */
          if (!empty($keyword_result['price']) &amp;&amp; ($defaultStartDate['exclusionPercentage'] &gt; 0)) {
              $percentageAmount = $keyword_result['price'] * ($defaultStartDate['exclusionPercentage'] / 100);
              $percentageAmount = floor($percentageAmount).substr($percentageAmount-floor($percentageAmount),1,2+1);
              $itemFilterPriced = new Types\ItemFilter();
              $itemFilterPriced-&gt;name = 'MinPrice';
              $itemFilterPriced-&gt;value[] = $percentageAmount;
          }
  
          $itemFilter = (isset($itemFilterPriced)) ? [$itemFilterForConditions, $itemFilterduplicate, $itemFilterStartDateFrom, $itemFilterPriced] : [$itemFilterForConditions, $itemFilterduplicate, $itemFilterStartDateFrom];
  
          $request-&gt;itemFilter = $itemFilter;
  
          /* 5) Request KEYWORD/MPN to filter Items Active Lists */
          $searchKeyword = (!empty($keyword_result['exclusions'])) ? $keyword_result['keyword'] . " -" . "(" . trim($keyword_result['exclusions']) . ")" : $keyword_result['keyword'];
          $request-&gt;keywords = $searchKeyword;
  
          /* 6) Request For sorting Items by Lowest To Highest Itemprice Price + Shipping Price */
          $request-&gt;sortOrder = 'PricePlusShippingLowest';
  
          /* 7) Request For Keyword Search Including the Item Descriptions,Title and Subtitle */
          $request-&gt;descriptionSearch = ($defaultStartDate['advancedExclusions'] == 1) ? true : false;
  
          $service = $this-&gt;index();
  
          /* Response returned by $request */
          $response = $this-&gt;apiresponse($request, $service, $id, $keyword_result['keyword'], $ignoredItems);
  
          return $response;
  
      } catch (\Exception $e) {
          echo json_encode(array('ack' =&gt; 'Failure', 'msg' =&gt; $e-&gt;getMessage()));
          exit;
      }
  }
  
  
  public function apiresponse($request, $service, $id, $keyword, $ignoredItems) {
  
              try {
  
                  $response = $service-&gt;findItemsAdvanced($request);
  
                  /* fetch all ignored Items */
  
                  $newItems = array();
                  $newOtherItems = array();
                  $usedItems = array();
                  $errors = "";
  
                  if ($response-&gt;ack == "Success") {
                      $results = $response-&gt;searchResult-&gt;item;
  
                      ob_start();
  
                      foreach ($results as $item) {
  
                          if ($item-&gt;listingInfo-&gt;buyItNowAvailable == '1') {
                              $itemPrice = $item-&gt;listingInfo-&gt;buyItNowPrice-&gt;value;
                          } elseif (($item-&gt;listingInfo-&gt;listingType == 'Auction') &amp;&amp; ($item-&gt;sellingStatus-&gt;currentPrice-&gt;value == 0)) {
                              $itemResponse = $this-&gt;getItemDetails($item-&gt;itemId);
                              $itemPrice = isset($itemResponse) ? $itemResponse : 0;
                          } else {
                              $itemPrice = $item-&gt;sellingStatus-&gt;currentPrice-&gt;value;
                          }
  
                          /* check for ignored items */
                          if (!in_array($item-&gt;itemId, $ignoredItems)) {
  
                              /* check for NEW Condition (conditionId = 1000) */
                              if ($item-&gt;condition-&gt;conditionId == '1000') {
                                  $newItems[] = array(
                                      'keywordId' =&gt; $id,
                                      'conditionId' =&gt; (string) $item-&gt;condition-&gt;conditionId,
                                      'itemId' =&gt; $item-&gt;itemId,
                                      'title' =&gt; $item-&gt;title,
                                      'price' =&gt; $itemPrice,
                                      'itemURL' =&gt; $item-&gt;viewItemURL,
                                      'galleryURL' =&gt; isset($item-&gt;galleryURL) ? $item-&gt;galleryURL : '',
                                  );
                              }
                              /* check for NEW OTHER Condition (conditionId = 1500) */ elseif ($item-&gt;condition-&gt;conditionId == '1500') {
                                  $newOtherItems[] = array(
                                      'keywordId' =&gt; $id,
                                      'conditionId' =&gt; (string) $item-&gt;condition-&gt;conditionId,
                                      'itemId' =&gt; $item-&gt;itemId,
                                      'title' =&gt; $item-&gt;title,
                                      'price' =&gt; $itemPrice,
                                      'itemURL' =&gt; $item-&gt;viewItemURL,
                                      'galleryURL' =&gt; isset($item-&gt;galleryURL) ? $item-&gt;galleryURL : '',
                                  );
                              }
                              /* check for USED Condition (conditionId = 3000) */ elseif ($item-&gt;condition-&gt;conditionId == '3000') {
                                  $usedItems[] = array(
                                      'keywordId' =&gt; $id,
                                      'conditionId' =&gt; (string) $item-&gt;condition-&gt;conditionId,
                                      'itemId' =&gt; $item-&gt;itemId,
                                      'title' =&gt; $item-&gt;title,
                                      'price' =&gt; $itemPrice,
                                      'itemURL' =&gt; $item-&gt;viewItemURL,
                                      'galleryURL' =&gt; isset($item-&gt;galleryURL) ? $item-&gt;galleryURL : '',
                                  );
                              }
                          }
  
                          ob_flush();
                      }
  
                      $newItems = array_slice($newItems, 0, 20);
                      $newOtherItems = array_slice($newOtherItems, 0, 20);
                      $usedItems = array_slice($usedItems, 0, 20);
  
                      $insertItems = array_merge($newItems, $newOtherItems, $usedItems);
  
                      /* clean up items */
                      Items::where('keywordId', $id)-&gt;delete();
  
                      /* save pulled items */
                      $items = Items::insert($insertItems);
                  } elseif ($response-&gt;ack == "Failure") {
                      foreach ($response-&gt;errorMessage-&gt;error as $error) {
                          $errors .= "%s: %s\n\n" . $error-&gt;severity === Enums\ErrorSeverity::C_ERROR ? '&lt;b&gt;Error&lt;/b&gt;&amp;nbsp;' : '&lt;b&gt;Warning&lt;/b&gt;&amp;nbsp;' . $error-&gt;message;
                      }
                  }
  
                  /* save API log fot the keyword */
                  $this-&gt;setapilog($request, $response, $keyword);
  
                  if (!empty($errors)) {
                      echo json_encode(array('ack' =&gt; 'Failure', 'msg' =&gt; $errors));
                      exit;
                  } else {
                      return true;
                  }
              } catch (\Exception $e) {
                  echo json_encode(array('ack' =&gt; 'Failure', 'msg' =&gt; $e-&gt;getMessage()));
                  exit;
              }
          }
  </code></pre>
  
  <p>Any information on this would be greatly appreciated. Thanks!</p>