| @@ -23,21 +23,27 @@ | ||
| 23 | 23 | public function createJob($jobType, $status = 'pending', $payload = null, $maxAttempts = 3) |
| 24 | 24 | { |
| 25 | 25 | global $wpdb; |
| 26 | 26 | |
| 27 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table; no object cache for these operational queries. | |
| 28 | - return $wpdb->insert( | |
| 29 | - $this->jobManagerTableName, | |
| 30 | - array( | |
| 31 | - 'job_type' => $jobType, | |
| 32 | - 'status' => $status, | |
| 27 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table; no object cache for these operational queries. | |
| 28 | + $result = $wpdb->insert( | |
| 29 | + $this->jobManagerTableName, | |
| 30 | + array( | |
| 31 | + 'job_type' => $jobType, | |
| 32 | + 'status' => $status, | |
| 33 | 33 | 'payload' => $payload, |
| 34 | 34 | 'attempts' => 0, |
| 35 | 35 | 'max_attempts' => $maxAttempts, |
| 36 | - 'created_at' => time(), | |
| 37 | - ) | |
| 38 | - ); | |
| 39 | - } | |
| 36 | + 'created_at' => time(), | |
| 37 | + ) | |
| 38 | + ); | |
| 39 | + | |
| 40 | + if ($result !== false) { | |
| 41 | + do_action('sync_basalam_job_created', $jobType, $status, $payload); | |
| 42 | + } | |
| 43 | + | |
| 44 | + return $result; | |
| 45 | + } | |
| 40 | 46 | |
| 41 | 47 | public function getNextEligibleJob(string $jobType): ?object |
| 42 | 48 | { |
| 43 | 49 | global $wpdb; |
| @@ -58,10 +64,55 @@ | ||
| 58 | 64 | public function hasAnyProcessingJob(): bool |
| 59 | 65 | { |
| 60 | 66 | return $this->getCountJobs(['status' => 'processing']) > 0; |
| 61 | 67 | } |
| 62 | - | |
| 63 | - public function getJob($where = array()) | |
| 68 | + | |
| 69 | + public function getProductUpdateStatus(): array | |
| 70 | + { | |
| 71 | + $quickJob = $this->getJob(['job_type' => 'sync_basalam_bulk_update_products', 'status' => 'processing']) | |
| 72 | + ?: $this->getJob(['job_type' => 'sync_basalam_bulk_update_products', 'status' => 'pending']); | |
| 73 | + $fullJob = $this->getJob(['job_type' => 'sync_basalam_update_all_products', 'status' => 'processing']) | |
| 74 | + ?: $this->getJob(['job_type' => 'sync_basalam_update_all_products', 'status' => 'pending']); | |
| 75 | + $singleCount = $this->getCountJobs([ | |
| 76 | + 'job_type' => 'sync_basalam_update_single_product', | |
| 77 | + 'status' => ['pending', 'processing'], | |
| 78 | + ]); | |
| 79 | + | |
| 80 | + return [ | |
| 81 | + 'active' => (bool) ($quickJob || $fullJob || $singleCount > 0), | |
| 82 | + 'type' => $quickJob ? 'quick' : (($fullJob || $singleCount > 0) ? 'full' : ''), | |
| 83 | + 'status' => $quickJob ? $quickJob->status : ($fullJob ? $fullJob->status : ''), | |
| 84 | + 'count' => $singleCount, | |
| 85 | + ]; | |
| 86 | + } | |
| 87 | + | |
| 88 | + public function hasPendingOrStaleProcessingJobs(int $staleProcessingTimeoutSeconds = 120): bool | |
| 89 | + { | |
| 90 | + global $wpdb; | |
| 91 | + | |
| 92 | + $now = time(); | |
| 93 | + $staleBefore = $now - $staleProcessingTimeoutSeconds; | |
| 94 | + | |
| 95 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom plugin table; identifier from $wpdb->prefix, not user input. | |
| 96 | + $result = $wpdb->get_var($wpdb->prepare( | |
| 97 | + "SELECT 1 FROM {$this->jobManagerTableName} | |
| 98 | + WHERE ( | |
| 99 | + status = 'pending' | |
| 100 | + AND (retry_after IS NULL OR retry_after <= %d) | |
| 101 | + ) OR ( | |
| 102 | + status = 'processing' | |
| 103 | + AND started_at IS NOT NULL | |
| 104 | + AND started_at < %d | |
| 105 | + ) | |
| 106 | + LIMIT 1", | |
| 107 | + $now, | |
| 108 | + $staleBefore | |
| 109 | + )); | |
| 110 | + | |
| 111 | + return (string) $result === '1'; | |
| 112 | + } | |
| 113 | + | |
| 114 | + public function getJob($where = array()) | |
| 64 | 115 | { |
| 65 | 116 | global $wpdb; |
| 66 | 117 | |
| 67 | 118 | if (empty($where)) return null; |