Class: Resque::Mailer::MessageDecoy

Inherits:
Object
  • Object
show all
Defined in:
lib/resque_mailer.rb

Instance Method Summary collapse

Constructor Details

#initialize(mailer_class, method_name, *args) ⇒ MessageDecoy



93
94
95
96
97
98
# File 'lib/resque_mailer.rb', line 93

def initialize(mailer_class, method_name, *args)
  @mailer_class = mailer_class
  @method_name = method_name
  *@args = *args
  actual_message if environment_excluded?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



173
174
175
# File 'lib/resque_mailer.rb', line 173

def method_missing(method_name, *args)
  actual_message.send(method_name, *args)
end

Instance Method Details

#actual_messageObject



120
121
122
# File 'lib/resque_mailer.rb', line 120

def actual_message
  @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
end

#current_envObject



104
105
106
107
108
109
110
# File 'lib/resque_mailer.rb', line 104

def current_env
  if defined?(Rails)
    ::Resque::Mailer.current_env || ::Rails.env
  else
    ::Resque::Mailer.current_env
  end
end

#deliverObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resque_mailer.rb', line 124

def deliver
  return deliver! if environment_excluded?

  if @mailer_class.deliver?
    begin
      resque.enqueue(@mailer_class, @method_name, *@args)
    rescue Errno::ECONNREFUSED, Redis::CannotConnectError
      logger.error "Unable to connect to Redis; falling back to synchronous mail delivery" if logger
      deliver!
    end
  end
end

#deliver!Object



169
170
171
# File 'lib/resque_mailer.rb', line 169

def deliver!
  actual_message.deliver
end

#deliver_at(time) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/resque_mailer.rb', line 137

def deliver_at(time)
  return deliver! if environment_excluded?

  unless resque.respond_to? :enqueue_at
    raise "You need to install resque-scheduler to use deliver_at"
  end

  if @mailer_class.deliver?
    resque.enqueue_at(time, @mailer_class, @method_name, *@args)
  end
end

#deliver_in(time) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/resque_mailer.rb', line 149

def deliver_in(time)
  return deliver! if environment_excluded?

  unless resque.respond_to? :enqueue_in
    raise "You need to install resque-scheduler to use deliver_in"
  end

  if @mailer_class.deliver?
    resque.enqueue_in(time, @mailer_class, @method_name, *@args)
  end
end

#environment_excluded?Boolean



112
113
114
# File 'lib/resque_mailer.rb', line 112

def environment_excluded?
  !ActionMailer::Base.perform_deliveries || excluded_environment?(current_env)
end

#excluded_environment?(name) ⇒ Boolean



116
117
118
# File 'lib/resque_mailer.rb', line 116

def excluded_environment?(name)
  ::Resque::Mailer.excluded_environments && ::Resque::Mailer.excluded_environments.include?(name.to_sym)
end

#loggerObject



181
182
183
# File 'lib/resque_mailer.rb', line 181

def logger
  @mailer_class.logger
end

#respond_to?(method_name, *args) ⇒ Boolean



177
178
179
# File 'lib/resque_mailer.rb', line 177

def respond_to?(method_name, *args)
  super || actual_message.respond_to?(method_name, *args)
end

#resqueObject



100
101
102
# File 'lib/resque_mailer.rb', line 100

def resque
  ::Resque::Mailer.default_queue_target
end

#unschedule_deliveryObject



161
162
163
164
165
166
167
# File 'lib/resque_mailer.rb', line 161

def unschedule_delivery
  unless resque.respond_to? :remove_delayed
    raise "You need to install resque-scheduler to use unschedule_delivery"
  end

  resque.remove_delayed(@mailer_class, @method_name, *@args)
end