运行命令计划表laravel时如何解决“致命错误:内存不足...”?

时间:2018-09-03 09:54:45

标签: laravel memory command laravel-5.6 schedule

我的命令如下:

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\ItemDetail;
class ImportItemDetail extends Command
{
    protected $signature = 'sync:item-detail';
    protected $description = 'sync item detail';
    public function __construct()
    {
        parent::__construct();
        ini_set('max_execution_time', 0);
        ini_set('memory_limit', '-1');
    }
    public function handle()
    {
        $path = storage_path('json/ItemDetail.json');
        $json = json_decode(file_get_contents($path), true);
        foreach ($json['value'] as $value) {
            ItemDetail::updateOrCreate(
                [ 'entry_number' => $value['Entry_Number'] ],
                [ 
                    'item_number' => $value['Item_Number'],
                    'description' => $value['Description'],
                    ....
                ]
            );
        }
    }
}

如果我运行命令,则存在这样的错误:

Fatal error: Out of memory (allocated 255852544) (tried to allocate 8192 bytes)

鉴于我在构造中设置了ini_set('memory_limit', '-1');

如何解决该错误?

注意

我在json文件中的记录包含成千上万的数据。因此,检查该字段需要花费很长时间

更新

我的ItemDetail.json是这样的:

{
  "@odata.context":"http://myapp-svr01.www.myapp.com:1224/DynamicsNAV100/ODataV4/$metadata#Collection(NAV.ODATAITEM)","value":[
    {
      "@odata.etag":"W/\"JzE2O0lBQUFBQUNIbXdrQ0FBQAD5OzE4ODQ1MDQ4NjA7Jw==\"","Entry_Number":123,"Item_Number":"9805010101","Variant_Code":"039","Posting_Date":"2018-01-02T00:00:00Z","Entry_Type":"Sale","Description":"MISC BISBAN POLOS 11MM A847","Quantity":-7200,"Source_Type":"Customer","Order_Type":" ","Sales_Amount_Actual":1800000,"ETag":"16;IAAAAACHmwkCAAAA9;1884504860;"
    },{
      "@odata.etag":"W/\"JzE2O0lBQUFBQUNIbkFrQ0FBQAD5OzE4ODQ1MDQ5MTA7Jw==\"","Entry_Number":124,"Item_Number":"9805010102","Variant_Code":"100","Posting_Date":"2018-01-02T00:00:00Z","Entry_Type":"Sale","Description":"MISC BISBAN POLOS 11MM A915","Quantity":-7200,"Source_Type":"Customer","Order_Type":" ","Sales_Amount_Actual":1800000,"ETag":"16;IAAAAACHnAkCAAAA9;1884504910;"
    }
  ]
}

我在上面的json中只给出了2条记录。实际上大约有150,000条记录

1 个答案:

答案 0 :(得分:1)

问题是您要一次读取整个文件,而应该分块或逐行读取。例如:

$items = [];
$file = fopen(storage_path('json/ItemDetail.json'),'r');
while (!feof($file)){
    $line = fgets($file);
    $obj = json_decode($line);
    // $obj is type stdClass
    // transform $obj to fit the update or create method
    ItemDetail::updateOrCreate( ... )
}

更新

处理大文件时,您应该研究流分析器。

jsonstreamingparser将是一个不错的选择。

相关问题