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



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

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



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

#notify_slack(subject:, message:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/movable_ink/aws/sns.rb', line 79

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



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

def sns_pagerduty_topic_arn
  @sns_pagerduty_topic_arn ||= run_with_backoff do
    sns_topics.each do |topic|
      return topic.topic_arn if topic.topic_arn.include? "pagerduty-custom-alerts"
    end
  end
end

#sns_slack_topic_arn ⇒ Object



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

def sns_slack_topic_arn
  @sns_slack_topic_arn ||= run_with_backoff do
    sns_topics.each do |topic|
      return topic.topic_arn if topic.topic_arn.include? "slack-aws-alerts"
    end
  end
end

#sns_topics ⇒ Object



29
30
31
# File 'lib/movable_ink/aws/sns.rb', line 29

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