161 lines
6.2 KiB
PHP
161 lines
6.2 KiB
PHP
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||
use Illuminate\Foundation\Bus\Dispatchable;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Illuminate\Queue\SerializesModels;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
/**
|
||
* Batch Excel'den yapılan toplu işlemler sonrası tüm trigger'ları
|
||
* tek bir job içinde çalıştırır. Her kayıt için ayrı job oluşturulur.
|
||
* Tekrarlı aynı veride sadece bir kez çalışır (ShouldBeUnique).
|
||
*/
|
||
class ExecuteBatchExcelTriggersJob implements ShouldQueue, ShouldBeUnique
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
public $timeout = 1800; // 30 minutes for large batches
|
||
public $tries = 1;
|
||
public $maxExceptions = 1;
|
||
public $uniqueFor = 1800; // Kilit süresi (30 dk)
|
||
|
||
protected $tableName;
|
||
protected $ids;
|
||
protected $requestData;
|
||
protected $changedColumns;
|
||
|
||
/**
|
||
* @param string $tableName Table name
|
||
* @param array $ids Array of ['id' => X] records
|
||
* @param array $requestData Cached request data
|
||
*/
|
||
public function __construct(string $tableName, array $ids, array $requestData, array $changedColumns = [])
|
||
{
|
||
$this->tableName = $tableName;
|
||
$this->ids = $ids;
|
||
$this->requestData = $requestData;
|
||
$this->changedColumns = $changedColumns;
|
||
|
||
$this->onQueue('save-trigger');
|
||
}
|
||
|
||
public function uniqueId()
|
||
{
|
||
// Chunk'taki tüm ID'lerin hash'ini kullan (chunk çakışmasını önle)
|
||
$idList = array_map(fn($item) => $item['id'] ?? 0, $this->ids);
|
||
sort($idList);
|
||
return $this->tableName . '_' . md5(implode(',', $idList));
|
||
}
|
||
|
||
public function handle(): void
|
||
{
|
||
$startTime = microtime(true);
|
||
$totalIds = count($this->ids);
|
||
|
||
Log::info("=== ExecuteBatchExcelTriggersJob STARTED ===", [
|
||
'table_name' => $this->tableName,
|
||
'total_ids' => $totalIds,
|
||
]);
|
||
|
||
if (!\Illuminate\Support\Facades\Schema::hasTable($this->tableName)) {
|
||
Log::critical("ExecuteBatchExcelTriggersJob FATAL ERROR: Table '{$this->tableName}' does not exist. ExecuteBatchExcelTriggersJob cannot proceed.", [
|
||
'table_name' => $this->tableName,
|
||
'ids_count' => count($this->ids)
|
||
]);
|
||
return;
|
||
}
|
||
|
||
$isLogTestType = in_array($this->tableName, log_test_types());
|
||
$isWeldLogs = ($this->tableName == "weld_logs");
|
||
|
||
foreach ($this->ids as $index => $recordIdData) {
|
||
try {
|
||
$recordId = (int) ($recordIdData['id'] ?? 0);
|
||
if ($recordId <= 0) {
|
||
Log::warning("ExecuteBatchExcelTriggersJob: Invalid record ID", ['data' => $recordIdData]);
|
||
continue;
|
||
}
|
||
|
||
Log::info("ExecuteBatchExcelTriggersJob: Start processing ID: {$recordId}");
|
||
$recordData = db($this->tableName)->where('id', $recordId)->first();
|
||
|
||
if (!$recordData) {
|
||
Log::info("ExecuteBatchExcelTriggersJob: ID {$recordId} not found in table '{$this->tableName}', skipping.");
|
||
continue;
|
||
}
|
||
|
||
// 1. log_test_types: logToWeldlogUpdate + logToWPQUpdate
|
||
if ($isLogTestType) {
|
||
Log::info("ExecuteBatchExcelTriggersJob: Running logToWeldlogUpdate...");
|
||
$triggerData = (array) $recordData;
|
||
if (function_exists('logToWeldlogUpdate')) {
|
||
logToWeldlogUpdate($triggerData);
|
||
}
|
||
Log::info("ExecuteBatchExcelTriggersJob: Running logToWPQUpdate...");
|
||
if (function_exists('logToWPQUpdate')) {
|
||
logToWPQUpdate($triggerData, $this->tableName);
|
||
}
|
||
}
|
||
|
||
// 2. weld_logs: materialGroupUpdater
|
||
if ($isWeldLogs && function_exists('materialGroupUpdater')) {
|
||
Log::info("ExecuteBatchExcelTriggersJob: Running materialGroupUpdater...");
|
||
materialGroupUpdater($recordId);
|
||
}
|
||
|
||
// 3. SaveTrigger dosyasını çalıştır
|
||
Log::info("ExecuteBatchExcelTriggersJob: Running SaveTrigger ({$this->tableName})...");
|
||
// include edilen dosya kendi global state'ini bozmasın diye taze bir scope kuralım (anonim fonksiyon ile de yapılabilirdi ama require/include olduğu için değişkenleri manuel veriyoruz)
|
||
$tableName = $this->tableName;
|
||
$request = is_array($this->requestData) ? $this->requestData : [];
|
||
$request['key'] = $recordIdData;
|
||
$key = $recordIdData;
|
||
$values = null;
|
||
$beforeData = null; // Batch Excel'de gerçek beforeData yok
|
||
$changedColumns = $this->changedColumns; // Hangi sütunlar Excel'den güncellendi
|
||
$action = "update";
|
||
|
||
$path = app_path("Http/Controllers/SaveTrigger/{$this->tableName}.php");
|
||
if (file_exists($path)) {
|
||
include($path);
|
||
}
|
||
Log::info("ExecuteBatchExcelTriggersJob: SaveTrigger done.");
|
||
|
||
} catch (\Throwable $th) {
|
||
Log::error("ExecuteBatchExcelTriggersJob loop error", [
|
||
'table_name' => $this->tableName,
|
||
'record_id' => $recordIdData['id'] ?? null,
|
||
'error' => $th->getMessage(),
|
||
'trace' => $th->getTraceAsString(),
|
||
]);
|
||
// Diğer kayıtlara devam et
|
||
continue;
|
||
}
|
||
}
|
||
|
||
$executionTime = microtime(true) - $startTime;
|
||
|
||
Log::info("=== ExecuteBatchExcelTriggersJob COMPLETED ===", [
|
||
'table_name' => $this->tableName,
|
||
'total_ids' => $totalIds,
|
||
'execution_time_seconds' => round($executionTime, 2),
|
||
]);
|
||
}
|
||
|
||
public function tags(): array
|
||
{
|
||
$tableName = $this->tableName ?? 'unknown';
|
||
$idsCount = is_array($this->ids) ? count($this->ids) : 0;
|
||
|
||
return [
|
||
"BatchExcelTriggers:{$tableName}",
|
||
"Count:{$idsCount}",
|
||
];
|
||
}
|
||
}
|