|
13 | 13 | use Illuminate\Queue\Events\JobProcessed; |
14 | 14 | use Illuminate\Queue\Events\JobProcessing; |
15 | 15 | use Illuminate\Queue\Events\WorkerStopping; |
| 16 | +use Illuminate\Queue\QueueManager; |
16 | 17 | use Illuminate\Routing\Events\RouteMatched; |
17 | 18 | use Illuminate\Routing\Route; |
18 | 19 | use Illuminate\Support\Str; |
@@ -98,6 +99,13 @@ class EventHandler |
98 | 99 | */ |
99 | 100 | private $recordQueueInfo; |
100 | 101 |
|
| 102 | + /** |
| 103 | + * Indicates if we pushed a scope for the queue. |
| 104 | + * |
| 105 | + * @var bool |
| 106 | + */ |
| 107 | + private $pushedQueueScope = false; |
| 108 | + |
101 | 109 | /** |
102 | 110 | * EventHandler constructor. |
103 | 111 | * |
@@ -135,9 +143,16 @@ public function subscribeAuthEvents() |
135 | 143 |
|
136 | 144 | /** |
137 | 145 | * Attach all queue event handlers. |
| 146 | + * |
| 147 | + * @param \Illuminate\Queue\QueueManager $queue |
138 | 148 | */ |
139 | | - public function subscribeQueueEvents() |
| 149 | + public function subscribeQueueEvents(QueueManager $queue) |
140 | 150 | { |
| 151 | + $queue->looping(function () { |
| 152 | + $this->cleanupScopeForQueuedJob(); |
| 153 | + $this->afterQueuedJob(); |
| 154 | + }); |
| 155 | + |
141 | 156 | foreach (static::$queueEventHandlerMap as $eventName => $handler) { |
142 | 157 | $this->events->listen($eventName, [$this, $handler]); |
143 | 158 | } |
@@ -365,7 +380,7 @@ protected function authenticatedHandler(Authenticated $event) |
365 | 380 | */ |
366 | 381 | protected function queueJobProcessingHandler(JobProcessing $event) |
367 | 382 | { |
368 | | - $this->beforeQueuedJob(); |
| 383 | + $this->prepareScopeForQueuedJob(); |
369 | 384 |
|
370 | 385 | if (!$this->recordQueueInfo) { |
371 | 386 | return; |
@@ -478,18 +493,34 @@ protected function commandFinishedHandler(CommandFinished $event) |
478 | 493 | Integration::flushEvents(); |
479 | 494 | } |
480 | 495 |
|
481 | | - private function beforeQueuedJob() |
| 496 | + private function afterQueuedJob(): void |
| 497 | + { |
| 498 | + // Flush any and all events that were possibly generated by queue jobs |
| 499 | + Integration::flushEvents(); |
| 500 | + } |
| 501 | + |
| 502 | + private function prepareScopeForQueuedJob(): void |
482 | 503 | { |
483 | | - // When a job starts, we want to push a new scope |
| 504 | + $this->cleanupScopeForQueuedJob(); |
| 505 | + |
484 | 506 | SentrySdk::getCurrentHub()->pushScope(); |
| 507 | + |
| 508 | + $this->pushedQueueScope = true; |
| 509 | + |
| 510 | + // When a job starts, we want to make sure the scope is cleared of breadcrumbs |
| 511 | + SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) { |
| 512 | + $scope->clearBreadcrumbs(); |
| 513 | + }); |
485 | 514 | } |
486 | 515 |
|
487 | | - private function afterQueuedJob() |
| 516 | + private function cleanupScopeForQueuedJob(): void |
488 | 517 | { |
489 | | - // Flush any and all events that were possibly generated by queue jobs |
490 | | - Integration::flushEvents(); |
| 518 | + if (!$this->pushedQueueScope) { |
| 519 | + return; |
| 520 | + } |
491 | 521 |
|
492 | | - // We have added a scope when the job started processing |
493 | 522 | SentrySdk::getCurrentHub()->popScope(); |
| 523 | + |
| 524 | + $this->pushedQueueScope = false; |
494 | 525 | } |
495 | 526 | } |
0 commit comments