【问题标题】:Laravel queue process gets timeoutLaravel 队列进程超时
【发布时间】:2019-08-08 23:23:19
【问题描述】:

我正在运行一个应该运行 Laravel Excel 导入的命令,但它会在一段时间后耗尽。我正在使用块,它以前工作过,但现在我正在努力让它工作。它是一组文件,位于文件系统内的文件夹中。

这是我使用 artisan 运行的命令:

public function handle()
    {
        //
        $directory = 'pv';
        $files = Storage::allFiles($directory);
        \Log::info('Process started.');
        $start = microtime(true);
        ini_set('max_execution_time', 600);
        foreach($files as $file)
        {
            $fname = basename($file);
            \Log::info('Processing',[$fname]);
            $arr = explode(" ", $fname);
            $day = substr($arr[2], 0, 10);
            $date = Carbon::parse($day);
            Excel::queueImport(new POSImport($date), $file);
        }
        $time = microtime(true) - $start;
        $me = 'me@mail.com';
        $msg = 'Process finished in '. $time.' secs.';
        Mail::to($me)->queue(new TasksFinished($msg));
        $this->call('calcular:previos', [
        '--queue' => 'default'
        ]);
    }

内存不足。

这是导入。

<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Pos;
use App\Device;
use \Datetime;

class POSImport implements ToCollection, WithHeadingRow, WithChunkReading, ShouldQueue
{


   public $tries = 3;

   function __construct(Datetime $date) {
       $this->date = $date;
   }

   /**
    * Importa datos de la planilla de puntos vigentes de Banco Estado.
     * 
     * Actúa sobre Device (equipos) y POS 
     * 
    * @param Collection $rows
    */

    public function collection(Collection $rows)
    {
        //
        ini_set('max_execution_time', 600);
        foreach($rows as $row)
        {
            // crea o modifica POS

            if(!isset($row['marca'])) {
                return null;
            }
            // Busca el POS (lugar) en la base
            $pos = Pos::where('id', $row['pos'])->first();
            // si no hay un "pos" registrado lo crea
            if(!$pos) {
                $pos = new Pos;
                $pos->id = $row['pos'];
                $pos->vigente = ($row['estado'] == 'VIGENTE' ? true : false);
                $pos->save();
            } else {
                $pos->vigente = ($row['estado'] == 'VIGENTE' ? true : false);
                $pos->save();
            }
            // limpia serial de ceros a la izquierda
            $serial = ltrim($row['serie_equipo'], '0');
            // busca serial en la base de datos
            $device = Device::where('serial', $serial)
                    ->where('fecha_recepcion', '<', $this->date)
                    ->where('customer_id', 1)
                    ->orderBy('fecha_recepcion', 'asc')
                    ->first();

            if($device && $device->pos_id != $row['pos'] && $device->fecha_instalacion != $this->date){
                // busca el dispositivo anterior

                $device->pos_id = $pos->id;
                $device->fecha_instalacion = $this->date;
                $device->save();

                $device->pos()->attach($pos);
            } 
        }

    }

    public function chunkSize(): int {
        return 2000;
    }

}

如您所见,我正在使用 WithChunkReading 和 ShouldQueue。 当我过去开始这个过程时,它只是处理了块,但现在队列显示了很多 QueueImport 条目。

我正在使用数据库作为队列驱动程序。

我希望你能帮我解决这个问题。

命令错误:

Symfony\Component\Debug\Exception\FatalErrorException  : Allowed memory size of 536870912 bytes exhausted (tried to allocate 175747072 bytes)

  at C:\laragon\www\reportes\vendor\laravel\framework\src\Illuminate\Queue\Queue.php:138
    134|
    135|         return array_merge($payload, [
    136|             'data' => [
    137|                 'commandName' => get_class($job),
  > 138|                 'command' => serialize(clone $job),
    139|             ],
    140|         ]);
    141|     }
    142|


   Whoops\Exception\ErrorException  : Allowed memory size of 536870912 bytes exhausted (tried to allocate 175747072 bytes)

  at C:\laragon\www\reportes\vendor\laravel\framework\src\Illuminate\Queue\Queue.php:138
    134|
    135|         return array_merge($payload, [
    136|             'data' => [
    137|                 'commandName' => get_class($job),
  > 138|                 'command' => serialize(clone $job),
    139|             ],
    140|         ]);
    141|     }
    142|

这是很多数据,这就是为什么我使用块和队列但我仍然有这个问题。

【问题讨论】:

    标签: laravel laravel-queue


    【解决方案1】:
    class POSImport implements ShouldQueue
    {
        /**
         * The number of seconds the job can run before timing out.
         *
         * @var int
         */
        public $timeout = 120;
    }
    

    另外,如果您希望队列工作者增加超时时间,您可以使用--timeout 标志(我认为默认值为 30 秒):

    php artisan queue:work --timeout=300


    对此我不确定,但也可能有效:

    $this->call('calcular:previos', [
        '--queue' => 'default',
        '--timeout' => '300'
    ]);
    

    【讨论】:

    • 问题是它也会因为内存大小而耗尽,但即使文件本身没有改变,以前也不会。 [2019-03-18 14:56:59][312] 处理:Maatwebsite\Excel\Jobs\QueueImport [2019-03-18 14:57:04][313] 处理:Maatwebsite\Excel\Jobs\ReadChunk [2019 -03-18 14:57:56][313] 处理:Maatwebsite\Excel\Jobs\ReadChunk [2019-03-18 14:58:00][314] 处理:Maatwebsite\Excel\Jobs\QueueImport 而之前只有 readchunk被处决了。
    • Whoops\Exception\ErrorException :在命令和 Symfony\Component\Process\Exception\ProcessTimedOutException 上允许的内存大小为 536870912 字节耗尽(试图分配 175747072 字节):进程“C:\laragon\ bin\php\php-7.2.11-Win32-VC15-x64\php.exe 工匠队列:工作 --once --queue=default --delay=0 --memory=512 --sleep=3 --tries= 3" 超过了 60 秒的超时时间。在队列中
    • 嗯,我怀疑这可能是 fastcgi 问题 - 在您的 nginx 配置中,在 location ~ \.php$ { } 内部底部添加 fastcgi_read_timeout 3600;
    • 我正在使用 apache。现在,正在读取块,但命令会耗尽内存。
    • ini_set('memory_limit', '512M'); 你能试试这个吗?和php的内存分配有关
    猜你喜欢
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    相关资源
    最近更新 更多