我正在使用带有作业的 LARAVEL 队列在我的数据库中插入一个大的 excel,并且一些作业被卡住并且没有执行。
我将文件内容分块(每个作业 250 行)并插入其中的大部分内容,直到它停止。
插入代码(插入 250 行或更少行的作业)
public function handle()
{
$uuid = Uuid::generate(4);
$defaultsSize = 0;
$customSize = 0;
$defaultsIdFields = [];
$customFields = [];
if (sizeof($this->matrixDefaultFields) > 0) {
$defaultsSize = sizeof($this->matrixDefaultFields[0][0]); //size of one of the vecs in default values
$defaultsIdFields = $this->matrixDefaultFields[0][0]; // all default fields id
}
if (sizeof($this->matrixCustomFields) > 0) {
$customSize = sizeof($this->matrixCustomFields[0][0]); //sizeof one of the vecs in custom values
$customFields = $this->matrixCustomFields[0][0]; // all custom fields id
}
for ($i = 0; $i < sizeof($this->matrixContacts); $i++) {
$contact = Contact::create(['UUID' => $uuid, 'id_contact_list' => $this->matrixContacts[$i][1],
'nome' => $this->matrixContacts[$i][2], 'email' => $this->matrixContacts[$i][3], 'unsub_code' => $this->matrixContacts[$i][4]]);
for ($j = 0; $j < $defaultsSize; $j++) {
$defaultsValuesFields = $this->matrixDefaultFields[$i][1]; //field value
CompanyListFieldValues::create(['company_id' => $this->idCompany,
'contact_id' => $contact->id,
'field_id' => $defaultsIdFields[$j],
'isDefault' => 1,
'value' => $defaultsValuesFields[$j]]);
}
for ($k = 0; $k < $customSize; $k++) {
$customValuesFields = $this->matrixCustomFields[$i][1]; //field value
CompanyListFieldValues::create(['company_id' => $this->idCompany,
'contact_id' => $contact->id,
'field_id' => $customFields[$k],
'isDefault' => 0,
'value' => $customValuesFields[$j]]);
}
}
}
来自工作生成器的代码(这个工作正常)
for ($row = $this->startFrom; $row <= $highestRow; $row++) {
if ($rowMatrix == $chunksize) {
// $job = (new importExcelInsert($matrixContacts, $matrixDefaultFields, $matrixCustomFields, $this->idCompany))->delay($jobDelay)->onQueue('excelInserts');
// var_dump($matrixContacts);
$job = (new importExcelInsert($matrixContacts, $matrixDefaultFields, $matrixCustomFields, $this->idCompany, $rowMatrix))->delay($jobDelay)->onQueue('excelInserts');
$this->dispatch($job);
$njobs++;
$matrixContacts = [];
$matrixDefaultFields = [];
$matrixCustomFields = [];
$rowMatrix = 0;
}
$userEmail = $sheet->getCellByColumnAndRow($posEmail, $row)->getValue();
if ($userEmail == '' || !filter_var($userEmail, FILTER_VALIDATE_EMAIL)) {
$error++;
} else if (Contact::where('email', '=', $userEmail)->where('id_contact_list', '=', $this->idList)->count()) {
$ignored++;
} else {
// $uuid = Uuid::generate(4);
$matrixContacts[$rowMatrix][1] = $this->idList; //CONTACT_LIST_ID
$matrixContacts[$rowMatrix][2] = $sheet->getCellByColumnAndRow($posNome, $row)->getValue(); //Name
$matrixContacts[$rowMatrix][3] = $userEmail; //Email
$matrixContacts[$rowMatrix][4] = $unsubCode; //Unsubscribe Code
$vecFieldId = [];
$vecValues = [];
$i = 0;
if (!$this->arrayDefaultFields[0] == null) {
for ($j = 0; $j < sizeof($this->arrayDefaultFields); $j += 2) {
$vecFieldId[$i] = $this->arrayDefaultFields[$j];
$vecValues[$i] = $sheet->getCellByColumnAndRow($this->arrayDefaultFields[$j + 1], $row)->getValue();
$i++;
}
$matrixDefaultFields[$rowMatrix][0] = $vecFieldId;
$matrixDefaultFields[$rowMatrix][1] = $vecValues;
}
$i = 0;
if (!$this->arrayCustomFields[0] == null) {
for ($j = 0; $j < sizeof($this->arrayCustomFields); $j += 2) {
$vecFieldId[$i] = $this->arrayCustomFields[$j];
$vecValues[$i] = $sheet->getCellByColumnAndRow($this->arrayCustomFields[$j + 1], $row)->getValue();
$i++;
}
$matrixCustomFields[$rowMatrix][0] = $vecFieldId;
$matrixCustomFields[$rowMatrix][1] = $vecValues;
}
$rowMatrix++;
}
}
if (!empty($matrixContacts)) {
$job = (new importExcelInsert($matrixContacts, $matrixDefaultFields, $matrixCustomFields, $this->idCompany, $rowMatrix + 1))->delay($jobDelay)->onQueue('excelInserts');
$this->dispatch($job);
$njobs++;
}