|
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
|
|
|
/** |
|
29
|
|
|
* Get the mail representation of the notification. |
|
30
|
|
|
* |
|
31
|
|
|
* @param mixed $notifiable |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage |
|
34
|
|
|
*/ |
|
35
|
|
|
public function toMail($notifiable): MailMessage |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
return (new MailMessage) |
|
38
|
|
|
->error() |
|
39
|
|
|
->subject('A job failed at '.env('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
|
|
|
/** |
|
47
|
|
|
* Get the Slack representation of the notification. |
|
48
|
|
|
* |
|
49
|
|
|
* @param mixed $notifiable |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Illuminate\Notifications\Messages\SlackMessage |
|
52
|
|
|
*/ |
|
53
|
|
|
public function toSlack($notifiable): SlackMessage |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
return (new SlackMessage) |
|
56
|
|
|
->error() |
|
57
|
|
|
->content('A job failed at '.env('APP_URL')) |
|
58
|
|
|
->attachment(function (SlackAttachment $attachment) { |
|
59
|
|
|
$attachment->fields([ |
|
60
|
|
|
'Exception message' => $this->event->exception->getMessage(), |
|
61
|
|
|
'Job class' => $this->event->job->resolveName(), |
|
62
|
|
|
'Job body' => $this->event->job->getRawBody(), |
|
63
|
|
|
'Exception' => $this->event->exception->getTraceAsString(), |
|
64
|
|
|
]); |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.