|
4 | 4 |
|
5 | 5 | use Carbon\Carbon; |
6 | 6 | use Illuminate\Support\Facades\Cache; |
| 7 | +use Illuminate\Support\Facades\Concurrency; |
7 | 8 | use Statamic\Events\StacheCleared; |
8 | 9 | use Statamic\Events\StacheWarmed; |
9 | 10 | use Statamic\Extensions\FileStore; |
@@ -118,7 +119,13 @@ public function warm() |
118 | 119 |
|
119 | 120 | $this->startTimer(); |
120 | 121 |
|
121 | | - $this->stores()->except($this->exclude)->each->warm(); |
| 122 | + $stores = $this->stores()->except($this->exclude); |
| 123 | + |
| 124 | + if ($this->shouldUseParallelWarming($stores)) { |
| 125 | + $this->warmInParallel($stores); |
| 126 | + } else { |
| 127 | + $stores->each->warm(); |
| 128 | + } |
122 | 129 |
|
123 | 130 | $this->stopTimer(); |
124 | 131 |
|
@@ -232,4 +239,80 @@ public function isWatcherEnabled(): bool |
232 | 239 | ? app()->isLocal() |
233 | 240 | : (bool) $config; |
234 | 241 | } |
| 242 | + |
| 243 | + protected function shouldUseParallelWarming($stores): bool |
| 244 | + { |
| 245 | + $config = config('statamic.stache.warming', []); |
| 246 | + |
| 247 | + if (! ($config['parallel_processing'] ?? false)) { |
| 248 | + return false; |
| 249 | + } |
| 250 | + |
| 251 | + if ($stores->count() < ($config['min_stores_for_parallel'] ?? 3)) { |
| 252 | + return false; |
| 253 | + } |
| 254 | + |
| 255 | + if ($this->getCpuCoreCount() < 2) { |
| 256 | + return false; |
| 257 | + } |
| 258 | + |
| 259 | + // Disable parallel processing if using Redis cache (serialization issues) |
| 260 | + $cacheDriver = config('statamic.stache.cache_store', config('cache.default')); |
| 261 | + if ($cacheDriver === 'redis') { |
| 262 | + \Log::info('Parallel warming disabled due to Redis cache driver'); |
| 263 | + |
| 264 | + return false; |
| 265 | + } |
| 266 | + |
| 267 | + return true; |
| 268 | + } |
| 269 | + |
| 270 | + protected function warmInParallel($stores) |
| 271 | + { |
| 272 | + try { |
| 273 | + $config = config('statamic.stache.warming', []); |
| 274 | + $maxProcesses = $config['max_processes'] ?? 0; |
| 275 | + |
| 276 | + if ($maxProcesses <= 0) { |
| 277 | + $maxProcesses = $this->getCpuCoreCount(); |
| 278 | + } |
| 279 | + |
| 280 | + $maxProcesses = min($maxProcesses, $stores->count()); |
| 281 | + |
| 282 | + $chunkSize = (int) ceil($stores->count() / $maxProcesses); |
| 283 | + $chunks = $stores->chunk($chunkSize); |
| 284 | + |
| 285 | + $closures = $chunks->map(function ($chunk) { |
| 286 | + return function () use ($chunk) { |
| 287 | + return $chunk->each->warm()->keys()->all(); |
| 288 | + }; |
| 289 | + })->all(); |
| 290 | + |
| 291 | + $driver = $config['concurrency_driver'] ?? 'process'; |
| 292 | + |
| 293 | + if (empty($closures)) { |
| 294 | + \Log::info('Closures are empty, skipping parallel warming'); |
| 295 | + } |
| 296 | + |
| 297 | + Concurrency::driver($driver)->run($closures); |
| 298 | + } catch (\Exception $e) { |
| 299 | + \Log::warning('Parallel warming failed, falling back to sequential: '.$e->getMessage()); |
| 300 | + $stores->each->warm(); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + protected function getCpuCoreCount(): int |
| 305 | + { |
| 306 | + if (! function_exists('shell_exec')) { |
| 307 | + return 1; |
| 308 | + } |
| 309 | + |
| 310 | + $command = match (PHP_OS_FAMILY) { |
| 311 | + 'Windows' => 'echo %NUMBER_OF_PROCESSORS%', |
| 312 | + 'Darwin' => 'sysctl -n hw.ncpu 2>/dev/null || echo 1', |
| 313 | + default => 'nproc 2>/dev/null || echo 1', |
| 314 | + }; |
| 315 | + |
| 316 | + return max(1, (int) shell_exec($command)); |
| 317 | + } |
235 | 318 | } |
0 commit comments