File: /var/www/html/laravel/app/Jobs/ProcessNotamJob.php
<?php
namespace App\Jobs;
use App\Models\Notam;
use App\Models\Prompt;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class ProcessNotamJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue;
public $type;
public $id;
/**
* Create a new job instance.
*/
public function __construct($type, $id)
{
$this->type = $type;
$this->id = $id;
}
public function handle()
{
try {
if (empty($this->type)) {
$this->fail('Type is required');
}
// Temporary disabling Local model processing
//$localPrompts = Prompt::where('model', 'like', 'local')->pluck('id')->toArray();
// if (!$localPrompts) {
$localPrompts = [];
// }
$additionalPrompts = Prompt::where('model', 'like', '%4.1-mini%')->pluck('id')->toArray();
switch ($this->type) {
case 'notam':
/** @var Notam $object */
$object = Notam::find($this->id);
if ($object) {
if ($object->chatgpt_response) {
if (!empty($localPrompts)) {
if (!$object->local_response || substr_count($object->local_response, 'process in progress')) {
$object->local_output = $object->local_response = $object->local_correct_response = json_encode([
'status' => Notam::IN_PROGRESS_STATUS,
]);
$object->saveQuietly();
$object->prompt_id = $localPrompts[0];
$processResponse = $object->processNotamFromQueue();
if ($processResponse !== true) {
$this->fail(json_encode($processResponse));
return;
}
}
}
if (!empty($additionalPrompts)) {
if (!$object->gpt41_response || substr_count($object->gpt41_response, 'In progress')) {
echo 'processing for 4.1';
$object->gpt41_output = $object->gpt41_response = $object->gpt41_correct_response = json_encode([
'status' => Notam::IN_PROGRESS_STATUS,
]);
$object->saveQuietly();
$object->prompt_id = $additionalPrompts[0];
$object->processNotamFromQueue();
}
}
} elseif (!in_array($object->prompt_id, $localPrompts)) {
$processResponse = $object->processNotamFromQueue(); // Processing by GPT
if ($processResponse !== true) {
$object->comments = json_encode($processResponse);
$object->saveQuietly();
$this->fail(json_encode($processResponse));
return;
}
// Temporary disabling Local model processing
// if (!empty($localPrompts)) {
// $object->local_output = $object->local_response = $object->local_correct_response = json_encode([
// 'status' => Notam::IN_PROGRESS_STATUS,
// ]);
//
// $object->saveQuietly();
// $object->prompt_id = $localPrompts[0]; // not updating promt_id in database, just for processing by local LLM
// $processResponse = $object->processNotamFromQueue();
//
// if ($processResponse !== true) {
// $object->comments = json_encode($processResponse);
// $object->saveQuietly();
// $this->fail(json_encode($processResponse));
// return;
// }
// }
if (!empty($additionalPrompts)) {
$object->gpt41_output = $object->gpt41_response = $object->gpt41_correct_response = json_encode([
'status' => Notam::IN_PROGRESS_STATUS,
]);
$object->saveQuietly();
$object->prompt_id = $additionalPrompts[0]; // not updating promt_id in database, just for processing by additional prompt
$processResponse = $object->processNotamFromQueue();
if ($processResponse !== true) {
$object->comments = json_encode($processResponse);
$object->saveQuietly();
$this->fail(json_encode($processResponse));
return;
}
}
}
}
break;
default:
$this->fail('Unknown type: ' . $this->type);
}
} catch (\Throwable $e) {
// mark job as failed immediately and trigger failed()
$this->fail($e->getMessage());
}
}
public function failed($message)
{
// runs when the job is failed (log, notify, revert state, etc.)
\Log::error('ProcessNotamJob failed', [
'id' => $this->id,
'type' => $this->type,
'error' => $message,
]);
}
}