Module: MovableInk::AWS::SNS

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

Instance Method Summary collapse

Instance Method Details

#notify_and_sleep(seconds, error_class) ⇒ Object



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

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_drained ⇒ Object



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

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/movable_ink/aws/sns.rb', line 38

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: "Unable to drain NSQ",
                message: json_message)
  end
end

#notify_slack(subject:, message:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/movable_ink/aws/sns.rb', line 71

def notify_slack(subject:, message:)
  instance_link = "<https://#{my_region}.console.aws.amazon.com/ec2/v2/home?region=#{my_region}#Instances:search=#{instance_id};sort=instanceId|#{instance_id}>"
  required_info = "Instance: #{instance_link}, `#{private_ipv4}`, `#{my_region}`"

  run_with_backoff do
    sns.publish(topic_arn: sns_slack_topic_arn,
                subject: subject.slice(0,99),
                message: "#{required_info}\n#{message}")
  end
end

#sns(region: my_region) ⇒ Object



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

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

#sns_pagerduty_topic_arn ⇒ Object



17
18
19
# File 'lib/movable_ink/aws/sns.rb', line 17

def sns_pagerduty_topic_arn
  @sns_pagerduty_topic_arn ||= sns_topics.detect { |topic| topic.topic_arn.include? "pagerduty-custom-alerts" }.topic_arn rescue nil
end

#sns_slack_topic_arn ⇒ Object



13
14
15
# File 'lib/movable_ink/aws/sns.rb', line 13

def sns_slack_topic_arn
  @sns_slack_topic_arn ||= sns_topics.detect { |topic| topic.topic_arn.include? "slack-aws-alerts" }.topic_arn rescue nil
end

#sns_topics ⇒ Object



21
22
23
# File 'lib/movable_ink/aws/sns.rb', line 21

def sns_topics
  @sns_topics ||= run_with_backoff { sns.list_topics.flat_map(&:topics) }
end