-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathQueueMonitorProvider.php
More file actions
147 lines (120 loc) · 5.04 KB
/
QueueMonitorProvider.php
File metadata and controls
147 lines (120 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace romanzipp\QueueMonitor\Providers;
use Illuminate\Config\Repository;
use Illuminate\Queue\Events as QueueEvents;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use romanzipp\QueueMonitor\Console\Commands\MarkJobsAsStaleCommand;
use romanzipp\QueueMonitor\Console\Commands\PurgeOldMonitorsCommand;
use romanzipp\QueueMonitor\Middleware\CheckQueueMonitorUiConfig;
use romanzipp\QueueMonitor\Models\Monitor;
use romanzipp\QueueMonitor\Services\QueueMonitor;
class QueueMonitorProvider extends ServiceProvider
{
public function boot(): void
{
/** @var \Illuminate\Config\Repository $config */
$config = $this->app['config'];
/** @var \Illuminate\Events\Dispatcher $events */
$events = $this->app['events'];
/** @var \Illuminate\Queue\QueueManager $queueManager */
$queueManager = $this->app->make(QueueManager::class);
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../../config/queue-monitor.php' => config_path('queue-monitor.php'),
], 'config');
$this->publishes([
__DIR__ . '/../../migrations' => database_path('migrations'),
], 'migrations');
$this->publishes([
__DIR__ . '/../../migrations-upgrade' => base_path('migrations-upgrade'),
], 'migrations-upgrade');
$this->publishes([
__DIR__ . '/../../views' => resource_path('views/vendor/queue-monitor'),
], 'views');
$this->publishes([
__DIR__ . '/../../dist' => public_path('vendor/queue-monitor'),
], 'assets');
$this->commands([
MarkJobsAsStaleCommand::class,
PurgeOldMonitorsCommand::class,
]);
}
$this->loadViewsFrom(__DIR__ . '/../../views', 'queue-monitor');
if ($config->boolean('queue-monitor.ui.enabled', false)) {
Route::group(self::buildRouteGroupConfig($config), function () {
$this->loadRoutesFrom(__DIR__ . '/../../routes/queue-monitor.php');
});
}
// Listen to queue job events
/**
* If the project uses Horizon, we will listen to the JobPushed event,
* because Horizon fires JobPushed event when the job is queued or retry the job again from its UI.
*
* @see https://laravel.com/docs/horizon
*/
if (class_exists('Laravel\Horizon\Events\JobPushed')) {
$events->listen('Laravel\Horizon\Events\JobPushed', function ($event) {
QueueMonitor::handleJobPushed($event);
});
} else {
$events->listen(QueueEvents\JobQueued::class, function (QueueEvents\JobQueued $event) {
QueueMonitor::handleJobQueued($event);
});
}
$queueManager->before(static function (QueueEvents\JobProcessing $event) {
QueueMonitor::handleJobProcessing($event);
});
$queueManager->after(static function (QueueEvents\JobProcessed $event) {
QueueMonitor::handleJobProcessed($event);
});
$queueManager->failing(static function (QueueEvents\JobFailed $event) {
QueueMonitor::handleJobFailed($event);
});
$queueManager->exceptionOccurred(static function (QueueEvents\JobExceptionOccurred $event) {
QueueMonitor::handleJobExceptionOccurred($event);
});
$this->app['events']->listen([
QueueEvents\JobExceptionOccurred::class,
QueueEvents\JobFailed::class,
QueueEvents\JobPopped::class,
QueueEvents\JobPopping::class,
QueueEvents\JobProcessed::class,
QueueEvents\JobProcessing::class,
QueueEvents\JobQueued::class,
QueueEvents\JobQueueing::class,
QueueEvents\JobReleasedAfterException::class,
QueueEvents\JobRetryRequested::class,
QueueEvents\JobTimedOut::class,
QueueEvents\Looping::class,
QueueEvents\QueueBusy::class,
QueueEvents\WorkerStopping::class,
], function ($event) {
// dump(get_class($event));
});
}
/**
* @return array<string, mixed>
*/
public static function buildRouteGroupConfig(Repository $config): array
{
$routeConfig = $config->array('queue-monitor.ui.route');
if ( ! isset($routeConfig['middleware'])) {
$routeConfig['middleware'] = [];
}
$routeConfig['middleware'][] = CheckQueueMonitorUiConfig::class;
return $routeConfig;
}
public function register(): void
{
/** @var \Illuminate\Config\Repository $config */
$config = $this->app['config'];
$this->mergeConfigFrom(
__DIR__ . '/../../config/queue-monitor.php',
'queue-monitor'
);
QueueMonitor::$model = $config->get('queue-monitor.model') ?: Monitor::class;
}
}