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

Methods included from Utilities::Helpers

#utility_remote

Constructor Details

#initialize(model) ⇒ Base

Returns a new instance of Base.



50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

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



32
33
34
# File 'lib/backup/notifier/base.rb', line 32

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.



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

def message
  @message
end

#modelObject (readonly)

Returns the value of attribute model.



48
49
50
# File 'lib/backup/notifier/base.rb', line 48

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



26
27
28
# File 'lib/backup/notifier/base.rb', line 26

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



14
15
16
# File 'lib/backup/notifier/base.rb', line 14

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



20
21
22
# File 'lib/backup/notifier/base.rb', line 20

def on_warning
  @on_warning
end

#retry_waitsecObject

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



37
38
39
# File 'lib/backup/notifier/base.rb', line 37

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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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