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