send notification on update failure

This commit is contained in:
Denis Duliçi
2021-03-12 17:24:03 +03:00
parent a97709a8cb
commit 1259fa7918
9 changed files with 408 additions and 68 deletions

View File

@ -4,6 +4,7 @@ namespace App\Console\Commands;
use App\Events\Install\UpdateCopied;
use App\Events\Install\UpdateDownloaded;
use App\Events\Install\UpdateFailed;
use App\Events\Install\UpdateUnzipped;
use App\Jobs\Install\CopyFiles;
use App\Jobs\Install\DownloadFile;
@ -56,7 +57,11 @@ class Update extends Command
$this->company = $this->argument('company');
if (false === $this->new = $this->getNewVersion()) {
$this->error('Not able to get the latest version of ' . $this->alias . '!');
$message = 'Not able to get the latest version of ' . $this->alias . '!';
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Version', $message));
return self::CMD_ERROR;
}
@ -112,7 +117,11 @@ class Update extends Command
event(new UpdateDownloaded($this->alias, $this->new, $this->old));
} catch (\Exception $e) {
$this->error($e->getMessage());
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Download', $message));
return false;
}
@ -129,7 +138,11 @@ class Update extends Command
event(new UpdateUnzipped($this->alias, $this->new, $this->old));
} catch (\Exception $e) {
$this->error($e->getMessage());
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Unzip', $message));
return false;
}
@ -146,7 +159,11 @@ class Update extends Command
event(new UpdateCopied($this->alias, $this->new, $this->old));
} catch (\Exception $e) {
$this->error($e->getMessage());
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Copy Files', $message));
return false;
}
@ -161,7 +178,11 @@ class Update extends Command
try {
$this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old, $this->company));
} catch (\Exception $e) {
$this->error($e->getMessage());
$message = $e->getMessage();
$this->error($message);
event(new UpdateFailed($this->alias, $this->new, $this->old, 'Finish', $message));
return false;
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Events\Install;
use App\Abstracts\Event;
class UpdateFailed extends Event
{
public $alias;
public $old;
public $new;
public $step;
public $message;
/**
* Create a new event instance.
*
* @param $alias
* @param $old
* @param $new
* @param $step
*/
public function __construct($alias, $old, $new, $step, $message = '')
{
$this->alias = $alias;
$this->old = $old;
$this->new = $new;
$this->step = $step;
$this->message = $message;
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace App\Listeners\Update;
use App\Events\Install\UpdateFailed as Event;
use App\Notifications\Install\UpdateFailed as Notification;
use Exception;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Notifications\Notifiable;
class SendNotificationOnFailure
{
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(Event $event)
{
$notifiable = $this->getNotifiableClass();
try {
$notifiable->notify(new Notification($event));
} catch (Exception | RequestException $e) {
report($e);
}
}
protected function getNotifiableClass()
{
return new class() {
use Notifiable;
public function routeNotificationForMail()
{
return config('update.notifications.mail.to');
}
public function routeNotificationForSlack()
{
return config('update.notifications.slack.to');
}
public function getKey()
{
return 1;
}
};
}
}

View File

@ -0,0 +1,125 @@
<?php
namespace App\Notifications\Install;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
class UpdateFailed extends Notification
{
/**
* The event.
*
* @var object
*/
public $event;
/**
* The notification config.
*
* @var object
*/
public $notifications;
/**
* Create a notification instance.
*
* @param object $event
*/
public function __construct($event)
{
$this->event = $event;
$this->notifications = config('update.notifications');
}
/**
* Get the notification's channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
$channels = [];
foreach ($this->notifications as $channel => $settings) {
if (empty($settings['enabled'])) {
continue;
}
$channels[] = $channel;
}
return $channels;
}
/**
* Build the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$subject = trans('notifications.update.mail.subject', [
'domain' => request()->getHttpHost(),
]);
$message = trans('notifications.mail.message', [
'alias' => $this->getAliasName(),
'current_version' => $this->event->old,
'new_version' => $this->event->new,
'step' => $this->event->step,
'error_message' => $this->event->message,
]);
return (new MailMessage)
->from($this->notifications['mail']['from'], $this->notifications['mail']['name'])
->subject($subject)
->line($message);
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
$message = trans('notifications.update.slack.message', [
'domain' => request()->getHttpHost(),
]);
return (new SlackMessage)
->error()
->from($this->notifications['slack']['from'], $this->notifications['slack']['emoji'])
->to($this->notifications['slack']['to'])
->content($message)
->attachment(function ($attachment) {
$attachment->fields([
'Alias' => $this->getAliasName(),
'Current Version' => $this->event->old,
'New Version' => $this->event->new,
'Step' => $this->event->step,
'Error Message' => $this->event->message,
]);
});
}
protected function getAliasName()
{
if ($this->event->alias == 'core') {
return ucfirst($this->event->alias);
}
$module = module($this->event->alias);
if (empty($module)) {
return ucfirst($this->event->alias);
}
return $module->getName();
}
}

View File

@ -68,6 +68,9 @@ class Event extends Provider
'App\Events\Document\DocumentViewed' => [
'App\Listeners\Document\MarkDocumentViewed',
],
'App\Events\Install\UpdateFailed' => [
'App\Listeners\Update\SendNotificationOnFailure',
],
'App\Events\Menu\AdminCreated' => [
'App\Listeners\Menu\AddAdminItems',
],