Maatwebsite 3.1导入,Queue不起作用
Maatwebsite 3.1 import, Queue does not work
我的文件是 excel.xlsx 包含超过 20,000 行,我正在使用 Centos 7 与 Nginx 网络服务器。当我上传一个只有几行的小文件时,它可以工作,但是当我引入 ShouldQueue 和 WithChunkReading 接口时,即使文件很小,它也会失败。我需要帮助。感谢您的宝贵时间
Here is the error in a log file
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php
[2019-07-11 14:48:47] development.ERROR: [0] File "/tmp/laravel-excel-4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx" does not exist. on line 344 of file vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php
[2019-07-11 14:48:47] development.ERROR: [0] File "/tmp/laravel-excel-4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx" does not exist. on line 344 of file vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php
Here is a queue error
[2019-07-12 10:11:47][532] Processing: Maatwebsite\Excel\Jobs\QueueImport
[2019-07-12 10:11:47][532] Processed: Maatwebsite\Excel\Jobs\QueueImport
[2019-07-12 10:11:47][533] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-12 10:11:47][534] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-12 10:11:47][535] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-12 10:11:47][535] Failed: Maatwebsite\Excel\Jobs\ReadChunk
Here is my controller function
public function store(Request $request)
{
Excel::import(new HsCodeImport(),"650.xlsx",'local');
return redirect()->back()->withFlashSuccess(__('label.app_success'));
}
Here is my Import file on App\Imports\HsCodeImport.php
<?php
namespace App\Imports;
use App\Exceptions\GeneralException;
use App\Models\Application\Hscode;
use App\Models\ReceiptCode\ReceiptCode;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class HsCodeImport implements ToCollection, WithHeadingRow, ShouldQueue, WithChunkReading
{
use Importable;
protected $receiptCode;
protected $chunk = 500;
protected $hscode_key = 'hscode';
protected $description_key = 'description';
protected $regulatory_status_key = 'regulatory_status';
protected $comment_key = 'comment';
protected $headings = ['hscode','description','regulatory_status','comment'];
public function __construct(/*ReceiptCode $receiptCode*/)
{
// $this->receiptCode = $receiptCode;
}
/**
* @param Collection $collection
* @throws GeneralException
*/
public function collection(Collection $collection)
{
/*fetching the first Collection*/
$columns = $collection->first();
if (!$columns->has($this->headings)) {
/*When file has different headings*/
} else {
/*When the file has expected headings*/
/*Truncate temp table*/
DB::table('hs_code_temps')->truncate();
/*Counting rows on a file*/
$original_file_rows_count = $collection->count();
/*Chunk the file data according @var $chunk*/
$chunks = $collection->chunk($this->chunk);
/*read each chunks insert into Temporary table and validate to get if there is error in each row*/
$chunks->each(function ($item) {
/*Iterate on each chunk*/
$item->toArray();
foreach ($item as $row) {
/* start: Validating row entries */
$error_report = "";
$error = 0;
foreach ($row as $key => $value) {
if (trim($key) == $this->hscode_key) {
if (trim($value) == "" Or $value == NULL) {
$error = 1;
$error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
$row[$key] = NULL;
}
} elseif (trim($key) == $this->description_key) {
if (trim($value) == "" Or $value == NULL) {
$error = 1;
$error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
$row[$key] = NULL;
}
} elseif (trim($key) == $this->regulatory_status_key) {
if (trim($value) == "" Or $value == NULL) {
$error = 1;
$error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
$row[$key] = NULL;
}
}
}
/*Inserting into Temp table*/
DB::table('hs_code_temps')->insert([
'code' => $row[$this->hscode_key],
$this->description_key => $row[$this->description_key],
$this->regulatory_status_key => $row[$this->regulatory_status_key],
$this->comment_key => $row[$this->comment_key],
'receipt_code_id' => 1/*$receiptCode->id*/,
'error' => $error,
'error_report' => $error_report,
'created_at' => Carbon::now(),
]);
/* end: Validating row entries*/
}
});
/*compare total rows with no error to total rows of the file*/
$total_temp_rows_count = DB::table('hs_code_temps')->whereError(0)->count();
if ($total_temp_rows_count != $original_file_rows_count) {
/*When there is error and rows are not equal*/
} else {
/*When there is no error*/
$originalHsCode = new Hscode();
$temp_table = DB::table('hs_code_temps')->get(['code', $this->description_key, $this->regulatory_status_key, $this->comment_key])/*->toArray()*/;
/*Iterate throw the rows in a temp row*/
foreach ($temp_table as $object) {
/*copy data from temp table to origin table*/
$originalHsCode->create(get_object_vars($object));
}
/*Truncate temp table*/
DB::table('hs_code_temps')->truncate();
}
}
}
public function headings(): array
{
return ['HSCode','Description','Regulatory_Status','Comment'];
}
/*public function sheets(): array
{
}*/
public function chunkSize(): int
{
return 500;
}
public function batchSize(): int
{
return 500;
}
}
我希望作业运行一个大数据到一个chunk来处理
将 false 作为第三个参数传递给 chunk() 以禁用排队
$data = [];
Excel::filter('chunk')->load($path)->chunk(1000, function ($results) use (&$data) {
foreach ($results as $row) {
$data[] = $row;
}
}, $shouldQueue = false);
return $data;
我在部署到 heroku 时遇到了类似的问题,这表明路径有问题。:
`ERROR: [0] File "/tmp/laravel-excel-4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx"
does not exist. on line 344 of file
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php`
laravel-excel 在 config/excel.php 中定义了 local_path 变量,因此请尝试更改此处的路径或检查您的 public_path () 你在哪里遇到问题。
在我的例子中,table failde_jobs 中保存的例外是:
"exception": """ InvalidArgumentException: File "/app/storage/framework/laravel-excel/laravel-excel-aCnUQTJT7ADvAnrf1AOEcaCBPLjhauij" does not exist. in /app/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Shared/File.php:135\n
一直在尝试查看如何进行更改以使其不进入应用程序文件夹..
我的文件是 excel.xlsx 包含超过 20,000 行,我正在使用 Centos 7 与 Nginx 网络服务器。当我上传一个只有几行的小文件时,它可以工作,但是当我引入 ShouldQueue 和 WithChunkReading 接口时,即使文件很小,它也会失败。我需要帮助。感谢您的宝贵时间
Here is the error in a log file
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php
[2019-07-11 14:48:47] development.ERROR: [0] File "/tmp/laravel-excel-4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx" does not exist. on line 344 of file vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php
[2019-07-11 14:48:47] development.ERROR: [0] File "/tmp/laravel-excel-4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx" does not exist. on line 344 of file vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php
Here is a queue error
[2019-07-12 10:11:47][532] Processing: Maatwebsite\Excel\Jobs\QueueImport
[2019-07-12 10:11:47][532] Processed: Maatwebsite\Excel\Jobs\QueueImport
[2019-07-12 10:11:47][533] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-12 10:11:47][534] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-12 10:11:47][535] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-12 10:11:47][535] Failed: Maatwebsite\Excel\Jobs\ReadChunk
Here is my controller function
public function store(Request $request)
{
Excel::import(new HsCodeImport(),"650.xlsx",'local');
return redirect()->back()->withFlashSuccess(__('label.app_success'));
}
Here is my Import file on App\Imports\HsCodeImport.php
<?php
namespace App\Imports;
use App\Exceptions\GeneralException;
use App\Models\Application\Hscode;
use App\Models\ReceiptCode\ReceiptCode;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class HsCodeImport implements ToCollection, WithHeadingRow, ShouldQueue, WithChunkReading
{
use Importable;
protected $receiptCode;
protected $chunk = 500;
protected $hscode_key = 'hscode';
protected $description_key = 'description';
protected $regulatory_status_key = 'regulatory_status';
protected $comment_key = 'comment';
protected $headings = ['hscode','description','regulatory_status','comment'];
public function __construct(/*ReceiptCode $receiptCode*/)
{
// $this->receiptCode = $receiptCode;
}
/**
* @param Collection $collection
* @throws GeneralException
*/
public function collection(Collection $collection)
{
/*fetching the first Collection*/
$columns = $collection->first();
if (!$columns->has($this->headings)) {
/*When file has different headings*/
} else {
/*When the file has expected headings*/
/*Truncate temp table*/
DB::table('hs_code_temps')->truncate();
/*Counting rows on a file*/
$original_file_rows_count = $collection->count();
/*Chunk the file data according @var $chunk*/
$chunks = $collection->chunk($this->chunk);
/*read each chunks insert into Temporary table and validate to get if there is error in each row*/
$chunks->each(function ($item) {
/*Iterate on each chunk*/
$item->toArray();
foreach ($item as $row) {
/* start: Validating row entries */
$error_report = "";
$error = 0;
foreach ($row as $key => $value) {
if (trim($key) == $this->hscode_key) {
if (trim($value) == "" Or $value == NULL) {
$error = 1;
$error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
$row[$key] = NULL;
}
} elseif (trim($key) == $this->description_key) {
if (trim($value) == "" Or $value == NULL) {
$error = 1;
$error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
$row[$key] = NULL;
}
} elseif (trim($key) == $this->regulatory_status_key) {
if (trim($value) == "" Or $value == NULL) {
$error = 1;
$error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
$row[$key] = NULL;
}
}
}
/*Inserting into Temp table*/
DB::table('hs_code_temps')->insert([
'code' => $row[$this->hscode_key],
$this->description_key => $row[$this->description_key],
$this->regulatory_status_key => $row[$this->regulatory_status_key],
$this->comment_key => $row[$this->comment_key],
'receipt_code_id' => 1/*$receiptCode->id*/,
'error' => $error,
'error_report' => $error_report,
'created_at' => Carbon::now(),
]);
/* end: Validating row entries*/
}
});
/*compare total rows with no error to total rows of the file*/
$total_temp_rows_count = DB::table('hs_code_temps')->whereError(0)->count();
if ($total_temp_rows_count != $original_file_rows_count) {
/*When there is error and rows are not equal*/
} else {
/*When there is no error*/
$originalHsCode = new Hscode();
$temp_table = DB::table('hs_code_temps')->get(['code', $this->description_key, $this->regulatory_status_key, $this->comment_key])/*->toArray()*/;
/*Iterate throw the rows in a temp row*/
foreach ($temp_table as $object) {
/*copy data from temp table to origin table*/
$originalHsCode->create(get_object_vars($object));
}
/*Truncate temp table*/
DB::table('hs_code_temps')->truncate();
}
}
}
public function headings(): array
{
return ['HSCode','Description','Regulatory_Status','Comment'];
}
/*public function sheets(): array
{
}*/
public function chunkSize(): int
{
return 500;
}
public function batchSize(): int
{
return 500;
}
}
我希望作业运行一个大数据到一个chunk来处理
将 false 作为第三个参数传递给 chunk() 以禁用排队
$data = [];
Excel::filter('chunk')->load($path)->chunk(1000, function ($results) use (&$data) {
foreach ($results as $row) {
$data[] = $row;
}
}, $shouldQueue = false);
return $data;
我在部署到 heroku 时遇到了类似的问题,这表明路径有问题。:
`ERROR: [0] File "/tmp/laravel-excel-4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx"
does not exist. on line 344 of file
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php`
laravel-excel 在 config/excel.php 中定义了 local_path 变量,因此请尝试更改此处的路径或检查您的 public_path () 你在哪里遇到问题。
在我的例子中,table failde_jobs 中保存的例外是:
"exception": """ InvalidArgumentException: File "/app/storage/framework/laravel-excel/laravel-excel-aCnUQTJT7ADvAnrf1AOEcaCBPLjhauij" does not exist. in /app/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Shared/File.php:135\n
一直在尝试查看如何进行更改以使其不进入应用程序文件夹..