Module: MovableInk::AWS::SNS

Included in:
MovableInk::AWS
Defined in:
lib/movable_ink/aws/sns.rb

Instance Method Summary collapse

Instance Method Details

#add_subject_info(subject:) ⇒ Object



79
80
81
82
# File 'lib/movable_ink/aws/sns.rb', line 79

def add_subject_info(subject:)
  required_info = " (#{instance_id}, #{my_region})"
  "#{subject.slice(0, 99-required_info.length)}#{required_info}"
end

#notify_and_sleep(seconds, error_class) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/movable_ink/aws/sns.rb', line 32

def notify_and_sleep(seconds, error_class)
  message = "Throttled by AWS. Sleeping #{seconds} seconds, (#{error_class})"
  notify_slack(subject: 'API Throttled',
               message: message)
  puts message
  sleep seconds
end

#notify_nsq_can_not_be_drainedObject



40
41
42
43
44
# File 'lib/movable_ink/aws/sns.rb', line 40

def notify_nsq_can_not_be_drained
  notify_slack(subject: 'NSQ not drained',
               message: "Unable to drain NSQ for instance <https://#{my_region}.console.aws.amazon.com/ec2/v2/home?region=#{my_region}#Instances:search=#{instance_id};sort=instanceId|#{instance_id}>")
  notify_pagerduty(region: my_region, instance_id: instance_id)
end

#notify_pagerduty(region:, instance_id:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/movable_ink/aws/sns.rb', line 46

def notify_pagerduty(region:, instance_id:)
  summary = "Unable to drain NSQ for instance #{instance_id} in region #{region}"

  # the PagerDuty integration key is added to the payload in the AWS integration
  json_message = {
    pagerduty: {
      event_action: 'trigger',
      payload: {
        source: 'MovableInkAWS',
        summary: summary,
        timestamp: Time.now.utc.iso8601,
        severity: 'error',
        component: 'nsq',
        group: 'nsq',
        custom_details: {
          InstanceId: instance_id,
        },
      },
      dedup_key: "nsq-not-draining-#{instance_id}",
      links: [{
        href: "https://#{region}.console.aws.amazon.com/ec2/v2/home?region=#{region}#Instances:search=#{instance_id};sort=instanceId",
        text: 'View Instance'
      }],
    }
  }.to_json

  run_with_backoff do
    sns.publish(topic_arn: sns_pagerduty_topic_arn,
                subject: add_subject_info(subject: "Unable to drain NSQ"),
                message: json_message)
  end
end

#notify_slack(subject:, message:) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/movable_ink/aws/sns.rb', line 84

def notify_slack(subject:, message:)
  run_with_backoff do
    sns.publish(topic_arn: sns_slack_topic_arn,
                subject: add_subject_info(subject: subject),
                message: message)
  end
end

#sns(region: my_region) ⇒ Object



7
8
9
10
# File 'lib/movable_ink/aws/sns.rb', line 7

def sns(region: my_region)
  @sns_client ||= {}
  @sns_client[region] ||= Aws::SNS::Client.new(region: region)
end

#sns_pagerduty_topic_arnObject



22
23
24
25
26
27
28
29
30
# File 'lib/movable_ink/aws/sns.rb', line 22

def sns_pagerduty_topic_arn
  run_with_backoff do
    sns.list_topics.each do |resp|
      resp.topics.each do |topic|
        return topic.topic_arn if topic.topic_arn.include? "pagerduty-custom-alerts"
      end
    end
  end
end

#sns_slack_topic_arnObject



12
13
14
15
16
17
18
19
20
# File 'lib/movable_ink/aws/sns.rb', line 12

def sns_slack_topic_arn
  run_with_backoff do
    sns.list_topics.each do |resp|
      resp.topics.each do |topic|
        return topic.topic_arn if topic.topic_arn.include? "slack-aws-alerts"
      end
    end
  end
end