【发布时间】:2021-11-05 21:13:20
【问题描述】:
我正在尝试使用 Maatwebsite Excel 在 laravel 8 中导出包含大约 100,000 行 12 个月的 xlsx 文件,此过程大约需要 15~30 分钟,所以我在 laravel excel 中使用 queue 为了保持作业在后台运行并在完成时通知用户。在php artisan queue:work --memory=10240 的过程中,它会停止并在终端中显示此错误
如果我运行php artisan queue:work,进程将停止并且作业仍在jobs table
当我再次运行它时,它会失败并在failed_jobs table 中显示此错误
Illuminate\Queue\MaxAttemptsExceededException: Maatwebsite\Excel\Jobs\AppendQueryToSheet has been attempted too many times or run too long. The job may have previously timed out.
我尝试服用但不适用于我的药物
1- 在php.ini 中更改memory_limit post_max_size upload_max_filesize 我使用WAMP SERVER
2- 在config/excel.php 中更改memory_limit => 6000000 和chunk_size => 1100。
导出使用队列的代码
类 MarkupAccountsYears
<?php
namespace App\Exports;
use App\Models\markupPreCalculate;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithTitle;
class MarkupAccountsYears implements FromQuery, WithTitle
{
private $month;
private $year;
public function __construct(int $year, int $month)
{
$this->month = $month;
$this->year = $year;
}
public function query()
{ >orderBy('TimeMsc');
$finalItem = ['real'];
return markupPreCalculate::query()->select('TimeMsc', '****', 'Login', 'Full_Name',
'Group', '****', '****', '****', '****', '****', '****')
->whereYear('TimeMsc', $this->year)
->whereMonth('TimeMsc', $this->month)
->orderBy('TimeMsc')
->where(function ($query) use ($finalItem) {
foreach($finalItem as $keyword){
if(is_numeric($keyword))
{
$query->orwhere('Login', '=', $keyword);
}else
{
$query->orwhere('Group', 'like', '%' . $keyword . '%');
}
}
});
}
/**
* @return string
*/
public function title(): string
{
return 'Month ' . $this->month;
}
}
类 MarkupAccount
<?php
namespace App\Exports;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\Exportable;
class MarkupAccount implements WithMultipleSheets, ShouldQueue
{
use Exportable;
protected $year;
public function __construct(int $year)
{
$this->year = $year;
}
/**
* @return array
*/
public function sheets(): array
{
$sheets = [];
for ($month = 1; $month <= 1; $month++) {
$sheets[] = new MarkupAccountsYears($this->year, $month);
}
return $sheets;
}
}
类 MarkupAccount
<?php
namespace App\Exports;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\Exportable;
class MarkupAccount implements WithMultipleSheets, ShouldQueue
{
use Exportable;
protected $year;
public function __construct(int $year)
{
$this->year = $year;
}
/**
* @return array
*/
public function sheets(): array
{
$sheets = [];
for ($month = 1; $month <= 1; $month++) {
$sheets[] = new MarkupAccountsYears($this->year, $month);
}
return $sheets;
}
}
控制器
public function downloadExcl(){
(new MarkupAccount(2021))->queue('test'.time().'.xlsx')->chain([dump("Done")]);
}
有什么办法可以解决这个问题还是我做错了什么?
【问题讨论】:
标签: php excel laravel laravel-excel laravel-queue