228 lines
7.0 KiB
PHP
228 lines
7.0 KiB
PHP
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Foundation\Bus\Dispatchable;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Illuminate\Queue\SerializesModels;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
class ExecuteSaveTriggerJob implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
/**
|
||
* The number of seconds the job can run before timing out.
|
||
*
|
||
* @var int
|
||
*/
|
||
public $timeout = 600; // 10 minutes
|
||
|
||
/**
|
||
* The number of times the job may be attempted.
|
||
*
|
||
* @var int
|
||
*/
|
||
public $tries = 1; // Only try once, no retry on failure
|
||
|
||
/**
|
||
* The maximum number of unhandled exceptions to allow before failing.
|
||
*
|
||
* @var int
|
||
*/
|
||
public $maxExceptions = 1;
|
||
|
||
protected $tableName;
|
||
protected $requestData;
|
||
protected $key;
|
||
protected $values;
|
||
protected $beforeData;
|
||
protected $action;
|
||
|
||
/**
|
||
* Create a new job instance.
|
||
*/
|
||
public function __construct(string $tableName, array $requestData, $key = null, $values = null, $beforeData = null, $action = null)
|
||
{
|
||
$this->tableName = $tableName;
|
||
$this->requestData = $requestData;
|
||
$this->key = $key;
|
||
$this->values = $values;
|
||
$this->beforeData = $beforeData;
|
||
$this->action = $action;
|
||
|
||
// Determine queue priority based on table and operation
|
||
$queueName = $this->determineQueuePriority($tableName, $values);
|
||
|
||
// Set the queue for this job
|
||
$this->onQueue($queueName);
|
||
}
|
||
|
||
/**
|
||
* Determine the queue priority based on table and data
|
||
* Material group operations get higher priority
|
||
*/
|
||
private function determineQueuePriority(string $tableName, $values): string
|
||
{
|
||
// High priority for material group related operations in weld_logs
|
||
if ($tableName === 'weld_logs' && $values) {
|
||
$materialGroupFields = [
|
||
'steel_grade_p',
|
||
'steel_grade_b',
|
||
'material_group_p',
|
||
'material_group_b',
|
||
'ru_material_group_1',
|
||
'ru_material_group_2'
|
||
];
|
||
|
||
// Check if any material group field is being updated
|
||
foreach ($materialGroupFields as $field) {
|
||
if (isset($values[$field])) {
|
||
Log::info("High priority queue assigned for material group operation", [
|
||
'table' => $tableName,
|
||
'field' => $field,
|
||
'queue' => 'high-priority-save-trigger'
|
||
]);
|
||
return 'high-priority-save-trigger';
|
||
}
|
||
}
|
||
}
|
||
|
||
// Default priority
|
||
return 'save-trigger';
|
||
}
|
||
|
||
/**
|
||
* Execute the job.
|
||
*/
|
||
public function handle(): void
|
||
{
|
||
$startTime = microtime(true);
|
||
|
||
Log::info("=== ExecuteSaveTriggerJob STARTED ===", [
|
||
'table_name' => $this->tableName,
|
||
'key' => $this->key,
|
||
'action' => $this->action,
|
||
'timestamp' => now(),
|
||
'memory_start' => memory_get_usage(true) / 1024 / 1024 . ' MB'
|
||
]);
|
||
|
||
$path = app_path("Http/Controllers/SaveTrigger/{$this->tableName}.php");
|
||
|
||
if (file_exists($path)) {
|
||
|
||
// Job içinde değişkenleri global olarak erişilebilir hale getir
|
||
$tableName = $this->tableName;
|
||
$request = $this->requestData;
|
||
$key = $this->key;
|
||
$values = $this->values;
|
||
$beforeData = $this->beforeData;
|
||
$action = $this->action;
|
||
|
||
Log::info("SaveTrigger file found, executing...", [
|
||
'path' => $path,
|
||
'table' => $tableName
|
||
]);
|
||
|
||
// Include dosyasını çalıştır
|
||
include($path);
|
||
|
||
} else {
|
||
Log::warning("SaveTrigger dosyası bulunamadı", [
|
||
'expected_path' => $path,
|
||
'table_name' => $this->tableName
|
||
]);
|
||
}
|
||
|
||
// Dispatch report generation if applicable
|
||
$this->dispatchReportGeneration();
|
||
|
||
$endTime = microtime(true);
|
||
$executionTime = $endTime - $startTime;
|
||
$memoryUsage = memory_get_peak_usage(true) / 1024 / 1024;
|
||
|
||
Log::info("=== ExecuteSaveTriggerJob COMPLETED ===", [
|
||
'table_name' => $this->tableName,
|
||
'key' => $this->key,
|
||
'execution_time_seconds' => round($executionTime, 2),
|
||
'memory_peak_mb' => round($memoryUsage, 2),
|
||
'timestamp' => now()
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* Check and dispatch report generation for relevant templates
|
||
*/
|
||
protected function dispatchReportGeneration()
|
||
{
|
||
try {
|
||
// Only proceed if we have a valid key (ID)
|
||
$recordId = $this->key['id'] ?? null;
|
||
if (!$recordId) {
|
||
return;
|
||
}
|
||
|
||
// Find templates that might be related to this table
|
||
$templates = \Illuminate\Support\Facades\DB::table("document_templates")
|
||
->where("y", "1")
|
||
->get();
|
||
|
||
foreach ($templates as $template) {
|
||
$fields = json_decode($template->fields);
|
||
|
||
if (!isset($fields->editorMaster)) {
|
||
continue;
|
||
}
|
||
|
||
$masterSql = $fields->editorMaster;
|
||
|
||
// Check conditions:
|
||
// 1. Contains tableName
|
||
// 2. Contains ORDER BY (case insensitive)
|
||
if (stripos($masterSql, $this->tableName) !== false && stripos($masterSql, 'ORDER BY') !== false) {
|
||
|
||
// Clone işleminde rapor oluşturmayı engelle
|
||
if($this->action === "clone") {
|
||
continue;
|
||
}
|
||
|
||
Log::debug("Dispatching RenderReportJob for template", [
|
||
'template_id' => $template->id,
|
||
'table' => $this->tableName,
|
||
'record_id' => $recordId
|
||
]);
|
||
|
||
\App\Jobs\RenderReportJob::dispatch($template->id, $recordId, $this->tableName);
|
||
}
|
||
}
|
||
|
||
} catch (\Exception $e) {
|
||
Log::error("Error in dispatchReportGeneration: " . $e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Get the job's description for Telescope.
|
||
*/
|
||
public function getDescription(): string
|
||
{
|
||
$action = $this->key ? 'Update' : 'Create';
|
||
return "Execute Save Trigger for {$this->tableName} table - {$action} operation";
|
||
}
|
||
|
||
/**
|
||
* Get the tags that should be assigned to the job.
|
||
*/
|
||
public function tags(): array
|
||
{
|
||
$action = $this->action;
|
||
return [
|
||
"SaveTrigger:{$this->tableName}",
|
||
"Action:{$action}",
|
||
"Table:{$this->tableName}"
|
||
];
|
||
}
|
||
}
|