HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
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,
        ]);
    }
}