inst-jobs-autoscaling 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/Dockerfile +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/build.sh +5 -0
- data/docker-compose.yml +2 -0
- data/inst-jobs-autoscaling.gemspec +28 -0
- data/lib/jobs_autoscaling.rb +9 -0
- data/lib/jobs_autoscaling/aws_action.rb +41 -0
- data/lib/jobs_autoscaling/logger_action.rb +17 -0
- data/lib/jobs_autoscaling/monitor.rb +35 -0
- data/lib/jobs_autoscaling/version.rb +3 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a3f9fbfdd0d13f234e05ebc243dbe26ae16673e4
|
4
|
+
data.tar.gz: 88e985211fecb32e66ac8d1c3c4c11bff309e207
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 11543a9b36146b72fe984410c9decb325baddf58e5eacc70447f774da575298adf5abe36034cc016e9966864e091bce15c26e25460555bd120b0db9d9e0c0ec9
|
7
|
+
data.tar.gz: 18a70e446be14da423e0ecba10d6a8880ba814b485c8d6b1d86d93030c0a4c72690bf773dc9a503ccdd8bc47996ec545afcadacbab83e8806eeb16951ac335a2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
FROM instructure/ruby:2.1
|
2
|
+
|
3
|
+
ADD Gemfile* *.gemspec /usr/src/app/
|
4
|
+
ADD lib/jobs_autoscaling/version.rb /usr/src/app/lib/jobs_autoscaling/
|
5
|
+
USER root
|
6
|
+
RUN bundle install
|
7
|
+
ADD . /usr/src/app/
|
8
|
+
RUN chown -R docker:docker /usr/src/app
|
9
|
+
USER docker
|
10
|
+
|
11
|
+
CMD ["rake"]
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Instructure Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Jobs Autoscaling
|
2
|
+
|
3
|
+
This [inst-jobs](https://github.com/instructure/inst-jobs) plugin enables
|
4
|
+
autoscaling of job worker pools using AWS Autoscaling Groups.
|
5
|
+
|
6
|
+
Using this plugin requires configuring inst-jobs to use the ParentProcess
|
7
|
+
WorkQueue implementation, which is not currently the default.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'inst-jobs-autoscaling'
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Before using the plugin you'll need to configure your jobs workers to run as an
|
20
|
+
AWS ASG. This typically means creating an AMI containing your application's
|
21
|
+
code, and then setting scaleup/scaledown actions based on either CPU or job
|
22
|
+
queue length.
|
23
|
+
|
24
|
+
To enable the plugin you'll need to provide the ASG name to the app via an ENV
|
25
|
+
var or other means, and add a line in an application initializer:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'jobs_autoscaling'
|
29
|
+
action = JobsAutoscaling::AwsAction.new(asg_name: "my_asg_name")
|
30
|
+
autoscaler = JobsAutoscaling::Monitor.new(action: action)
|
31
|
+
autoscaler.activate!
|
32
|
+
```
|
33
|
+
|
34
|
+
To just log what actions would be taken, rather than make actual ASG API calls,
|
35
|
+
use the `LoggerAction`:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
action = JobsAutoscaling::LoggerAction.new
|
39
|
+
```
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jobs_autoscaling"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
data/build.sh
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jobs_autoscaling/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "inst-jobs-autoscaling"
|
8
|
+
spec.version = JobsAutoscaling::VERSION
|
9
|
+
spec.authors = ["Brian Palmer"]
|
10
|
+
spec.email = ["brianp@instructure.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{AWS ASG autoscaling for inst-jobs}
|
13
|
+
spec.homepage = "https://github.com/instructure/inst-jobs-autoscaling"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "inst-jobs", ">= 0.11.0"
|
22
|
+
spec.add_dependency "aws-sdk", ">= 2.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.4.0"
|
28
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module JobsAutoscaling
|
5
|
+
class AwsAction
|
6
|
+
attr_reader :asg_name, :client
|
7
|
+
|
8
|
+
def initialize(asg_name:)
|
9
|
+
@asg_name = asg_name
|
10
|
+
@client = Aws::AutoScaling::Client.new(retry_limit: 10)
|
11
|
+
end
|
12
|
+
|
13
|
+
def idle
|
14
|
+
@client.set_instance_protection(
|
15
|
+
protected_from_scale_in: false,
|
16
|
+
auto_scaling_group_name: asg_name,
|
17
|
+
instance_ids: [instance_id],
|
18
|
+
)
|
19
|
+
end
|
20
|
+
|
21
|
+
# it's intentional that if this call fails, the error bubbles up to
|
22
|
+
# inst-jobs and errors the WorkQueue. We don't want to start running the
|
23
|
+
# job if we weren't able to block scaledowns.
|
24
|
+
def busy
|
25
|
+
@client.set_instance_protection(
|
26
|
+
protected_from_scale_in: true,
|
27
|
+
auto_scaling_group_name: asg_name,
|
28
|
+
instance_ids: [instance_id],
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
# This is the hard-coded EC2 endpoint for getting instance metadata.
|
33
|
+
# Oddly the ruby SDK doesn't include a method to get this information,
|
34
|
+
# you are expected to just hit the endpoint yourself.
|
35
|
+
INSTANCE_ID_ENDPOINT = 'http://169.254.169.254/latest/meta-data/instance-id'
|
36
|
+
|
37
|
+
def instance_id
|
38
|
+
@instance_id ||= Net::HTTP.get(URI.parse(INSTANCE_ID_ENDPOINT))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module JobsAutoscaling
|
2
|
+
class LoggerAction
|
3
|
+
def idle
|
4
|
+
log_change("idle")
|
5
|
+
end
|
6
|
+
|
7
|
+
def busy
|
8
|
+
log_change("busy")
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def log_change(new_state)
|
14
|
+
Rails.logger.info("Worker state is now: #{new_state}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'delayed_job'
|
2
|
+
|
3
|
+
module JobsAutoscaling
|
4
|
+
class Monitor
|
5
|
+
IDLE = :idle
|
6
|
+
BUSY = :busy
|
7
|
+
|
8
|
+
def initialize(action: action)
|
9
|
+
@action = action
|
10
|
+
end
|
11
|
+
|
12
|
+
def activate!
|
13
|
+
Delayed::Worker.lifecycle.after(:work_queue_pop, &method(:work_queue_pop))
|
14
|
+
change_state(IDLE)
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def work_queue_pop(work_queue)
|
20
|
+
unless work_queue.respond_to?(:all_workers_idle?)
|
21
|
+
raise("JobsAutoscaling requires a ParentProcess work queue")
|
22
|
+
end
|
23
|
+
|
24
|
+
new_state = work_queue.all_workers_idle? ? IDLE : BUSY
|
25
|
+
if new_state != @worker_state
|
26
|
+
change_state(new_state)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def change_state(new_state)
|
31
|
+
@worker_state = new_state
|
32
|
+
@action.public_send(new_state)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inst-jobs-autoscaling
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Palmer
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: inst-jobs
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.11.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.11.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.4.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.4.0
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- brianp@instructure.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- Dockerfile
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- build.sh
|
114
|
+
- docker-compose.yml
|
115
|
+
- inst-jobs-autoscaling.gemspec
|
116
|
+
- lib/jobs_autoscaling.rb
|
117
|
+
- lib/jobs_autoscaling/aws_action.rb
|
118
|
+
- lib/jobs_autoscaling/logger_action.rb
|
119
|
+
- lib/jobs_autoscaling/monitor.rb
|
120
|
+
- lib/jobs_autoscaling/version.rb
|
121
|
+
homepage: https://github.com/instructure/inst-jobs-autoscaling
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.5.1
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: AWS ASG autoscaling for inst-jobs
|
145
|
+
test_files: []
|