| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Jobs; |
| 4 |
|
| 5 |
use SyncBasalam\JobManager; |
| 6 |
use SyncBasalam\Jobs\Exceptions\JobException; |
| 7 |
use SyncBasalam\Services\Api\CircuitBreakerOpenException; |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
class JobExecutor |
| 12 |
{ |
| 13 |
private $jobManager; |
| 14 |
private $lockManager; |
| 15 |
private $jobRegistry; |
| 16 |
|
| 17 |
public function __construct(JobManager $jobManager, LockManager $lockManager, JobRegistry $jobRegistry) |
| 18 |
{ |
| 19 |
$this->jobManager = $jobManager; |
| 20 |
$this->lockManager = $lockManager; |
| 21 |
$this->jobRegistry = $jobRegistry; |
| 22 |
} |
| 23 |
|
| 24 |
public function execute(string $jobType, object $job): bool |
| 25 |
{ |
| 26 |
$jobExecutor = $this->jobRegistry->get($jobType); |
| 27 |
|
| 28 |
if (!$jobExecutor) return false; |
| 29 |
|
| 30 |
$payload = json_decode($job->payload, true); |
| 31 |
|
| 32 |
if (!is_array($payload)) { |
| 33 |
$payload = $this->normalizeLegacyPayload($jobType, $payload); |
| 34 |
} |
| 35 |
|
| 36 |
try { |
| 37 |
$result = $jobExecutor->execute($payload); |
| 38 |
|
| 39 |
if ($result instanceof JobResult) return $this->handleJobResult($job, $result); |
| 40 |
$this->jobManager->deleteJob(['id' => $job->id]); |
| 41 |
return true; |
| 42 |
} catch (CircuitBreakerOpenException $e) { |
| 43 |
// Circuit is open — reschedule without counting as a failure. |
| 44 |
return $this->jobManager->retryJob($job->id, $e->getMessage()); |
| 45 |
} catch (JobException $e) { |
| 46 |
return $this->handleJobException($job, $e); |
| 47 |
} catch (\Exception $e) { |
| 48 |
$this->jobManager->failJob($job->id, $e->getMessage()); |
| 49 |
return false; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
private function handleJobResult(object $job, JobResult $result): bool |
| 54 |
{ |
| 55 |
if ($result->isSuccessful()) { |
| 56 |
$this->jobManager->deleteJob(['id' => $job->id]); |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
if ($result->shouldRetry()) { |
| 61 |
$retried = $this->jobManager->retryJob($job->id, $result->getErrorMessage()); |
| 62 |
|
| 63 |
return $retried; |
| 64 |
} |
| 65 |
|
| 66 |
$this->jobManager->failJob($job->id, $result->getErrorMessage()); |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
private function handleJobException(object $job, JobException $exception): bool |
| 71 |
{ |
| 72 |
if ($exception->shouldRetry()) { |
| 73 |
$retried = $this->jobManager->retryJob($job->id, $exception->getMessage()); |
| 74 |
|
| 75 |
return $retried; |
| 76 |
} |
| 77 |
|
| 78 |
$this->jobManager->failJob($job->id, $exception->getMessage()); |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
private function normalizeLegacyPayload(string $jobType, $legacyPayload): array |
| 83 |
{ |
| 84 |
switch ($jobType) { |
| 85 |
case 'sync_basalam_update_single_product': |
| 86 |
case 'sync_basalam_create_single_product': |
| 87 |
return ['product_id' => $legacyPayload]; |
| 88 |
|
| 89 |
case 'sync_basalam_bulk_update_products': |
| 90 |
case 'sync_basalam_update_all_products': |
| 91 |
return ['last_updatable_product_id' => $legacyPayload]; |
| 92 |
|
| 93 |
case 'sync_basalam_create_all_products': |
| 94 |
return ['include_out_of_stock' => false, 'posts_per_page' => 100]; |
| 95 |
|
| 96 |
default: |
| 97 |
return []; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public function getSortedJobTypes(): array |
| 102 |
{ |
| 103 |
return $this->jobRegistry->getSortedByPriority(); |
| 104 |
} |
| 105 |
|
| 106 |
public function canRun(string $jobType): bool |
| 107 |
{ |
| 108 |
$jobExecutor = $this->jobRegistry->get($jobType); |
| 109 |
|
| 110 |
if (!$jobExecutor) return false; |
| 111 |
|
| 112 |
return $jobExecutor->canRun(); |
| 113 |
} |
| 114 |
|
| 115 |
public function acquireLock(string $jobType, int $timeout = 0): bool |
| 116 |
{ |
| 117 |
return $this->lockManager->acquire($jobType, $timeout); |
| 118 |
} |
| 119 |
|
| 120 |
public function releaseLock(string $jobType): bool |
| 121 |
{ |
| 122 |
return $this->lockManager->release($jobType); |
| 123 |
} |
| 124 |
|
| 125 |
public function acquireGlobalJobsLock(int $timeout = 0): bool |
| 126 |
{ |
| 127 |
return $this->lockManager->acquireGlobalJobsLock($timeout); |
| 128 |
} |
| 129 |
|
| 130 |
public function releaseGlobalJobsLock(): bool |
| 131 |
{ |
| 132 |
return $this->lockManager->releaseGlobalJobsLock(); |
| 133 |
} |
| 134 |
} |
| 135 |
|