1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\FailedJobMonitor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Queue\Events\JobFailed; |
6
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
7
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
8
|
|
|
use Illuminate\Notifications\Messages\SlackAttachment; |
9
|
|
|
use Illuminate\Notifications\Notification as IlluminateNotification; |
10
|
|
|
|
11
|
|
|
class Notification extends IlluminateNotification |
12
|
|
|
{ |
13
|
|
|
/** @var \Illuminate\Queue\Events\JobFailed */ |
14
|
|
|
protected $event; |
15
|
|
|
|
16
|
|
|
public function via($notifiable): array |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
return config('laravel-failed-job-monitor.channels'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setEvent(JobFailed $event): self |
22
|
|
|
{ |
23
|
|
|
$this->event = $event; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getEvent(): JobFailed |
29
|
|
|
{ |
30
|
|
|
return $this->event; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function toMail($notifiable): MailMessage |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
return (new MailMessage) |
36
|
|
|
->error() |
37
|
|
|
->subject('A job failed at '.config('app.url')) |
38
|
|
|
->line("Exception message: {$this->event->exception->getMessage()}") |
39
|
|
|
->line("Job class: {$this->event->job->resolveName()}") |
40
|
|
|
->line("Job body: {$this->event->job->getRawBody()}") |
41
|
|
|
->line("Exception: {$this->event->exception->getTraceAsString()}"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function toSlack(): SlackMessage |
45
|
|
|
{ |
46
|
|
|
return (new SlackMessage) |
47
|
|
|
->error() |
48
|
|
|
->content('A job failed at '.config('app.url')) |
49
|
|
|
->attachment(function (SlackAttachment $attachment) { |
50
|
|
|
$attachment->fields([ |
51
|
|
|
'Exception message' => $this->event->exception->getMessage(), |
52
|
|
|
'Job class' => $this->event->job->resolveName(), |
53
|
|
|
'Job body' => $this->event->job->getRawBody(), |
54
|
|
|
'Exception' => $this->event->exception->getTraceAsString(), |
55
|
|
|
]); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.