class Backup::Notifier::Base

Attributes

max_retries[RW]

Number of times to retry failed attempts to send notification. Default: 10

message[RW]

Message to send. Depends on notifier implementation if this is used. Default: lambda returning: “#{ message } #{ model.label } (#{ model.trigger })”

@yieldparam [model] Backup::Model @yieldparam [data] Hash containing ‘message` and `key` values.

model[R]
notify_on_failure?[RW]

When set to true, the user will be notified by email when a backup process raises an exception before finishing

notify_on_success?[RW]

When set to true, the user will be notified by email when a backup process ends without raising any exceptions

notify_on_warning?[RW]

When set to true, the user will be notified by email when a backup process is successful, but has warnings

on_failure[RW]

When set to true, the user will be notified by email when a backup process raises an exception before finishing

on_success[RW]

When set to true, the user will be notified by email when a backup process ends without raising any exceptions

on_warning[RW]

When set to true, the user will be notified by email when a backup process is successful, but has warnings

retry_waitsec[RW]

Time in seconds to pause before each retry. Default: 30

Public Class Methods

new(model) click to toggle source
# File lib/backup/notifier/base.rb, line 50
def initialize(model)
  @model = model
  load_defaults!

  @on_success = true if on_success.nil?
  @on_warning = true if on_warning.nil?
  @on_failure = true if on_failure.nil?
  @max_retries    ||= 10
  @retry_waitsec  ||= 30
  @message        ||= lambda do |model, data|
    "[#{ data[:status][:message] }] #{ model.label } (#{ model.trigger })"
  end
end

Public Instance Methods

perform!() click to toggle source

This method is called from an ensure block in Model#perform! and must not raise any exceptions. However, each Notifier’s notify! method should raise an exception if the request fails so it may be retried.

# File lib/backup/notifier/base.rb, line 67
def perform!
  status = case model.exit_status
           when 0
             :success if notify_on_success?
           when 1
             :warning if notify_on_success? || notify_on_warning?
           else
             :failure if notify_on_failure?
           end

  if status
    Logger.info "Sending notification using #{ notifier_name }..."
    with_retries { notify!(status) }
  end

rescue Exception => err
  Logger.error Error.wrap(err, "#{ notifier_name } Failed!")
end

Private Instance Methods

notifier_name() click to toggle source

Return the notifier name, with Backup namespace removed

# File lib/backup/notifier/base.rb, line 104
def notifier_name
  self.class.to_s.sub('Backup::', '')
end
status_data_for(status) click to toggle source

Return status data for message creation

# File lib/backup/notifier/base.rb, line 110
def status_data_for(status)
  {
    :success => {
      :message => 'Backup::Success',
      :key => :success
    },
    :warning => {
      :message => 'Backup::Warning',
      :key => :warning
    },
    :failure => {
      :message => 'Backup::Failure',
      :key => :failure
    }
  }[status]
end
with_retries() { || ... } click to toggle source
# File lib/backup/notifier/base.rb, line 88
def with_retries
  retries = 0
  begin
    yield
  rescue StandardError, Timeout::Error => err
    retries += 1
    raise if retries > max_retries

    Logger.info Error.wrap(err, "Retry ##{ retries } of #{ max_retries }.")
    sleep(retry_waitsec)
    retry
  end
end