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

Returns a new instance of MessageDecoy



98
99
100
101
102
103
104
# File 'lib/resque_mailer.rb', line 98

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



183
184
185
# File 'lib/resque_mailer.rb', line 183

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

Instance Method Details

#actual_messageObject



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

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

#current_envObject



110
111
112
113
114
115
116
# File 'lib/resque_mailer.rb', line 110

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

#deliverObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/resque_mailer.rb', line 130

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



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

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

#deliver_at(time) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/resque_mailer.rb', line 143

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



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/resque_mailer.rb', line 155

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

Returns:

  • (Boolean)


118
119
120
# File 'lib/resque_mailer.rb', line 118

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

#excluded_environment?(name) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/resque_mailer.rb', line 122

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

#loggerObject



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

def logger
  @mailer_class.logger
end

#respond_to?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/resque_mailer.rb', line 187

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

#resqueObject



106
107
108
# File 'lib/resque_mailer.rb', line 106

def resque
  ::Resque::Mailer.default_queue_target
end

#unschedule_deliveryObject



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

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