Class: Jenode::EmailTask

Inherits:
Object
  • Object
show all
Includes:
Java::JavaUtilConcurrent::Callable
Defined in:
lib/jenode/email_task.rb

Constant Summary collapse

SKIP_ERROR_MESSAGES =
[
  'invalid mailbox',
  'No such user',
  'Must be local recipient',
  'User unknown',
  'user not found'
]
WAIT_ERROR_MESSAGES =
[
  'try again later',
  'Connection refused'
]
ERROR_QUEUE_NAME =
"error"
EMAIL_SENDED_QUEUE_NAME =
"sended_email"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, job) ⇒ EmailTask

Returns a new instance of EmailTask.



39
40
41
42
# File 'lib/jenode/email_task.rb', line 39

def initialize(ip, job)
  @ip = ip
  @job = job
end

Class Attribute Details

.dkim_keyObject

Returns the value of attribute dkim_key.



20
21
22
# File 'lib/jenode/email_task.rb', line 20

def dkim_key
  @dkim_key
end

.dont_touch_templateObject

Returns the value of attribute dont_touch_template.



20
21
22
# File 'lib/jenode/email_task.rb', line 20

def dont_touch_template
  @dont_touch_template
end

.senderObject

Returns the value of attribute sender.



20
21
22
# File 'lib/jenode/email_task.rb', line 20

def sender
  @sender
end

.templateObject

Returns the value of attribute template.



20
21
22
# File 'lib/jenode/email_task.rb', line 20

def template
  @template
end

.utm_attributesObject

Returns the value of attribute utm_attributes.



20
21
22
# File 'lib/jenode/email_task.rb', line 20

def utm_attributes
  @utm_attributes
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



37
38
39
# File 'lib/jenode/email_task.rb', line 37

def ip
  @ip
end

#jobObject (readonly)

Returns the value of attribute job.



37
38
39
# File 'lib/jenode/email_task.rb', line 37

def job
  @job
end

Class Method Details

.generate_email_object(data, ip) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jenode/email_task.rb', line 22

def generate_email_object(data, ip)
  merged_utm_attributes = utm_attributes.merge(data['utm_attributes'])
  email = Email.new(
    source_ip:           ip,
    utm_attributes:      merged_utm_attributes,
    dont_touch_template: dont_touch_template,
    recipient:           data['recipient'],
    template_vars:       data['template_vars'],
    template:            template,
    sender:              sender,
    dkim_key:            dkim_key
  )
end

Instance Method Details

#error_notify(e) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jenode/email_task.rb', line 96

def error_notify(e)
  body = MultiJson.dump(
    ip:     ip,
    job:    job.payload,
    sender: self.class.sender,
    error:  {
      message:   e.message,
      backtrace: e.backtrace,
    }
  )
  exchange.publish(body, routing_key: ERROR_QUEUE_NAME)
end

#exchangeObject



49
50
51
52
53
54
# File 'lib/jenode/email_task.rb', line 49

def exchange
  Thread.current[:exchange] ||= begin
    channel = $queue_connection.create_channel
    channel.default_exchange
  end
end

#is_skip_error?(e) ⇒ Boolean

Returns:



84
85
86
87
88
# File 'lib/jenode/email_task.rb', line 84

def is_skip_error?(e)
  SKIP_ERROR_MESSAGES.any? do |error_message|
    e.message.match(/#{error_message}/i)
  end
end

#is_wait_error?(e) ⇒ Boolean

Returns:



90
91
92
93
94
# File 'lib/jenode/email_task.rb', line 90

def is_wait_error?(e)
  WAIT_ERROR_MESSAGES.any? do |error_message|
    e.message.match(/#{error_message}/i)
  end
end

#performObject



56
57
58
59
60
61
62
63
64
# File 'lib/jenode/email_task.rb', line 56

def perform
  begin
    send_email
    # email_sended_notify
    job.ack
  rescue Exception => e
    proccess_error(e)
  end
end

#proccess_error(e) ⇒ Object



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

def proccess_error(e)
  error_notify(e)

  if is_skip_error?(e)
    puts "Skip error=#{ e.message }"
    job.ack
  elsif is_wait_error?(e)
    waiting_time = rand(180) + 30 # wait 30-210 seconds
    puts "Wait #{waiting_time} seconds"
    job.reject
    sleep(waiting_time)
  else
    puts "Stop error=#{ e.message } backtrace=#{ e.backtrace }"
    job.reject
    Thread.stop
  end
end

#send_emailObject



44
45
46
47
# File 'lib/jenode/email_task.rb', line 44

def send_email
  email = self.class.generate_email_object(job.payload, ip)
  email.send
end