|
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
|
5 |
|
public function via($notifiable): array |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
5 |
|
return config('throttled-failed-jobs.channels'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
5 |
|
public function setEvent(JobFailed $event): self |
|
24
|
|
|
{ |
|
25
|
5 |
|
$this->event = $event; |
|
26
|
|
|
|
|
27
|
5 |
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
5 |
|
public function getEvent(): JobFailed |
|
31
|
|
|
{ |
|
32
|
5 |
|
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
|
|
|
', |
|
67
|
|
|
$this->event->exception->getMessage(), |
|
68
|
|
|
$this->event->job->resolveName(), |
|
69
|
|
|
$this->event->job->getRawBody() |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return MsTeamsMessage::create() |
|
73
|
|
|
->type('error') |
|
74
|
|
|
->title('A job failed at '.config('app.url')) |
|
75
|
|
|
->content($content) |
|
76
|
|
|
->code($this->event->exception->getTraceAsString()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
5 |
|
public function throttleDecayMinutes(): int |
|
80
|
|
|
{ |
|
81
|
5 |
|
return config('throttled-failed-jobs.throttle_decay'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
public function throttleKeyId() |
|
85
|
|
|
{ |
|
86
|
5 |
|
if ($this->getEvent()->exception instanceof \Exception) { |
|
87
|
5 |
|
return Str::kebab($this->getEvent()->exception->getMessage()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// fall back throttle key, use the notification name... |
|
91
|
|
|
return static::class; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.