Skip to content
Snippets Groups Projects

Issue #3133933 - Send email on failure

Files
4
+ 25
6
@@ -27,12 +27,12 @@ class Notify extends PluginBase implements PluginCallerInterface {
*/
public function configSchema(array $params = []) {
$schema = [];
// Backup configuration.
if ($params['operation'] == 'backup') {
$schema['groups']['notify'] = [
'title' => 'Email Settings',
];
$schema['fields']['notify_success_enable'] = [
'group' => 'notify',
'type' => 'boolean',
@@ -45,6 +45,19 @@ class Notify extends PluginBase implements PluginCallerInterface {
'default_value' => \Drupal::config('system.site')->get('mail'),
'description' => 'The email added to send a notification about backup.',
];
$schema['fields']['notify_failure_enable'] = [
'group' => 'notify',
'type' => 'boolean',
'title' => 'Send an email if backup fails',
];
$schema['fields']['notify_failure_email'] = [
'group' => 'notify',
'type' => 'text',
'title' => 'Email Address for Failure Notices',
'default_value' => \Drupal::config('system.site')->get('mail'),
'description' => 'The email added to send a notification about backup.',
];
}
return $schema;
@@ -88,15 +101,21 @@ class Notify extends PluginBase implements PluginCallerInterface {
$subject = 'Backup finished successfully';
$body = t('Site backup succeeded ' . \Drupal::config('system.site')->get('name'));
$recipient = $this->config->get('notify_sucess_email');
$this->sendNotification($subject, $body, $recipient);
$this->sendNotification('backup_succeed', $subject, $body, $recipient);
}
}
/**
*
* Call notification function if backup was failed.
*/
public function backupFail(\Exception $e) {
if ($this->config->get('notify_failure_enable')) {
$subject = t('Backup finished with failure');
$body = t('Site backup failed ' . \Drupal::config('system.site')->get('name') . "\n");
$body = $body . t('Exception Message: ') . $e;
$recipient = $this->config->get('notify_sucess_email');
$this->sendNotification('backup_failed', $subject, $body, $recipient);
}
}
/**
@@ -116,8 +135,8 @@ class Notify extends PluginBase implements PluginCallerInterface {
* @param $body
* @param $recipient
*/
protected function sendNotification($subject, $body, $recipient) {
\Drupal::service('backup_migrate.mailer')->send($recipient, $subject, $body);
protected function sendNotification($key, $subject, $body, $recipient) {
\Drupal::service('backup_migrate.mailer')->send($key, $recipient, $subject, $body);
}
/**
Loading