发送文件时,Laravel Queue无法正常工作

时间:2017-06-26 11:38:21

标签: laravel file queue

我正在使用Laravel Queue发送文件,但似乎Laravel Queue无法正常工作。在我的PHP配置文件中,我将memory_limit设置为128 MB(我不想更改它)。在Laravel配置文件中,我有队列驱动程序是同步(当驱动程序是数据库时它不起作用)。但是在这个过程中我遇到了以下错误:

  

允许134217728字节的内存大小耗尽(试图分配31860068字节)PHP

这是我的逻辑。

    $storage = $this->configs[ 'storage' ];
    if( isset( $this->configs[ 'old_storage' ] ) ) {
        $old_storage = $this->configs[ 'old_storage' ];
    } else {
        throw new \Exception( 'Not found old storage' );
    }

    $files = $old_storage->files()->withTrashed()->get();

    try {
        if( $old_storage->testConnection()[ 'status' ] != 'true' ) {
            throw new \Exception( ufl( 'disk::disk.not_connection' ) . ' ' . $old_storage->name );
        }

        if( $storage->testConnection()[ 'status' ] != 'true' ) {
            throw new \Exception( ufl( 'disk::disk.not_connection' ) . ' ' . $storage->name );
        }

        foreach( $files as $file ) {
            try {
                //TODO Добавить логи в будущем (ошибка при копировании файла)
                $stream = $file->readStream();
                $file->setStorage( $storage->id );

                $file->storage()->getDriver()->putStream( $file->getRealDestination(), $stream );

                $file->putStream( $file->getRealDestination(), $stream );
            } catch( \Exception $e ) {
                $file->setStorage( $storage->id );
            }
            $file->save();
        }

        $old_storage->is_active = 0;
        $old_storage->status    = DiskStorage::STATUS_OK;
        $old_storage->save();
        $storage->is_active = 1;
        $storage->status    = DiskStorage::STATUS_OK;
        $storage->save();

    } catch( \Exception $e ) {
        //Откат
        foreach( $files as $file ) {
            $file->setStorage( $old_storage->id );
            $file->save();
        }

        $storage->status = DiskStorage::STATUS_OK;
        $storage->save();

        $old_storage->status = DiskStorage::STATUS_OK;
        $old_storage->save();

        throw new \Exception( $e->getMessage() );
    }

0 个答案:

没有答案