|
7 | 7 | use Illuminate\Notifications\Messages\NexmoMessage;
|
8 | 8 | use Illuminate\Notifications\Messages\SlackMessage;
|
9 | 9 | use Illuminate\Notifications\Notification;
|
| 10 | +use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock; |
| 11 | +use Illuminate\Notifications\Slack\SlackMessage as ChannelIdSlackMessage; |
| 12 | +use Illuminate\Support\Str; |
10 | 13 | use Laravel\Horizon\Horizon;
|
11 | 14 |
|
12 | 15 | class LongWaitDetected extends Notification
|
@@ -90,19 +93,42 @@ public function toMail($notifiable)
|
90 | 93 | */
|
91 | 94 | public function toSlack($notifiable)
|
92 | 95 | {
|
| 96 | + $fromName = 'Laravel Horizon'; |
| 97 | + $title = 'Long Wait Detected'; |
| 98 | + $text = 'Oh no! Something needs your attention.'; |
| 99 | + $imageUrl = 'https://laravel.com/assets/img/horizon-48px.png'; |
| 100 | + |
| 101 | + $content = sprintf( |
| 102 | + '[%s] The "%s" queue on the "%s" connection has a wait time of %s seconds.', |
| 103 | + config('app.name'), |
| 104 | + $this->longWaitQueue, |
| 105 | + $this->longWaitConnection, |
| 106 | + $this->seconds |
| 107 | + ); |
| 108 | + |
| 109 | + if (class_exists('\Illuminate\Notifications\Slack\SlackMessage') && |
| 110 | + class_exists('\Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock') && |
| 111 | + ! (is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://']))) { |
| 112 | + return (new ChannelIdSlackMessage) |
| 113 | + ->username($fromName) |
| 114 | + ->image($imageUrl) |
| 115 | + ->text($text) |
| 116 | + ->headerBlock($title) |
| 117 | + ->sectionBlock(function (SectionBlock $block) use ($content): void { // @phpstan-ignore-line |
| 118 | + $block->text($content); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
93 | 122 | return (new SlackMessage) // @phpstan-ignore-line
|
94 |
| - ->from('Laravel Horizon') |
95 |
| - ->to(Horizon::$slackChannel) |
96 |
| - ->image('https://laravel.com/assets/img/horizon-48px.png') |
97 |
| - ->error() |
98 |
| - ->content('Oh no! Something needs your attention.') |
99 |
| - ->attachment(function ($attachment) { |
100 |
| - $attachment->title('Long Wait Detected') |
101 |
| - ->content(sprintf( |
102 |
| - '[%s] The "%s" queue on the "%s" connection has a wait time of %s seconds.', |
103 |
| - config('app.name'), $this->longWaitQueue, $this->longWaitConnection, $this->seconds |
104 |
| - )); |
105 |
| - }); |
| 123 | + ->from($fromName) |
| 124 | + ->to(Horizon::$slackChannel) |
| 125 | + ->image($imageUrl) |
| 126 | + ->error() |
| 127 | + ->content($text) |
| 128 | + ->attachment(function ($attachment) use ($title, $content) { |
| 129 | + $attachment->title($title) |
| 130 | + ->content($content); |
| 131 | + }); |
106 | 132 | }
|
107 | 133 |
|
108 | 134 | /**
|
|
0 commit comments