vendredi 27 juillet 2018

Foreach loop not working in laravel queue job

I'm trying to send data to Web Api...The idea is load a lot of sheets one by one. So I create a job to send this sheets to the curl function which is in charge of insert data on DB. I'm passing an array like this to my job:

0 => array:5 [
"xls_path" => "http://airviro.r9.cl/dev-sma/uploads/TEST_SO2_NO_PM101532702117.xlsx"
"sheet" => "Registros Crudos NO"
"estacion" => "DV1"
"tipo_dato" => "M"
"cont" => "0002"
]

1 => array:5 [
"xls_path" => "http://airviro.r9.cl/dev-sma/uploads/TEST_SO2_NO_PM101532702117.xlsx"
"sheet" => "Registros Crudos SO2"
"estacion" => "DV1"
"tipo_dato" => "M"
"cont" => "0001"]

2 => array:5 [
"xls_path" => "http://airviro.r9.cl/dev-sma/uploads/TEST_SO2_NO_PM101532702117.xlsx"
"sheet" => "Registros Crudos MP10"
"estacion" => "DV1"
"tipo_dato" => "M"
"cont" => "PM10"
 ]

Each position contains sheet information...so the job function in charge of process this array is:

public function handle()
{

    $total_hojas = count($this->hojas["hojas"]);

    $i=1;
    $porcentaje = 0;

    foreach ($this->hojas["hojas"] as $key => $value) {

      $response = Insert::insertData($value);
      $response = json_decode($response,true);

      if($response["code"] == 0){

        $porcentaje = $porcentaje + ($i/$total_hojas) * 100;

        #actualizar estado de carga de las hojas en bd

        DB::table('importacion')
          ->where('id', $this->hojas["import"])
          ->update(['comentario' => "Porcentaje de carga completado: $porcentaje%"]);

        continue;
        #$fila = DB::table('importacion')->select('*')->where('id','=',$this->hojas["import"])->get();
        #dd($fila);
      }
      else{

        DB::table('importacion')
          ->where('id', $this->hojas["import"])
          ->update(['comentario' => $response["msg"]]);
        break;
      }


      $i++;
    }
}

The array with sheets data it's reaching without problems the handle function of my job...the first position of the array is send to the curl function...but then when the foreach loop must continue with next position of the array and send it to the service.. it just keep looping over the position 0 of the array...The service is returning an ok message after operate an array position..so when the first one is processed the message i'm getting on $response variable it's ok and first sheet data it's saving on DB...so the final result is, i'm getting the first response message over and over, and the foreach loop doesn't process the other positions...Any help ?? Thanks for your time.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2mOBKD0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire