如何在laravel中处理时间获取请求? [英] how to process time taking request in laravel?

查看:119
本文介绍了如何在laravel中处理时间获取请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从excel文件中读取数据,然后将其存储在数据库中.但是有问题.有时文件有很多数据,近20000条记录.当我尝试使用大量数据处理该请求时,它会处理2至3分钟,并在数据库中输入大约13000条记录,然后该请求失败. 这就是我在做什么.这个过程由五个功能完成,每个功能都有其自己的工作. 现在,我想应该按队列进行操作,但是我不知道它如何工作,因为我从每个函数中都返回了一些无法在队列中工作的东西,而且我的控制器中也有全局数组.

I am trying to read data from excel file and and then store it in database. but there is problem . sometimes file has a lot of data nearly 20000 records. when I try to process that request with that much data it process for 2 to 3 minutes and enter around 13000 record in database and after that request got failed. here is what i am doing . this process gets completed in five functions every functions has it own job. Now, I think I should do it by queue but i have no idea how it will work because I am returning some thing from every function which won't work in queue and there is also global array in my controller.

这是我的控制器.我认为您不必一一阅读所有功能,只是告诉我如何实现目标即可.

here is my controller. I think you don't need to read all the functions one by one just give me idea how I can achieve my goal

    //global array variable declaration for import delivery Zone
    protected $createdDeliveryZones;
    protected $updatedDeliveryZones;
    protected $errorDeliveryZones;
    /**
     * AddressController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->createdDeliveryZones = array();
        $this->updatedDeliveryZones = array();
        $this->errorDeliveryZones = array();
        $this->middleware('staff.permissions', ['only' => ['index', 'create', 'edit', 'delete']]);
    }
//delivery zone import function
    public function importDeliveryZone(Request $request)
    {

        if ($request->isMethod('get')) {
            return back();
        }
        $extension = $request->file('delivery_zone_file')->getClientOriginalExtension();
        if($extension != 'csv')
        {
            return back()->with('error','Please Choose CSV File');
        }  

        //getting spreadsheet data
        $path        = $request->file('delivery_zone_file')->getRealPath();
        $spreadsheet = IOFactory::load($path);
        $sheetData   = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
        //these variable will decide from where locations are starting in spreadsheet
        $startAt = 2;
        $endAt     = count($sheetData);
        $validation = 1;

        //   loop will fetch row and pass it to function row by row
          foreach ($sheetData as $line_number => $delivery_zone) {
            //fileformatevalidation function will return 1 or 0 
            if($line_number == 1){
            //  $validation = $this->fileFormatValidation($delivery_zone,$line_number); 

             if($validation == 0){
                return back()->With('error','Please choose File with Correct Format');
            }
            }
            else 
            {

                    if ($line_number >= $startAt && $line_number <= $endAt && array_filter($delivery_zone)) {
                            $this->extractDeliveryZones($delivery_zone,$line_number);
                    }
                }
            }
            $errorDeliveryZones = $this->errorDeliveryZones;
            $updatedDeliveryZones = $this->updatedDeliveryZones;
            $createdDeliveryZones = $this->createdDeliveryZones;
            return view('web.pages.transports.delivery_zones.import_delivery_zone_validation',compact('errorDeliveryZones',$errorDeliveryZones,'updatedDeliveryZones',$updatedDeliveryZones,'createdDeliveryZones',$createdDeliveryZones));
    }

    public function fileFormatValidation($delivery_zone)
    {
        if($delivery_zone['A'] != 'id' || $delivery_zone['B'] != 'postcode' || $delivery_zone['C'] != 'suburb' || $delivery_zone['D'] != 'state' || $delivery_zone['E'] != 'country' || $delivery_zone['F'] != 'income_rate_zone' || $delivery_zone['G'] != 'expense_rate_zone' || $delivery_zone['H'] != 'delivery_run' || $delivery_zone['I'] != 'carrier' || $delivery_zone['J'] != 'customer')
        {

            return 0;
        }
        else 
        {
            return 1;
        }
    }

    public function extractDeliveryZones($delivery_zone,$line_number)
    {
        $data_validation = 1;

            //making array of location data
            $deliveryZoneArray = array('id' => $delivery_zone['A'],'postcode' => $delivery_zone['B'], 'suburb' => $delivery_zone['C'], 'address_zone' => $delivery_zone['D'],'country' => $delivery_zone['E']);
             // this function will validate data in excel sheet

            Customer::where('name',$delivery_zone['J'])->exists() ? $deliveryZoneArray['apply_to_customer'] = Customer::where('name',$delivery_zone['J'])->first()->id : $deliveryZoneArray['apply_to_customer'] = null;
            if(DeliveryRun::where('name',$delivery_zone['H'])->exists()) 
            {
            $deliveryZoneArray['delivery_run'] = DeliveryRun::where('name',$delivery_zone['H'])->first()->id;
            } 
             else 
            {
               $deliveryZoneArray['delivery_run'] = $delivery_zone['H'];
               $deliveryZoneArray['reason'] = "Delivery Run Dosn't Exist";
               array_push($this->errorDeliveryZones,$deliveryZoneArray);

               return 1;
             }

             $data_validation = $this->importFileDataValidation($deliveryZoneArray);

             if($data_validation == 1){

                $this->createDeliveryZoneFromExcel($deliveryZoneArray);
             }

    }

    public function createDeliveryZoneFromExcel($deliveryZoneArray)
    {
        if(empty($deliveryZoneArray['id']))
        {
            if(DeliveryZone::where('postcode',$deliveryZoneArray['postcode'])->where('suburb',$deliveryZoneArray['suburb'])->where('address_zone',$deliveryZoneArray['address_zone'])->exists())
            {
                $deliveryZoneArray['reason'] = "Duplicate postcode, suburb and state";
                array_push($this->errorDeliveryZones,$deliveryZoneArray);
            }

            else
            {
               DeliveryZone::create($deliveryZoneArray);
               array_push($this->createdDeliveryZones,$deliveryZoneArray);
            }
        }
        else
        {
            if(DeliveryZone::where('id',$deliveryZoneArray['id'])->exists())
            {
                $delivery_run = DeliveryZone::find($deliveryZoneArray['id'])->update($deliveryZoneArray);
                array_push($this->updatedDeliveryZones, $deliveryZoneArray);
            }
            else
            {
                $deliveryZoneArray['reason'] = "ID Doesn't Exist";
                array_push($this->errorDeliveryZones,$deliveryZoneArray);
            }
        }

    }

    public function importFileDataValidation($deliveryZoneArray)
    {
        $countries = array('Australia');
        if(empty($deliveryZoneArray['postcode']) || empty($deliveryZoneArray['suburb']) || empty($deliveryZoneArray['address_zone']))
        {
            $deliveryZoneArray['reason'] = 'Postcode, suburb or satate is Empty';
            array_push($this->errorDeliveryZones,$deliveryZoneArray);  
            return 0;
        }
       else if(!in_array($deliveryZoneArray['country'],$countries))
       {
        $deliveryZoneArray['reason'] = 'Country not Found';
        array_push($this->errorDeliveryZones,$deliveryZoneArray);  
        return 0;
       }
        else
        {
             return 1;
        }
    }

推荐答案

我在与此过程相关的每个函数中都使用了set_time_limit(200),这是我的错,我只需要在第一个函数中使用它.现在,它工作正常

I was using set_time_limit(200) in every function related to this process that was my mistake I only have to use it in the first function. Now, its working fine

这篇关于如何在laravel中处理时间获取请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆