Class: SendgridNotification::SendgridStatus

Inherits:
Object
  • Object
show all
Defined in:
app/models/sendgrid_notification/sendgrid_status.rb

Defined Under Namespace

Classes: RetrieveError, Suppression

Class Method Summary collapse

Class Method Details

.auto_update(ignore_errors: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/sendgrid_notification/sendgrid_status.rb', line 20

def self.auto_update(ignore_errors: false)
  last_end_time = SendgridNotification::SendgridStatusUpdateHistory.order(:id).reverse_order.limit(1).pluck(:end_time).first
  start_time = last_end_time || 1.hour.ago.to_i
  end_time = Time.now.to_i

  suppressions = update(start_time, end_time)

  begin
    SendgridStatusUpdateHistory.create!(
      start_time: start_time,
      end_time: end_time,
      count: suppressions.size,
      body: suppressions.map(&:to_s).join("\n")
    )
  rescue => e
    if ignore_errors
      warn "#{e} (ignored)"
      SendgridStatusUpdateHistory.create(
        start_time: start_time,
        end_time: end_time,
        count: suppressions.size,
        body: "" # ignored
      )
    else
      raise
    end
  end
end

.clientObject



89
90
91
# File 'app/models/sendgrid_notification/sendgrid_status.rb', line 89

def self.client
  SendgridClient.new
end

.retrieve(type, start_time, end_time, limit = 100) ⇒ Object

type is one of SendgridClient::SUPPRESSION_TYPES start,end_time is Time or Integer



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/sendgrid_notification/sendgrid_status.rb', line 67

def self.retrieve(type, start_time, end_time, limit = 100)
  _client = client
  offset = 0
  suppressions = []
  begin
    res = _client.suppression_get(type,
            start_time: start_time.to_i, end_time: end_time.to_i,
            limit: limit, offset: offset)
    # FIXME: if timeout?
    if res.success?
      # res.body is type of [{}]
      results = res.body.map { |h| Suppression.build(type.to_s, h) }
      suppressions.concat results
    else
      # Rails.logger.warn "sendgrid returns status code #{res.status_code}. (#{type}, #{start_time}, #{end_time})"
      raise RetrieveError, "sendgrid returns status code #{res.status_code}. (#{type}, #{start_time}, #{end_time})"
    end
    offset += limit
  end while results.size == limit
  suppressions
end

.retrieve_all(start_time, end_time) ⇒ Object



58
59
60
61
62
63
# File 'app/models/sendgrid_notification/sendgrid_status.rb', line 58

def self.retrieve_all(start_time, end_time)
  SendgridClient::SUPPRESSION_TYPES.each_with_object([]) do |type, suppressions|
    result = retrieve(type, start_time, end_time)
    suppressions.concat result
  end
end

.update(start_time = 1.hour.ago, end_time = Time.now) ⇒ Object

TODO: 前回の保存日時からの内容を記録



50
51
52
53
54
55
56
# File 'app/models/sendgrid_notification/sendgrid_status.rb', line 50

def self.update(start_time = 1.hour.ago, end_time = Time.now)
  suppressions = retrieve_all(start_time, end_time)
  suppressions.each do |s|
    update_mail_history(s.email, s.created, s.type, s.reason)
  end
  suppressions
end

.update_mail_history(mailto, status_timestamp, error_type, error_reason, relation = MailHistory.all) ⇒ Object

status_timestamp allows Integer unix timestamp



94
95
96
97
98
99
100
101
# File 'app/models/sendgrid_notification/sendgrid_status.rb', line 94

def self.update_mail_history(mailto, status_timestamp, error_type, error_reason, relation = MailHistory.all)
  target = relation.order(:sent_at).where(to: mailto).where('sent_at <= ?', Time.at(status_timestamp)).first
  if target
    target.update!(error_type: error_type, error_reason: error_reason)
  else
    Rails.logger.warn("SendgridStatus::Updater#update: Fail to find history; mailto: #{mailto}, created: #{status_timestamp}, error_type: #{error_type}, error_reason: #{error_reason}")
  end
end