| @@ -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,36 @@ | ||
| 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 hasPendingOrStaleProcessingJobs(int $staleProcessingTimeoutSeconds = 120): bool | |
| 70 | + { | |
| 71 | + global $wpdb; | |
| 72 | + | |
| 73 | + $now = time(); | |
| 74 | + $staleBefore = $now - $staleProcessingTimeoutSeconds; | |
| 75 | + | |
| 76 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom plugin table; identifier from $wpdb->prefix, not user input. | |
| 77 | + $result = $wpdb->get_var($wpdb->prepare( | |
| 78 | + "SELECT 1 FROM {$this->jobManagerTableName} | |
| 79 | + WHERE ( | |
| 80 | + status = 'pending' | |
| 81 | + AND (retry_after IS NULL OR retry_after <= %d) | |
| 82 | + ) OR ( | |
| 83 | + status = 'processing' | |
| 84 | + AND started_at IS NOT NULL | |
| 85 | + AND started_at < %d | |
| 86 | + ) | |
| 87 | + LIMIT 1", | |
| 88 | + $now, | |
| 89 | + $staleBefore | |
| 90 | + )); | |
| 91 | + | |
| 92 | + return (string) $result === '1'; | |
| 93 | + } | |
| 94 | + | |
| 95 | + public function getJob($where = array()) | |
| 64 | 96 | { |
| 65 | 97 | global $wpdb; |
| 66 | 98 | |
| 67 | 99 | if (empty($where)) return null; |