-
Notifications
You must be signed in to change notification settings - Fork 0
/
spot_helper.rb
49 lines (41 loc) · 1.39 KB
/
spot_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
module SpotHelper
def SpotHelper.sqs_client
Aws::SQS::Client.new({
region: ENV['AWS_REGION'],
credentials: Aws::Credentials.new(
ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])})
end
def SpotHelper.sqs_poller(url)
Aws::SQS::QueuePoller.new(url)
end
def SpotHelper.ec2_client
Aws::EC2::Client.new({
region: ENV['AWS_REGION'],
credentials: Aws::Credentials.new(
ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])})
end
def SpotHelper.add_buffer_to_price(price)
new_price = price.to_f
((new_price + (new_price * 0.2)).round(3)).to_s
end
def SpotHelper.all_zones
self.ec2_client.describe_availability_zones.
availability_zones.map(&:zone_name)
end
def SpotHelper.number_of_jobs_in(url)
sqs_client.get_queue_attributes(
queue_url: url,
attribute_names: ['ApproximateNumberOfMessages']).
attributes['ApproximateNumberOfMessages'].to_f
end
def SpotHelper.get_self_instance_id
`curl http://169.254.169.254/latest/meta-data/instance-id`
end
def SpotHelper.ratio_of_backlog_to_wip
backlog_queue_url = 'https://sqs.us-west-2.amazonaws.com/828660616807/backlog'
wip_queue_url = 'https://sqs.us-west-2.amazonaws.com/828660616807/wip'
wip = number_of_jobs_in(wip_queue_url)
wip = wip == 0.0 ? 0.01 : wip # avoids devision by 0 exception
number_of_jobs_in(backlog_queue_url) / wip
end
end