|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\FailedJobMonitor; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Queue\Events\JobFailed; |
|
7
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
|
8
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
|
9
|
|
|
use Illuminate\Notifications\Messages\SlackAttachment; |
|
10
|
|
|
use Illuminate\Notifications\Notification as NotificationBase; |
|
11
|
|
|
|
|
12
|
|
|
class Notification extends NotificationBase |
|
13
|
|
|
{ |
|
14
|
|
|
protected $event; |
|
15
|
|
|
|
|
16
|
|
|
public function via($notifiable) |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
return config('laravel-failed-job-monitor.channels'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function setEvent(JobFailed $event) |
|
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
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage |
|
33
|
|
|
*/ |
|
34
|
|
|
public function toMail($notifiable) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
return (new MailMessage) |
|
37
|
|
|
->subject(trans('laravel-failed-job-monitor::mail.subject')) |
|
|
|
|
|
|
38
|
|
|
->greeting(trans('laravel-failed-job-monitor::mail.greeting')) |
|
|
|
|
|
|
39
|
|
|
->line(trans('laravel-failed-job-monitor::mail.intro')) |
|
|
|
|
|
|
40
|
|
|
->line(trans('laravel-failed-job-monitor::mail.job_info', ['job' => $this->event->job->resolveName()])) |
|
|
|
|
|
|
41
|
|
|
->line(trans('laravel-failed-job-monitor::mail.attachment')) |
|
|
|
|
|
|
42
|
|
|
->attachData($this->buildException($this->event->exception), |
|
43
|
|
|
'failed_job_'.Carbon::now()->format('Y-m-d h:i:s').'.txt') |
|
44
|
|
|
->attachData($this->event->job->getRawBody(), |
|
45
|
|
|
'payload_'.Carbon::now()->format('Y-m-d h:i:s').'.txt'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the Slack representation of the notification. |
|
50
|
|
|
* |
|
51
|
|
|
* @param mixed $notifiable |
|
52
|
|
|
* @return \Illuminate\Notifications\Messages\SlackMessage |
|
53
|
|
|
*/ |
|
54
|
|
|
public function toSlack($notifiable) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
return (new SlackMessage) |
|
57
|
|
|
->error() |
|
58
|
|
|
->content(trans('laravel-failed-job-monitor::slack.intro')) |
|
|
|
|
|
|
59
|
|
|
->attachment(function (SlackAttachment $attachment) { |
|
60
|
|
|
$attachment->title(trans('laravel-failed-job-monitor::slack.job_info')) |
|
|
|
|
|
|
61
|
|
|
->content($this->event->job->resolveName()); |
|
62
|
|
|
})->attachment(function (SlackAttachment $attachment) { |
|
63
|
|
|
$attachment->title('failed_job_'.Carbon::now()->format('Y-m-d h:i:s').'.txt') |
|
64
|
|
|
->content($this->buildException($this->event->exception)); |
|
65
|
|
|
})->attachment(function (SlackAttachment $attachment) { |
|
66
|
|
|
$attachment->title('payload_'.Carbon::now()->format('Y-m-d h:i:s').'.txt') |
|
67
|
|
|
->content($this->event->job->getRawBody()); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function buildException(\Exception $exception) |
|
72
|
|
|
{ |
|
73
|
|
|
$response = sprintf( |
|
74
|
|
|
'%s: %s (%s:%s)', |
|
75
|
|
|
get_class($exception), |
|
76
|
|
|
$exception->getMessage(), |
|
77
|
|
|
$exception->getFile(), |
|
78
|
|
|
$exception->getLine() |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$response .= PHP_EOL; |
|
82
|
|
|
$response .= $exception->getTraceAsString().PHP_EOL; |
|
83
|
|
|
|
|
84
|
|
|
return $response; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.