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



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

def initialize(mailer_class, method_name, *args)
  @mailer_class = mailer_class
  @method_name = method_name
  *@args = *args
  @serialized_args = ::Resque::Mailer.argument_serializer.serialize(*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



192
193
194
# File 'lib/resque_mailer.rb', line 192

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

Instance Method Details

#actual_messageObject



133
134
135
# File 'lib/resque_mailer.rb', line 133

def actual_message
  @actual_message ||= ::Resque::Mailer.prepare_message(@mailer_class, @method_name, *@args)
end

#current_envObject



117
118
119
120
121
122
123
# File 'lib/resque_mailer.rb', line 117

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

#deliverObject Also known as: deliver_now



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

def deliver
  return deliver! if environment_excluded?

  if @mailer_class.deliver?
    begin
      resque.enqueue(@mailer_class, @method_name, @serialized_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 Also known as: deliver_now!



183
184
185
186
187
188
189
# File 'lib/resque_mailer.rb', line 183

def deliver!
  if actual_message.respond_to?(:deliver_now)
    actual_message.deliver_now
  else
    actual_message.deliver
  end
end

#deliver_at(time) ⇒ Object



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

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, @serialized_args)
  end
end

#deliver_in(time) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/resque_mailer.rb', line 163

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, @serialized_args)
  end
end

#environment_excluded?Boolean



125
126
127
# File 'lib/resque_mailer.rb', line 125

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

#excluded_environment?(name) ⇒ Boolean



129
130
131
# File 'lib/resque_mailer.rb', line 129

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

#loggerObject



200
201
202
# File 'lib/resque_mailer.rb', line 200

def logger
  @mailer_class.logger
end

#respond_to?(method_name, *args) ⇒ Boolean



196
197
198
# File 'lib/resque_mailer.rb', line 196

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

#resqueObject



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

def resque
  ::Resque::Mailer.default_queue_target
end

#unschedule_deliveryObject



175
176
177
178
179
180
181
# File 'lib/resque_mailer.rb', line 175

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, @serialized_args)
end