Class: Backup::Notifier::Base

Inherits:
Object
  • Object
show all
Includes:
Config::Helpers, Utilities::Helpers
Defined in:
lib/backup/notifier/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Config::Helpers

included

Constructor Details

#initialize(model) ⇒ Base

Returns a new instance of Base.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/backup/notifier/base.rb', line 48

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 |m, data|
    "[#{data[:status][:message]}] #{m.label} (#{m.trigger})"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Backup::Config::Helpers

Instance Attribute Details

#max_retriesObject

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



30
31
32
# File 'lib/backup/notifier/base.rb', line 30

def max_retries
  @max_retries
end

#message {|Backup::Model, Hash| ... } ⇒ Object

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

Yield Parameters:

  • Backup::Model (model)
  • Hash (data)

    containing ‘message` and `key` values.



44
45
46
# File 'lib/backup/notifier/base.rb', line 44

def message
  @message
end

#modelObject (readonly)

Returns the value of attribute model.



46
47
48
# File 'lib/backup/notifier/base.rb', line 46

def model
  @model
end

#on_failureObject Also known as: notify_on_failure?

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



24
25
26
# File 'lib/backup/notifier/base.rb', line 24

def on_failure
  @on_failure
end

#on_successObject Also known as: notify_on_success?

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



12
13
14
# File 'lib/backup/notifier/base.rb', line 12

def on_success
  @on_success
end

#on_warningObject Also known as: notify_on_warning?

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



18
19
20
# File 'lib/backup/notifier/base.rb', line 18

def on_warning
  @on_warning
end

#retry_waitsecObject

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



35
36
37
# File 'lib/backup/notifier/base.rb', line 35

def retry_waitsec
  @retry_waitsec
end

Instance Method Details

#perform!Object

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.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/backup/notifier/base.rb', line 65

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