|
55 | 55 | use PHPeek\LaravelQueueMetrics\Services\RedisKeyScannerService; |
56 | 56 | use PHPeek\LaravelQueueMetrics\Services\ServerMetricsService; |
57 | 57 | use PHPeek\LaravelQueueMetrics\Services\WorkerMetricsQueryService; |
| 58 | +use PHPeek\LaravelQueueMetrics\Contracts\MetricsHook; |
| 59 | +use PHPeek\LaravelQueueMetrics\Support\HookManager; |
| 60 | +use PHPeek\LaravelQueueMetrics\Support\HookPipeline; |
58 | 61 | use PHPeek\LaravelQueueMetrics\Support\RedisMetricsStore; |
59 | 62 | use PHPeek\LaravelQueueMetrics\Utilities\PercentileCalculator; |
60 | 63 | use Spatie\LaravelPackageTools\Package; |
@@ -112,6 +115,10 @@ public function packageRegistered(): void |
112 | 115 |
|
113 | 116 | // Register utilities |
114 | 117 | $this->app->singleton(PercentileCalculator::class); |
| 118 | + |
| 119 | + // Register hook system |
| 120 | + $this->app->singleton(HookPipeline::class); |
| 121 | + $this->app->singleton(HookManager::class); |
115 | 122 | } |
116 | 123 |
|
117 | 124 | /** |
@@ -180,25 +187,50 @@ public function packageBooted(): void |
180 | 187 |
|
181 | 188 | // Register scheduled tasks |
182 | 189 | $this->registerScheduledTasks(); |
| 190 | + |
| 191 | + // Load and register hooks from configuration |
| 192 | + $this->loadHooksFromConfig(); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Load hooks from configuration and register them. |
| 197 | + */ |
| 198 | + protected function loadHooksFromConfig(): void |
| 199 | + { |
| 200 | + /** @var HookManager $hookManager */ |
| 201 | + $hookManager = $this->app->make(HookManager::class); |
| 202 | + |
| 203 | + /** @var array<string, array<class-string<MetricsHook>>> $configHooks */ |
| 204 | + $configHooks = config('queue-metrics.hooks', []); |
| 205 | + |
| 206 | + foreach ($configHooks as $context => $hookClasses) { |
| 207 | + foreach ($hookClasses as $hookClass) { |
| 208 | + if (! class_exists($hookClass)) { |
| 209 | + continue; |
| 210 | + } |
| 211 | + |
| 212 | + /** @var MetricsHook $hook */ |
| 213 | + $hook = $this->app->make($hookClass); |
| 214 | + $hookManager->register($context, $hook); |
| 215 | + } |
| 216 | + } |
183 | 217 | } |
184 | 218 |
|
185 | 219 | /** |
186 | 220 | * Register scheduled tasks for queue metrics maintenance. |
187 | 221 | */ |
188 | 222 | protected function registerScheduledTasks(): void |
189 | 223 | { |
190 | | - /** @var string $schedule */ |
191 | | - $schedule = config('queue-metrics.worker_heartbeat.auto_detect_schedule', '* * * * *'); |
192 | 224 | /** @var int $threshold */ |
193 | 225 | $threshold = config('queue-metrics.worker_heartbeat.stale_threshold', 60); |
194 | 226 |
|
195 | 227 | // Schedule stale worker cleanup |
196 | | - $this->app->booted(function () use ($schedule, $threshold) { |
| 228 | + $this->app->booted(function () use ($threshold) { |
197 | 229 | $scheduler = $this->app->make(\Illuminate\Console\Scheduling\Schedule::class); |
198 | 230 |
|
199 | 231 | $scheduler->command('queue-metrics:cleanup-stale-workers', [ |
200 | 232 | '--threshold' => $threshold, |
201 | | - ])->cron($schedule); |
| 233 | + ])->everyMinute(); |
202 | 234 |
|
203 | 235 | // Schedule adaptive baseline calculation |
204 | 236 | $this->scheduleAdaptiveBaselineCalculation($scheduler); |
|
0 commit comments