Commit 2fb5168e authored by Damien McKenna's avatar Damien McKenna
Browse files

Issue #3133933 by DamienMcKenna, ramonvasconcelos, larisse, paulocs,...

Issue #3133933 by DamienMcKenna, ramonvasconcelos, larisse, paulocs, mrinalini9, alexanderj, solideogloria, pflora: Send email on failure.
parent fbd2f1b6
Loading
Loading
Loading
Loading
+2 −0
Changes for CHANGELOG.txt: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ By DamienMcKenna: Updated CHANGELOG.txt for recent 5.0.x changes already present
  filectime() and filesize() receiving NULL values.
#3309301 by DamienMcKenna: Adjust site restore check to be less strict.
#3305241 by DamienMcKenna, WagnerMelo: Improve test coverage, fix D10 issues.
#3133933 by DamienMcKenna, ramonvasconcelos, larisse, paulocs, mrinalini9,
  alexanderj, solideogloria, pflora: Send email on failure.


Backup and Migrate 5.0.2, 2022-08-19
+15 −2
Changes for backup_migrate.module: 15 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -342,6 +342,19 @@ function backup_migrate_module_version() {
 * Implements hook_mail().
*/
function backup_migrate_mail($key, &$message, $params) {
  $message['subject'] = $key;
  $message['body'] = $params;
  if (isset($params['subject'])) {
    $subject = $params['subject'];
  }
  elseif ($key == 'backup_success') {
    $subject = 'The backup succeeded';
  }
  elseif ($key == 'backup_failure') {
    $subject = 'The backup failed';
  }

  if (!empty($subject)) {
    $message['from'] = \Drupal::config('system.site')->get('mail');
    $message['subject'] = $subject;
    $message['body'][] = $params['message'];
  }
}
+29 −10
Changes for src/Core/Filter/Notify.php: 29 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -27,24 +27,37 @@ 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',
        'title' => 'Send an email if backup succeeds',
      ];
      $schema['fields']['notify_sucess_email'] = [
      $schema['fields']['notify_success_email'] = [
        'group' => 'notify',
        'type' => 'text',
        'title' => 'Email Address for Success Notices',
        '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;
@@ -83,20 +96,26 @@ class Notify extends PluginBase implements PluginCallerInterface {
  /**
   * Call notification function if backup was succeed
   */
  public function backupSucceed() {
  public function backupSuccess() {
    if ($this->config->get('notify_success_enable')) {
      $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);
      $recipient = $this->config->get('notify_success_email');
      $this->sendNotification('backup_success', $subject, $body, $recipient);
    }
  }

  /**
   *
   * Call notification function if backup was failed.
   */
  public function backupFail(\Exception $e) {

  public function backupFailure(\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_success_email');
      $this->sendNotification('backup_failure', $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);
  }

  /**
+2 −2
Changes for src/Core/Main/BackupMigrate.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -97,11 +97,11 @@ class BackupMigrate implements BackupMigrateInterface {
      }

      // Let plugins react to a successful operation.
      $this->plugins()->call('backupSucceed', $file);
      $this->plugins()->call('backupSuccess', $file);
    }
    catch (\Exception $e) {
      // Let plugins react to a failed operation.
      $this->plugins()->call('backupFail', $e);
      $this->plugins()->call('backupFailure', $e);

      // The consuming software needs to deal with this.
      throw $e;
+5 −2
Changes for src/Core/Service/Mailer.php: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ class Mailer implements MailerInterface {
  /**
   * {@inheritdoc}
   */
  public function send($to, $subject, $body, $replacements = [], $additional_headers = []) {
  public function send($key, $to, $subject, $body, $replacements = [], $additional_headers = []) {
    // Combine the to objects.
    if (is_array($to)) {
      $to = implode(',', $to);
@@ -28,7 +28,10 @@ class Mailer implements MailerInterface {
    }

    $langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
    \Drupal::service('plugin.manager.mail')->mail('backup_migrate', $subject, $to, $langcode, ['content' => $body]);
    \Drupal::service('plugin.manager.mail')->mail('backup_migrate', $key, $to, $langcode, [
      'message' => $body,
      'subject' => $subject,
    ]);
  }

}
Loading