1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TalvBansal\ThrottledFailedJobMonitor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
6
|
|
|
use Illuminate\Notifications\Messages\SlackAttachment; |
7
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
8
|
|
|
use Illuminate\Notifications\Notification as IlluminateNotification; |
9
|
|
|
use Illuminate\Queue\Events\JobFailed; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use NotificationChannels\MsTeams\MsTeamsMessage; |
12
|
|
|
|
13
|
|
|
class Notification extends IlluminateNotification implements ThrottledNotification |
14
|
|
|
{ |
15
|
|
|
/** @var \Illuminate\Queue\Events\JobFailed */ |
16
|
|
|
protected $event; |
17
|
|
|
|
18
|
|
|
public function via($notifiable): array |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
return config('throttled-failed-jobs.channels'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setEvent(JobFailed $event): self |
24
|
|
|
{ |
25
|
|
|
$this->event = $event; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getEvent(): JobFailed |
31
|
|
|
{ |
32
|
|
|
return $this->event; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function toMail($notifiable): MailMessage |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
return (new MailMessage) |
38
|
|
|
->error() |
39
|
|
|
->subject('A job failed at '.config('app.url')) |
40
|
|
|
->line("Exception message: {$this->event->exception->getMessage()}") |
41
|
|
|
->line("Job class: {$this->event->job->resolveName()}") |
42
|
|
|
->line("Job body: {$this->event->job->getRawBody()}") |
43
|
|
|
->line("Exception: {$this->event->exception->getTraceAsString()}"); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function toSlack(): SlackMessage |
47
|
|
|
{ |
48
|
|
|
return (new SlackMessage) |
49
|
|
|
->error() |
50
|
|
|
->content('A job failed at '.config('app.url')) |
51
|
|
|
->attachment(function (SlackAttachment $attachment) { |
52
|
|
|
$attachment->fields([ |
53
|
|
|
'Exception message' => $this->event->exception->getMessage(), |
54
|
|
|
'Job class' => $this->event->job->resolveName(), |
55
|
|
|
'Job body' => $this->event->job->getRawBody(), |
56
|
|
|
'Exception' => $this->event->exception->getTraceAsString(), |
57
|
|
|
]); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function toMsTeams(): MsTeamsMessage |
62
|
|
|
{ |
63
|
|
|
$content = sprintf('## Job class : %s |
64
|
|
|
> Exception message: %s |
65
|
|
|
Job body: %s |
66
|
|
|
```php |
67
|
|
|
%s |
68
|
|
|
``` |
69
|
|
|
', |
70
|
|
|
$this->event->exception->getMessage(), |
71
|
|
|
$this->event->job->resolveName(), |
72
|
|
|
$this->event->job->getRawBody(), |
73
|
|
|
$this->event->exception->getTraceAsString() |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return MsTeamsMessage::create() |
77
|
|
|
->title('A job failed at '.config('app.url')) |
78
|
|
|
->content($content); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function throttleDecayMinutes(): int |
82
|
|
|
{ |
83
|
|
|
return config('throttled-failed-jobs.throttle_decay'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function throttleKeyId() |
87
|
|
|
{ |
88
|
|
|
if ($this->getEvent()->exception instanceof \Exception) { |
89
|
|
|
return Str::kebab($this->getEvent()->exception->getMessage()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// fall back throttle key, use the notification name... |
93
|
|
|
return static::class; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.