| @@ -65,8 +65,27 @@ | ||
| 65 | 65 | { |
| 66 | 66 | return $this->getCountJobs(['status' => 'processing']) > 0; |
| 67 | 67 | } |
| 68 | 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 | + | |
| 69 | 88 | public function hasPendingOrStaleProcessingJobs(int $staleProcessingTimeoutSeconds = 120): bool |
| 70 | 89 | { |
| 71 | 90 | global $wpdb; |
| 72 | 91 | |
| @@ -89,8 +108,23 @@ | ||
| 89 | 108 | $staleBefore |
| 90 | 109 | )); |
| 91 | 110 | |
| 92 | 111 | return (string) $result === '1'; |
| 112 | + } | |
| 113 | + | |
| 114 | + public function getStaleProcessingJob(int $timeoutSeconds = 120): ?object | |
| 115 | + { | |
| 116 | + global $wpdb; | |
| 117 | + | |
| 118 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom queue table; identifier from the WordPress prefix, values prepared. | |
| 119 | + return $wpdb->get_row($wpdb->prepare( | |
| 120 | + "SELECT id, started_at FROM {$this->jobManagerTableName} | |
| 121 | + WHERE status = 'processing' | |
| 122 | + AND started_at IS NOT NULL | |
| 123 | + AND started_at < %d | |
| 124 | + ORDER BY started_at ASC, id ASC LIMIT 1", | |
| 125 | + time() - $timeoutSeconds | |
| 126 | + )); | |
| 93 | 127 | } |
| 94 | 128 | |
| 95 | 129 | public function getJob($where = array()) |
| 96 | 130 | { |