| @@ -31,11 +31,11 @@ | ||
| 31 | 31 | $CheckHttpBlockService |
| 32 | 32 | ) { |
| 33 | 33 | add_action('wp_ajax_' . self::ASYNC_ACTION, [$this, 'handleAsyncRequest']); |
| 34 | 34 | add_action('wp_ajax_nopriv_' . self::ASYNC_ACTION, [$this, 'handleAsyncRequest']); |
| 35 | - // Keep database work out of shutdown, where another callback may have | |
| 36 | - // left the shared mysqli connection with an unread result set. | |
| 37 | - add_action('init', [$this, 'maybeDispatchAsyncRequest'], 10, 0); | |
| 35 | + // Probe and dispatch after the response path so normal storefront | |
| 36 | + // requests never pay for the queue query or loopback HTTP request. | |
| 37 | + add_action('shutdown', [$this, 'maybeDispatchAsyncRequest'], PHP_INT_MAX); | |
| 38 | 38 | add_action('sync_basalam_job_created', [$this, 'maybeDispatchAsyncRequest'], 10, 0); |
| 39 | 39 | |
| 40 | 40 | $this->jobManager = $jobManager; |
| 41 | 41 | $this->jobExecutor = $jobExecutor; |
| @@ -44,24 +44,24 @@ | ||
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | public function maybeDispatchAsyncRequest(): void |
| 47 | 47 | { |
| 48 | - // Retain a guard for third-party callers that may still invoke this | |
| 49 | - // method during shutdown even though no shutdown hook is registered. | |
| 50 | - if (function_exists('did_action') && did_action('shutdown')) return; | |
| 51 | 48 | if ($this->isCurrentAsyncRequest()) return; |
| 52 | 49 | if ($this->CheckHttpBlockService->SyncBasalamHttpBlock()) return; |
| 53 | 50 | if (get_transient(self::ASYNC_DISPATCH_LOCK_TRANSIENT)) return; |
| 54 | 51 | |
| 55 | - if (!$this->jobManager->hasPendingOrStaleProcessingJobs(self::STALE_PROCESSING_TIMEOUT_SECONDS)) { | |
| 56 | - return; | |
| 57 | - } | |
| 58 | - | |
| 52 | + // Reserve the dispatch lease before probing the queue. This keeps | |
| 53 | + // concurrent shutdown callbacks from all running the queue query and | |
| 54 | + // dispatching duplicate async workers. | |
| 59 | 55 | set_transient( |
| 60 | 56 | self::ASYNC_DISPATCH_LOCK_TRANSIENT, |
| 61 | 57 | 1, |
| 62 | 58 | self::ASYNC_DISPATCH_LOCK_SECONDS |
| 63 | 59 | ); |
| 60 | + | |
| 61 | + if (!$this->jobManager->hasPendingOrStaleProcessingJobs(self::STALE_PROCESSING_TIMEOUT_SECONDS)) { | |
| 62 | + return; | |
| 63 | + } | |
| 64 | 64 | |
| 65 | 65 | $this->dispatchAsyncRequest(); |
| 66 | 66 | } |
| 67 | 67 | |