background_job.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
  2. def initialize(id)
  3. @article_id = id
  4. end
  5. def perform
  6. record = Ticket::Article.find(@article_id)
  7. # build subject
  8. ticket = Ticket.lookup(id: record.ticket_id)
  9. article_count = Ticket::Article.where(ticket_id: ticket.id).count
  10. subject = if article_count > 1
  11. ticket.subject_build(record.subject, true)
  12. else
  13. ticket.subject_build(record.subject)
  14. end
  15. # set retry count
  16. if !record.preferences['delivery_retry']
  17. record.preferences['delivery_retry'] = 0
  18. end
  19. record.preferences['delivery_retry'] += 1
  20. # send email
  21. if !ticket.group.email_address_id
  22. log_error(record, "No email address defined for group id '#{ticket.group.id}'!")
  23. elsif !ticket.group.email_address.channel_id
  24. log_error(record, "No channel defined for email_address id '#{ticket.group.email_address_id}'!")
  25. end
  26. channel = ticket.group.email_address.channel
  27. notification = false
  28. sender = Ticket::Article::Sender.lookup(id: record.sender_id)
  29. if sender['name'] == 'System'
  30. notification = true
  31. end
  32. # get linked channel and send
  33. begin
  34. message = channel.deliver(
  35. {
  36. message_id: record.message_id,
  37. in_reply_to: record.in_reply_to,
  38. references: ticket.get_references([record.message_id]),
  39. from: record.from,
  40. to: record.to,
  41. cc: record.cc,
  42. subject: subject,
  43. content_type: record.content_type,
  44. body: record.body,
  45. attachments: record.attachments
  46. },
  47. notification
  48. )
  49. rescue => e
  50. log_error(record, e.message)
  51. return
  52. end
  53. if !message
  54. log_error(record, 'Unable to get sent email')
  55. return
  56. end
  57. # set delivery status
  58. record.preferences['delivery_status_message'] = nil
  59. record.preferences['delivery_status'] = 'success'
  60. record.preferences['delivery_status_date'] = Time.zone.now
  61. record.save!
  62. # store mail plain
  63. record.save_as_raw(message.to_s)
  64. # add history record
  65. recipient_list = ''
  66. [:to, :cc].each { |key|
  67. next if !record[key]
  68. next if record[key] == ''
  69. if recipient_list != ''
  70. recipient_list += ','
  71. end
  72. recipient_list += record[key]
  73. }
  74. Rails.logger.info "Send email to: '#{recipient_list}' (from #{record.from})"
  75. return if recipient_list == ''
  76. History.add(
  77. o_id: record.id,
  78. history_type: 'email',
  79. history_object: 'Ticket::Article',
  80. related_o_id: ticket.id,
  81. related_history_object: 'Ticket',
  82. value_from: record.subject,
  83. value_to: recipient_list,
  84. created_by_id: record.created_by_id,
  85. )
  86. end
  87. def log_error(local_record, message)
  88. local_record.preferences['delivery_status'] = 'fail'
  89. local_record.preferences['delivery_status_message'] = message
  90. local_record.preferences['delivery_status_date'] = Time.zone.now
  91. local_record.save
  92. Rails.logger.error message
  93. if local_record.preferences['delivery_retry'] > 3
  94. recipient_list = ''
  95. [:to, :cc].each { |key|
  96. next if !local_record[key]
  97. next if local_record[key] == ''
  98. if recipient_list != ''
  99. recipient_list += ','
  100. end
  101. recipient_list += local_record[key]
  102. }
  103. Ticket::Article.create(
  104. ticket_id: local_record.ticket_id,
  105. content_type: 'text/plain',
  106. body: "Unable to send email to '#{recipient_list}': #{message}",
  107. internal: true,
  108. sender: Ticket::Article::Sender.find_by(name: 'System'),
  109. type: Ticket::Article::Type.find_by(name: 'note'),
  110. preferences: {
  111. delivery_article_id_related: local_record.id,
  112. delivery_message: true,
  113. },
  114. updated_by_id: 1,
  115. created_by_id: 1,
  116. )
  117. end
  118. raise message
  119. end
  120. def max_attempts
  121. 4
  122. end
  123. def reschedule_at(current_time, attempts)
  124. if Rails.env.production?
  125. return current_time + attempts * 20.seconds
  126. end
  127. current_time + 5.seconds
  128. end
  129. end