ActivityReportJob.php
2 days ago
CleanupJob.php
2 days ago
DeleteExpiredJob.php
2 days ago
Job.php
2 days ago
JobBuilder.php
2 days ago
JobHandler.php
2 days ago
SendEmailJob.php
2 days ago
JobBuilder.php
272 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Job Builder |
| 4 | * |
| 5 | * Fluent interface for creating and scheduling jobs. |
| 6 | * |
| 7 | * @package WPDM\__\Jobs |
| 8 | * @since 7.0.1 |
| 9 | */ |
| 10 | |
| 11 | namespace WPDM\__\Jobs; |
| 12 | |
| 13 | use WPDM\__\CronJob; |
| 14 | |
| 15 | class JobBuilder |
| 16 | { |
| 17 | /** |
| 18 | * Job handler class name |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | private $handler; |
| 23 | |
| 24 | /** |
| 25 | * Job payload data |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | private $data = []; |
| 30 | |
| 31 | /** |
| 32 | * Queue name |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | private $queue = 'default'; |
| 37 | |
| 38 | /** |
| 39 | * Job priority (1-10, higher = sooner) |
| 40 | * |
| 41 | * @var int |
| 42 | */ |
| 43 | private $priority = 5; |
| 44 | |
| 45 | /** |
| 46 | * Delay in seconds before execution |
| 47 | * |
| 48 | * @var int |
| 49 | */ |
| 50 | private $delay = 0; |
| 51 | |
| 52 | /** |
| 53 | * Maximum retry attempts |
| 54 | * |
| 55 | * @var int |
| 56 | */ |
| 57 | private $maxAttempts = 3; |
| 58 | |
| 59 | /** |
| 60 | * Number of times to repeat (0 = infinite for scheduled jobs) |
| 61 | * |
| 62 | * @var int |
| 63 | */ |
| 64 | private $repeat = 1; |
| 65 | |
| 66 | /** |
| 67 | * Interval between repeats in seconds |
| 68 | * |
| 69 | * @var int |
| 70 | */ |
| 71 | private $interval = 0; |
| 72 | |
| 73 | /** |
| 74 | * Unique job code (to prevent duplicates) |
| 75 | * |
| 76 | * @var string|null |
| 77 | */ |
| 78 | private $uniqueCode = null; |
| 79 | |
| 80 | /** |
| 81 | * Constructor |
| 82 | * |
| 83 | * @param string $handler Job handler class name |
| 84 | */ |
| 85 | public function __construct(string $handler) |
| 86 | { |
| 87 | $this->handler = $handler; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set job payload data |
| 92 | * |
| 93 | * @param array $data |
| 94 | * @return self |
| 95 | */ |
| 96 | public function withData(array $data): self |
| 97 | { |
| 98 | $this->data = $data; |
| 99 | return $this; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Set queue name |
| 104 | * |
| 105 | * @param string $queue |
| 106 | * @return self |
| 107 | */ |
| 108 | public function onQueue(string $queue): self |
| 109 | { |
| 110 | $this->queue = $queue; |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set job priority (1-10) |
| 116 | * |
| 117 | * @param int $priority |
| 118 | * @return self |
| 119 | */ |
| 120 | public function priority(int $priority): self |
| 121 | { |
| 122 | $this->priority = max(1, min(10, $priority)); |
| 123 | return $this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Delay execution by seconds |
| 128 | * |
| 129 | * @param int $seconds |
| 130 | * @return self |
| 131 | */ |
| 132 | public function delay(int $seconds): self |
| 133 | { |
| 134 | $this->delay = max(0, $seconds); |
| 135 | return $this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Delay execution until a specific timestamp |
| 140 | * |
| 141 | * @param int $timestamp |
| 142 | * @return self |
| 143 | */ |
| 144 | public function delayUntil(int $timestamp): self |
| 145 | { |
| 146 | $this->delay = max(0, $timestamp - time()); |
| 147 | return $this; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Set maximum retry attempts |
| 152 | * |
| 153 | * @param int $attempts |
| 154 | * @return self |
| 155 | */ |
| 156 | public function attempts(int $attempts): self |
| 157 | { |
| 158 | $this->maxAttempts = max(1, $attempts); |
| 159 | return $this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set unique code to prevent duplicate jobs |
| 164 | * |
| 165 | * @param string $code |
| 166 | * @return self |
| 167 | */ |
| 168 | public function unique(string $code): self |
| 169 | { |
| 170 | $this->uniqueCode = $code; |
| 171 | return $this; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Run every N minutes |
| 176 | * |
| 177 | * @param int $minutes |
| 178 | * @return self |
| 179 | */ |
| 180 | public function everyMinutes(int $minutes): self |
| 181 | { |
| 182 | $this->repeat = 0; // Infinite |
| 183 | $this->interval = $minutes * 60; |
| 184 | return $this; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Run hourly |
| 189 | * |
| 190 | * @return self |
| 191 | */ |
| 192 | public function hourly(): self |
| 193 | { |
| 194 | return $this->everyMinutes(60); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Run every N hours |
| 199 | * |
| 200 | * @param int $hours |
| 201 | * @return self |
| 202 | */ |
| 203 | public function everyHours(int $hours): self |
| 204 | { |
| 205 | return $this->everyMinutes($hours * 60); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Run daily |
| 210 | * |
| 211 | * @return self |
| 212 | */ |
| 213 | public function daily(): self |
| 214 | { |
| 215 | return $this->everyMinutes(1440); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Run weekly |
| 220 | * |
| 221 | * @return self |
| 222 | */ |
| 223 | public function weekly(): self |
| 224 | { |
| 225 | return $this->everyMinutes(10080); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Repeat N times with interval |
| 230 | * |
| 231 | * @param int $times Number of times to repeat |
| 232 | * @param int $intervalSeconds Seconds between each execution |
| 233 | * @return self |
| 234 | */ |
| 235 | public function repeat(int $times, int $intervalSeconds = 0): self |
| 236 | { |
| 237 | $this->repeat = max(1, $times); |
| 238 | $this->interval = max(0, $intervalSeconds); |
| 239 | return $this; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Create the job and add to queue |
| 244 | * |
| 245 | * @return int|false Job ID on success, false on failure |
| 246 | */ |
| 247 | public function create() |
| 248 | { |
| 249 | return CronJob::create( |
| 250 | $this->handler, |
| 251 | $this->data, |
| 252 | time() + $this->delay, |
| 253 | $this->repeat, |
| 254 | $this->interval, |
| 255 | $this->queue, |
| 256 | $this->priority, |
| 257 | $this->maxAttempts, |
| 258 | $this->uniqueCode |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Alias for create() |
| 264 | * |
| 265 | * @return int|false |
| 266 | */ |
| 267 | public function dispatch() |
| 268 | { |
| 269 | return $this->create(); |
| 270 | } |
| 271 | } |
| 272 |