|
| 1 | +<?php |
| 2 | +namespace App\Listeners; |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2026 OpenStack Foundation |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +use App\Audit\AuditContext; |
| 18 | +use Illuminate\Queue\Events\JobProcessing; |
| 19 | +use Illuminate\Support\Facades\Log; |
| 20 | + |
| 21 | +class RestoreJobAuditContextListener |
| 22 | +{ |
| 23 | + private const PAYLOAD_DATA_KEY = 'data'; |
| 24 | + private const PAYLOAD_CONTEXT_KEY = 'auditContext'; |
| 25 | + private const LOG_CONTEXT_KEY = 'event_name'; |
| 26 | + private const LOG_CONTEXT_VALUE = 'job.processing'; |
| 27 | + |
| 28 | + public function handle(JobProcessing $event): void |
| 29 | + { |
| 30 | + if (!$this->isOpenTelemetryEnabled()) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + $context = $this->extractContextFromPayload($event->job->payload()); |
| 36 | + |
| 37 | + if ($context !== null) { |
| 38 | + app()->instance(AuditContext::CONTAINER_KEY, $context); |
| 39 | + } |
| 40 | + } catch (\Exception $e) { |
| 41 | + Log::warning('RestoreJobAuditContextListener::handle Failed to restore audit context from queue job', [ |
| 42 | + self::LOG_CONTEXT_KEY => self::LOG_CONTEXT_VALUE, |
| 43 | + 'exception_message' => $e->getMessage(), |
| 44 | + 'exception_class' => get_class($e), |
| 45 | + ]); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private function isOpenTelemetryEnabled(): bool |
| 50 | + { |
| 51 | + return config('opentelemetry.enabled', false); |
| 52 | + } |
| 53 | + |
| 54 | + private function extractContextFromPayload(array $payload): ?AuditContext |
| 55 | + { |
| 56 | + if (!isset($payload[self::PAYLOAD_DATA_KEY][self::PAYLOAD_CONTEXT_KEY])) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + $context = unserialize( |
| 62 | + $payload[self::PAYLOAD_DATA_KEY][self::PAYLOAD_CONTEXT_KEY], |
| 63 | + ['allowed_classes' => [AuditContext::class]] |
| 64 | + ); |
| 65 | + |
| 66 | + if (!$context instanceof AuditContext) { |
| 67 | + Log::warning('RestoreJobAuditContextListener::extractContextFromPayload Invalid audit context type in job payload', [ |
| 68 | + self::LOG_CONTEXT_KEY => self::LOG_CONTEXT_VALUE, |
| 69 | + 'actual_type' => gettype($context), |
| 70 | + ]); |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + return $context; |
| 75 | + } catch (\Exception $e) { |
| 76 | + Log::warning('RestoreJobAuditContextListener::extractContextFromPayload Failed to unserialize audit context from job payload', [ |
| 77 | + self::LOG_CONTEXT_KEY => self::LOG_CONTEXT_VALUE, |
| 78 | + 'exception_message' => $e->getMessage(), |
| 79 | + ]); |
| 80 | + return null; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments