title: MySQL server has gone away while running Laravel Queue
body: <p>I am trying to import data from CSV file to database using laravel queue. These CSV files are huge with around 500k number of rows.</p>
  
  <p>I have learned somewhere that using laravel queue we don't need to think about connection time out but this is not looking true. Maybe I was wrong.</p>
  
  <p>Please, check my job code if there is anything wrong in these methods. I am using "League\Csv" to read the CSV file.</p>
  
  <pre><code>public function __construct($data,$error_arr,$error_row_numbers) {  
         $this-&gt;data = $data;
         $this-&gt;error_arr = $error_arr;
         $this-&gt;error_row_numbers = $error_row_numbers;
      }
  
      /**
       * Execute the job.
       *
       * @return void
       */
      public function handle()
      {
          $offset = $this-&gt;data['offset']; 
          $limit = $this-&gt;data['limit'];
  
          $filename = $this-&gt;data['file_name'];
          $service = new Service();
          $table = 'committees';
          $dbase = new Committee();
  
          //map_data have array about which csv column
          //should be inserted in which column of database
          $map_data = $this-&gt;data['map_data'];
  
      //get all columns name of a table
          $db_header_obj = new Committee();
          $db_header = $db_header_obj-&gt;getTableColumns();
  
          $csv_file_path = storage_path('app/files/committee/').$filename;
          if (!ini_get("auto_detect_line_endings")) {
              ini_set("auto_detect_line_endings", TRUE);
          }
          $csv = Reader::createFromPath($csv_file_path, 'r');
  
          $csv-&gt;setOutputBOM(Reader::BOM_UTF8);
          $csv-&gt;addStreamFilter('convert.iconv.ISO-8859-15/UTF-8');
  
              $csv-&gt;setHeaderOffset(0); 
              $csv_header = $csv-&gt;getHeader();    
  
  
  
              $rec_arr = array();
              $records = array();
              $records_arr = array();
  
                      $stmt = (new Statement())
                      -&gt;offset($offset)
                      -&gt;limit($limit)
                      ;
  
                      $records = $stmt-&gt;process($csv);
  
                      foreach ($records as $record) 
                      {
                          $rec_arr[] = array_values($record);
                      }
  
                      //trim index if the value of an array is empty
                      $records_arr = $service-&gt;trimArray($rec_arr);
  
                      if(count($records_arr)&gt;0)
                      {
                          foreach($records_arr as $ck =&gt; $cv){
  
  
                              $committee_arr = array();
                              foreach ($map_data as $mk =&gt; $mv) {
                                  if(isset($mv)){
                                      $data_type = $service-&gt;getDatabaseColumnType($table,$mv);
                                      //if data is one of datetime data type
                                      //then format the csv data to mysql datetime format
                                      if($data_type == 'date' || $data_type == 'datetime' || $data_type == 'timestamp'){
                                          $datetime =  (array)$cv[$mk];
                                          $dt = array_shift($datetime);
                                          $dt = date('Y-m-d h:i:s', strtotime($dt));
                                          $committee_arr[$mv] = $dt;
                                      }else{
                                          $committee_arr[$mv] = $cv[$mk];
                                      }  
  
                                  }
                              }
  
                              $error_encountered = false;
  
  
                              DB::beginTransaction();
  
                              if(!empty($committee_arr['com_id'])){
  
                                  try{
                                          $committee_row = Committee::updateOrCreate(
                                              ['com_id' =&gt; $committee_arr['com_id']],
                                              $committee_arr
                                          );
                                          if ($committee_row-&gt;wasRecentlyCreated === true) {
                                              $committee_row-&gt;created_by = $this-&gt;data['user_id'];
                                          }else{
                                              $committee_row-&gt;updated_by = $this-&gt;data['user_id'];
                                          }
                                          $committee_row-&gt;save();
                                      } catch (\Exception $e) {
                                          $error_encountered = true;
                                           $this-&gt;error_arr[] = $e-&gt;getMessage();
                                           $this-&gt;error_row_numbers[] = $this-&gt;data['row_value']; 
                                      }
  
                              }
  
  
                              DB::commit();
                              //just to keep track which row is currently processing
                              //so that user can be notified in which row of csv
                              //there is an error
                              $this-&gt;data['row_value'] = $this-&gt;data['row_value'] + 1;
  
                          }
  
                          //offset just to start fectch next chunk of data from csv
                          $this-&gt;data['offset'] = $offset + $limit;
  
              //Call to same job but with increased offset value
                          $committeeInsertJob = (new StoreCommittee($this-&gt;data,$this-&gt;error_arr,$this-&gt;error_row_numbers))-&gt;delay(Carbon::now()-&gt;addSeconds(3)); 
                          dispatch($committeeInsertJob);
  
                      }else{
  
                          //Store activity just to keep track of activity
                          $activity = new Activity();
                          $activity-&gt;url = $this-&gt;data['url'];
                          $activity-&gt;action = 'store';
                          $activity-&gt;description = $table;
                          $activity-&gt;user_id = $this-&gt;data['user_id'];
                          $activity-&gt;created_at = date('Y-m-d H:i:s');
                          $activity-&gt;save();
  
                          $arr_data = [
                                  'filename' =&gt; $filename,
                                  'user_name' =&gt; $this-&gt;data['user_name'],
                                  'error' =&gt; $this-&gt;error_arr,
                                  'error_row_numbers' =&gt; $this-&gt;error_row_numbers
                              ];
                          //Notify user that the job is complete
                          Mail::to($this-&gt;data['user_email'])-&gt;send(new CSVImportJobComplete($arr_data));
  
                      }
  
  
  
              if (!ini_get("auto_detect_line_endings")) {
                  ini_set("auto_detect_line_endings", FALSE);
              }
          }
  </code></pre>
  
  <p>Error: From (laravel.log inside storage)</p>
  
  <pre><code>[2019-04-05 07:13:23] local.ERROR: PDOStatement::execute(): MySQL server has gone away (SQL: insert into `jobs` (`queue`, `attempts`, `reserved_at`, `available_at`, `created_at`, `payload`) values (default, 0, , 1554448406, 1554448403, ....................................................(long list)
  </code></pre>
  
  <p>From: command terminal</p>
  
  <pre><code>$ php artisan queue:work --tries=3
  [2019-04-05 07:09:11][1] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:09:33][1] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:09:36][2] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:09:58][2] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:10:01][3] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:10:23][3] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:10:26][4] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:10:48][4] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:10:51][5] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:11:13][5] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:11:17][6] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:11:40][6] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:11:43][7] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:12:05][7] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:12:08][8] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:12:31][8] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:12:34][9] Processing: App\Jobs\StoreCommittee
  [2019-04-05 07:12:57][9] Processed:  App\Jobs\StoreCommittee
  [2019-04-05 07:13:00][10] Processing: App\Jobs\StoreCommittee
  
  dell@DESKTOP-UQ2 MINGW64 /d/wamp64/www/project(master)
  $
  (it stops without any error or failed notifications)
  
  </code></pre>
  
  <p>Is there anything I can improve on my job logic? How can I handle all this connection drop, or maxed timed out or some other stuff? I don't think the increasing timeout is the solution. As it can't be guaranteed that it will be finished within this fixed time.</p>
  
  <p>Instead is there a way, were connection can be closed and again reconnect between each queue working?</p>