Deekshith's avatar

getting timeout error when importing data from csv.

I have a csv file with 1k data in each file but it is giving timeout problem after 73 records only. any tweaks to speed up? i have used chunck as well and now after timout error it is working in background

my code is,

public function postUploadData(Request $request)
    {
        $request->validate([
            'uploadfile' => 'required|required|mimes:csv,txt',

        ]);

        $file = $request->file('uploadfile');
        $filename = $file->getClientOriginalName();
        // File upload location
        $location = 'uploads';

        $file->move(public_path() . '/uploadfiles/', $filename);
        

        $header = NULL;
        $dataset = array();
        $delimiter = ',';

        if (($handle = fopen(public_path() . '/uploadfiles/' . $filename, 'r')) !== FALSE) {
            while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
                if(!$header) {
                    $header = $row;
                } else {
                    $dataset[] = array_combine($header, $row);
                }
            }


            fclose($handle);
        }

        $uuid = md5(rand(1, 10) . microtime());

        $createdata = [
                'file_name' => $filename,
                'type'  => 'uploadtype',
                'uuid' => $uuid,
                'upload_date' => date('Y-m-d'),
                'uploaded_by' => Auth::user()->id,
                ];

       UploadHistory::create($createdata);
    
       $chunks = array_chunk($dataset, 20);

       foreach($chunks as $dataget) {
           foreach($dataget as $data) {

        // for($i=0;$i<sizeof($data); $i++) {

             $jsonPayload = json_encode([[
                'field1' => $data[$i]['field1'],
                'field2' => $data[$i]['field2'],
                'field3' =>$data[$i]['field3'],
                'field4' => $data[$i]['field4'],
                'field5' => $data[$i]['field5'],
                'field6' => $data[$i]['field6'],
                'field7' => $data[$i]['field7'],
                'field8' => $data[$i]['field8'],
                'field9' => $data[$i]['field9'],
                'field10' => $data[$i]['field10'],
                'field11' => $data[$i]['field11'],
                'field12' => $data[$i]['field12'],
                'field13' => $data[$i]['field13'],
                'field14' => $data[$i]['field14'],
 				'field15' => $data[$i]['field15'],
				'field16' => $data[$i]['field16'],
            ]]);

            $client = new \GuzzleHttp\Client();
            $headers = [
                'Authorization' => '8245',
                'Content-Type' => 'application/json',
            ];

            
           // start request
           $promise = $client->postAsync('https://web.comI/api', [
                                'body' => $jsonPayload,
                                'headers' => $headers,
            ])->then(
               function ($response) {
                   return $response->getBody();
               }, function ($exception) {
                   return $exception->getMessage();
               }
           );
            
           // do other things
        //    echo '<b>This will not wait for the previous request to finish to be displayed!</b>';
            
           // wait for request to finish and display its response
           $response = $promise->wait();

           $responsedata = json_decode($response, true);
      


              if($responsedata)
              {
                
                $color = $responsedata['status'] == "Success" ? 'green' : 'red';
               

                $uploaddata = [
                    'status' => $responsedata['status'],
                    'message' => serialize($responsedata['statusMessage']),
                    'color'  => $color,
                    'uuid' => $uuid,
                    'uploaded_by' => Auth::user()->id,
                ];

              } else {

                $uploaddata = [
                        'status' => 'Fail',
                        'message' => 'No Response From API',
                        'color'  => 'red',
                        'uuid' => $uuid,
                        'uploaded_by' => Auth::user()->id,
                    ];
              }

              UploadDataTRack::create($uploaddata);

              
            
         }
         
        }

         $msg = 'Data Uploaded successfully. <a href="'. url('track-activity/upload/') . '/'.$uuid.'" target="_blank"> click here  </a>  to see the log';

         return redirect('admin-dashboard')->with('message',$msg);
    }

How to avoid timeout problem?

0 likes
0 replies

Please or to participate in this conversation.