redis_cloud_auto_upgrade 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2db871f69cbadb7c351fed5915610fefec43aac7
4
- data.tar.gz: ceb75e6153ab57d20e8d682418fd2495420f67de
3
+ metadata.gz: 494f5b0990ddbf04156d7d310084dcd1baacbe60
4
+ data.tar.gz: 551620dfdcb94c70813279385311f7ce5c419a43
5
5
  SHA512:
6
- metadata.gz: 6347ceefcea9f63e783b59201daeedca16ada8f66db7f963ba355c7c4aad03081660a926328b17bce62a7557ef903eb91e538f432e24216d2f3631b587e8b5ef
7
- data.tar.gz: ee380942da44a27f296751c2914cfa11820ee6cfc19ceaf8167b7a358d3aa590dfd2330e03421704040c4df8925db98149c86218bf252fea47663e854de58806
6
+ metadata.gz: 6d09a535c278efa6d9989553683a6d6013962c17a1c356b4a88fbe04d9113b7f18455a97f1bdd3fad4c38b6c287ad52f15894de68327ec2a83aaf55f168558a9
7
+ data.tar.gz: 20abd85a69edcb43abca63d2afc5686b565a92b0dd082fe9c46fd9b6946d2184cc12b1b5ebc9ef4e1c71eb8fc34e9ee50a9918c48610b307d191de4da8bd2ab2
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  require_relative './redis_cloud_auto_upgrade/version'
2
3
  require_relative './redis_cloud_auto_upgrade/exceptions'
3
4
  require_relative './redis_cloud_auto_upgrade/configuration'
5
+ require_relative './redis_cloud_auto_upgrade/dependent_values'
6
+ require_relative './redis_cloud_auto_upgrade/class_methods'
4
7
 
5
8
  require_relative './redis_cloud_auto_upgrade/heroku_api'
6
9
 
@@ -8,14 +11,12 @@ require 'redis'
8
11
 
9
12
  # See README.md for details
10
13
  class RedisCloudAutoUpgrade
11
- class << self
12
- def potential_upgrade!(conf, &blk)
13
- updated_conf = conf.merge(on_upgrade: blk)
14
- new
15
- .configure(updated_conf)
16
- .potential_upgrade!
17
- end
18
- end # class << self
14
+ include self::DependentValues
15
+ extend self::ClassMethods
16
+
17
+ def initialize
18
+ @config = Configuration.new
19
+ end
19
20
 
20
21
  def configure(config)
21
22
  @config.configure config
@@ -24,7 +25,7 @@ class RedisCloudAutoUpgrade
24
25
 
25
26
  def current_redis_cloud_plan
26
27
  @__current_redis_cloud_plan ||=
27
- HerokuAPI.current_redis_cloud_plan(**heroku_params)
28
+ HerokuAPI.current_redis_cloud_plan(**heroku_params)
28
29
  end
29
30
 
30
31
  # Memoize from lab42_core gem?
@@ -35,29 +36,23 @@ class RedisCloudAutoUpgrade
35
36
  end
36
37
 
37
38
  def needs_to_upgrade?
38
- !(current_redis_mem_usage < currently_available_memory * config.treshhold)
39
+ !(current_redis_mem_usage < currently_available_memory * config.treshhold)
39
40
  end
40
41
 
41
42
  def potential_upgrade!
42
- if config.valid?
43
- do_potential_upgrade!
44
- else
45
- fail IllegalConfiguration, config.errors_human_readable
46
- end
43
+ raise IllegalConfiguration, config.errors_human_readable unless config.valid?
44
+ do_potential_upgrade!
47
45
  end
48
46
 
49
47
  private
50
48
 
51
- def initialize
52
- @config = Configuration.new
53
- end
54
-
55
49
  attr_reader :config
56
50
 
57
51
  def currently_available_memory
58
52
  @__currently_available_memory__ ||=
59
53
  HerokuAPI.currently_available_memory(
60
- **config.only(:heroku_api_key, :heroku_app_name))
54
+ **config.only(:heroku_api_key, :heroku_app_name)
55
+ )
61
56
  end
62
57
 
63
58
  def do_potential_upgrade!
@@ -65,7 +60,7 @@ class RedisCloudAutoUpgrade
65
60
  do_upgrade!
66
61
  true
67
62
  else
68
- info "no upgrade needed #{config.heroku_app_name} mem usage #{current_redis_mem_usage / 1_000_000}MB"
63
+ info_no_upgrade
69
64
  false
70
65
  end
71
66
  end
@@ -85,7 +80,7 @@ upgraded RedisCloud plan for app: #{config.heroku_app_name}
85
80
  mem usage was approximately #{current_redis_mem_usage / 1_000_000}MB
86
81
  old_plan was #{old_plan}
87
82
  new_plan is #{new_plan}
88
- EOS
83
+ EOS
89
84
  end
90
85
 
91
86
  def heroku_params
@@ -98,9 +93,16 @@ new_plan is #{new_plan}
98
93
  config.logger.info([self.class.name, str].join(' '))
99
94
  end
100
95
 
101
- def mem_usage_in_percent
102
- @__mem_usage_in_percent__ ||=
103
- current_redis_mem_usage * 100 / currently_available_memory
96
+ def info_no_upgrade
97
+ msg = format(
98
+ 'no upgrade needed %s mem usage %dMB or %d%%, treshhold: %d%%, current plan: %s',
99
+ config.heroku_app_name,
100
+ current_redis_mem_usage_megab,
101
+ current_redis_mem_usage_percent,
102
+ treshhold_in_percent,
103
+ current_redis_cloud_plan
104
+ )
105
+ info msg
104
106
  end
105
107
 
106
108
  def update_data(old_plan, new_plan)
@@ -109,13 +111,8 @@ new_plan is #{new_plan}
109
111
  new_plan: new_plan,
110
112
  upgraded_at: Time.now,
111
113
  mem_usage: current_redis_mem_usage,
112
- mem_usage_in_percent: mem_usage_in_percent,
114
+ mem_usage_in_percent: current_redis_mem_usage_percent,
113
115
  treshhold_in_percent: treshhold_in_percent
114
116
  )
115
117
  end
116
-
117
- def treshhold_in_percent
118
- @__treshhold_in_percent__ ||=
119
- (config.treshhold * 100).round
120
- end
121
118
  end # class RedisCloudAutoUpgrade
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ # class level methods
3
+ module RedisCloudAutoUpgrade::ClassMethods
4
+ def potential_upgrade!(conf, &blk)
5
+ updated_conf = conf.merge(on_upgrade: blk)
6
+ new
7
+ .configure(updated_conf)
8
+ .potential_upgrade!
9
+ end
10
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # Configuration part
2
3
  class RedisCloudAutoUpgrade
3
4
  Configuration = Struct.new(
@@ -18,7 +19,7 @@ class RedisCloudAutoUpgrade
18
19
  end
19
20
 
20
21
  def only(*keys)
21
- keys.inject({})do |h, k|
22
+ keys.inject({}) do |h, k|
22
23
  h.merge(k => send(k))
23
24
  end
24
25
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ # Extracting behavior for compution of
3
+ # dependent values
4
+ module RedisCloudAutoUpgrade::DependentValues
5
+ private
6
+
7
+ def current_redis_mem_usage_megab
8
+ @__current_redis_mem_usage_megab__ ||=
9
+ current_redis_mem_usage / 1_000_000
10
+ rescue
11
+ 0
12
+ end
13
+
14
+ def current_redis_mem_usage_percent
15
+ @__current_redis_mem_usage_percent__ ||=
16
+ current_redis_mem_usage * 100 / currently_available_memory
17
+ rescue
18
+ 0
19
+ end
20
+
21
+ def treshhold_in_percent
22
+ @__treshhold_in_percent__ ||=
23
+ (config.treshhold * 100).round
24
+ end
25
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  class RedisCloudAutoUpgrade
2
3
  IllegalConfiguration = Class.new RuntimeError
3
4
  end # class RedisCloudAutoUpgrade
@@ -1,99 +1,102 @@
1
+ # frozen_string_literal: true
1
2
  require 'platform-api'
2
3
  # A function wrapper accessing the Heroku Platform API in a functional way
3
- class RedisCloudAutoUpgrade
4
- module HerokuAPI
5
- class << self
6
- def available_memory(plan)
7
- plan_memory
8
- .fetch(plan) do
9
- fail ArgumentError, "the plan #{plan.inspect} does not exist"
10
- end
4
+ module RedisCloudAutoUpgrade::HerokuAPI
5
+ class << self
6
+ def available_memory(plan)
7
+ plan_memory
8
+ .fetch(plan) do
9
+ raise ArgumentError, "the plan #{plan.inspect} does not exist"
11
10
  end
11
+ end
12
12
 
13
- def current_redis_cloud_plan(heroku_api_key:, heroku_app_name:)
14
- heroku(heroku_api_key)
15
- .addon
16
- .list_by_app(heroku_app_name)
17
- .find(&select_redis_cloud_addon)['plan']['name']
18
- end
13
+ def current_redis_cloud_plan(heroku_api_key:, heroku_app_name:)
14
+ heroku(heroku_api_key)
15
+ .addon
16
+ .list_by_app(heroku_app_name)
17
+ .find(&select_redis_cloud_addon)['plan']['name']
18
+ end
19
19
 
20
- def currently_available_memory(heroku_api_key:, heroku_app_name:)
21
- available_memory(
22
- current_redis_cloud_plan(
23
- heroku_api_key: heroku_api_key, heroku_app_name: heroku_app_name))
24
- end
20
+ def currently_available_memory(heroku_api_key:, heroku_app_name:)
21
+ available_memory(
22
+ current_redis_cloud_plan(
23
+ heroku_api_key: heroku_api_key, heroku_app_name: heroku_app_name
24
+ )
25
+ )
26
+ end
25
27
 
26
- def next_plan_to_upgrade_to(heroku_api_key:, heroku_app_name:)
27
- next_plan(
28
- current_redis_cloud_plan(
29
- heroku_api_key: heroku_api_key, heroku_app_name: heroku_app_name))
30
- end
28
+ def next_plan_to_upgrade_to(heroku_api_key:, heroku_app_name:)
29
+ next_plan(
30
+ current_redis_cloud_plan(
31
+ heroku_api_key: heroku_api_key, heroku_app_name: heroku_app_name
32
+ )
33
+ )
34
+ end
31
35
 
32
- def upgrade_plan!(heroku_api_key:, heroku_app_name:)
33
- next_plan =
34
- next_plan_to_upgrade_to(heroku_api_key: heroku_api_key, heroku_app_name: heroku_app_name)
35
- heroku(heroku_api_key)
36
- .addon
37
- .update(heroku_app_name, 'rediscloud', plan: next_plan)
38
- next_plan
39
- end
36
+ def upgrade_plan!(heroku_api_key:, heroku_app_name:)
37
+ next_plan =
38
+ next_plan_to_upgrade_to(heroku_api_key: heroku_api_key, heroku_app_name: heroku_app_name)
39
+ heroku(heroku_api_key)
40
+ .addon
41
+ .update(heroku_app_name, 'rediscloud', plan: next_plan)
42
+ next_plan
43
+ end
40
44
 
41
- private
45
+ private
42
46
 
43
- def heroku(api_key)
44
- PlatformAPI.connect api_key
45
- end
47
+ def heroku(api_key)
48
+ PlatformAPI.connect api_key
49
+ end
46
50
 
47
- def next_plan(plan)
48
- plan_transitions
49
- .fetch(plan) do
50
- fail ArgumentError, "the plan #{plan.inspect} does not exist or cannot be upgraded"
51
- end
51
+ def next_plan(plan)
52
+ plan_transitions
53
+ .fetch(plan) do
54
+ raise ArgumentError, "the plan #{plan.inspect} does not exist or cannot be upgraded"
52
55
  end
56
+ end
53
57
 
54
- # rubocop:disable Metrics/MethodLength
55
- def plan_transitions
56
- {
57
- 'rediscloud:30' => 'rediscloud:100',
58
- 'rediscloud:100' => 'rediscloud:250',
59
- 'rediscloud:250' => 'rediscloud:500',
60
- 'rediscloud:500' => 'rediscloud:1_000',
61
- 'rediscloud:1_000' => 'rediscloud:2_500',
62
- 'rediscloud:2_500' => 'rediscloud:5_000',
63
- 'rediscloud:5_000' => 'rediscloud:10_000',
64
- 'rediscloud:10_000' => 'rediscloud:15_000',
65
- 'rediscloud:15_000' => 'rediscloud:20_000',
66
- 'rediscloud:20_000' => 'rediscloud:25_000',
67
- 'rediscloud:25_000' => 'rediscloud:50_000'
68
- }
69
- end
58
+ # rubocop:disable Metrics/MethodLength
59
+ def plan_transitions
60
+ {
61
+ 'rediscloud:30' => 'rediscloud:100',
62
+ 'rediscloud:100' => 'rediscloud:250',
63
+ 'rediscloud:250' => 'rediscloud:500',
64
+ 'rediscloud:500' => 'rediscloud:1000',
65
+ 'rediscloud:1000' => 'rediscloud:2500',
66
+ 'rediscloud:2500' => 'rediscloud:5000',
67
+ 'rediscloud:5000' => 'rediscloud:10000',
68
+ 'rediscloud:10000' => 'rediscloud:15000',
69
+ 'rediscloud:15000' => 'rediscloud:20000',
70
+ 'rediscloud:20000' => 'rediscloud:25000',
71
+ 'rediscloud:25000' => 'rediscloud:50000'
72
+ }
73
+ end
70
74
 
71
- def plan_memory
72
- {
73
- 'rediscloud:30' => 30_000_000,
74
- 'rediscloud:100' => 100_000_000,
75
- 'rediscloud:250' => 250_000_000,
76
- 'rediscloud:500' => 500_000_000,
77
- 'rediscloud:1000' => 1_000_000_000,
78
- 'rediscloud:2500' => 2_500_000_000,
79
- 'rediscloud:5000' => 5_000_000_000,
80
- 'rediscloud:10000' => 10_000_000_000,
81
- 'rediscloud:15000' => 15_000_000_000,
82
- 'rediscloud:20000' => 20_000_000_000,
83
- 'rediscloud:25000' => 25_000_000_000,
84
- 'rediscloud:50000' => 50_000_000_000
85
- }
86
- end
75
+ def plan_memory
76
+ {
77
+ 'rediscloud:30' => 30_000_000,
78
+ 'rediscloud:100' => 100_000_000,
79
+ 'rediscloud:250' => 250_000_000,
80
+ 'rediscloud:500' => 500_000_000,
81
+ 'rediscloud:1000' => 1_000_000_000,
82
+ 'rediscloud:2500' => 2_500_000_000,
83
+ 'rediscloud:5000' => 5_000_000_000,
84
+ 'rediscloud:10000' => 10_000_000_000,
85
+ 'rediscloud:15000' => 15_000_000_000,
86
+ 'rediscloud:20000' => 20_000_000_000,
87
+ 'rediscloud:25000' => 25_000_000_000,
88
+ 'rediscloud:50000' => 50_000_000_000
89
+ }
90
+ end
87
91
 
88
- def select_redis_cloud_addon
89
- lambda do |addon|
90
- begin
91
- addon['addon_service']['name'] == 'rediscloud'
92
- rescue
93
- nil
94
- end
92
+ def select_redis_cloud_addon
93
+ lambda do |addon|
94
+ begin
95
+ addon['addon_service']['name'] == 'rediscloud'
96
+ rescue
97
+ nil
95
98
  end
96
99
  end
97
- end # class << self
98
- end # module HerokuAPI
99
- end # class RCAU
100
+ end
101
+ end # class << self
102
+ end # class RCAU::HerokuApi
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  class RedisCloudAutoUpgrade
2
- VERSION = '0.2.3'
3
+ VERSION = '0.3.0'
3
4
  end # class RedisCloudAutoUpgrade
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_cloud_auto_upgrade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-18 00:00:00.000000000 Z
11
+ date: 2016-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: 0.29.1
75
+ version: 0.40.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: 0.29.1
82
+ version: 0.40.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: timecop
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -132,7 +132,9 @@ files:
132
132
  - LICENSE
133
133
  - README.md
134
134
  - lib/redis_cloud_auto_upgrade.rb
135
+ - lib/redis_cloud_auto_upgrade/class_methods.rb
135
136
  - lib/redis_cloud_auto_upgrade/configuration.rb
137
+ - lib/redis_cloud_auto_upgrade/dependent_values.rb
136
138
  - lib/redis_cloud_auto_upgrade/exceptions.rb
137
139
  - lib/redis_cloud_auto_upgrade/heroku_api.rb
138
140
  - lib/redis_cloud_auto_upgrade/version.rb
@@ -148,7 +150,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
150
  requirements:
149
151
  - - ">="
150
152
  - !ruby/object:Gem::Version
151
- version: 2.2.0
153
+ version: 2.3.1
152
154
  required_rubygems_version: !ruby/object:Gem::Requirement
153
155
  requirements:
154
156
  - - ">="