ready-for-migration 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0594c87355e8056fb15067e0aab39c234d56f603e10377f86fde0b65d48e3fdf'
4
- data.tar.gz: c140de4c6b325e2655bffb64abdc3fc3b6eb95cc85ae49053679275baad3e78c
3
+ metadata.gz: 2bbd767ff83942c2a4350e319b1e5bde3e89f2367020b0dec977b20a5471be94
4
+ data.tar.gz: 2acef1d15ecdddc72940d2c6fd59d146637f3311a5f67c07aa40a545543459f9
5
5
  SHA512:
6
- metadata.gz: affa9b3b0aa6def18e31f46876232f96b6497b7c48528fa13f9059950a1a063606ba053d6996da3beec1e2cf51743ae72cdcbb7ee571787655a740229a9ae6fb
7
- data.tar.gz: 3fee0628bb2f9192fb508986dfcb346c519e3ab08b989cbca07442fc0285366c8ba4d69f8b7ede426f404fe45de6d9330f527ba0a13f70a1dca8d9b854a16ecd
6
+ metadata.gz: 03e072cff695d1913c05116d78372dc84b673569328b6de0a25f2c83fd8d073fc39f3e42dc730635234627b0ccf0329ce07de1670597b417da995c4794b70078
7
+ data.tar.gz: ebe49ef79d0bafbd7f6fcc42243e91eeae3902f61e9380ae058fc00cc59c3c835db5220f56b90a9406551fd16eba89d4a645c716aeba04930556b2457a3fae6a
data/README.md CHANGED
@@ -29,5 +29,23 @@ Or install it yourself as:
29
29
  $ gem install ready-for-migration
30
30
  ```
31
31
 
32
+ ## Simulate Health Check Response
33
+
34
+ You can simulate health check response by adding specific query parameters to your health check requests.
35
+
36
+ Here's the pattern we're providing. (values are just an example)
37
+
38
+ |pattern|behavior|
39
+ |--|--|
40
+ |No query parameters|health check as usual.|
41
+ |`?status=503`|respond with specified status code.|
42
+ |`?status=503&after=60`|respond with specified status code once specified seconds have passed since the first access with `after`.|
43
+ |`?status=503&sleep=60`|respond with specified status code after sleeping specified seconds.|
44
+ |`?status=503&random=10` |randomly respond with specified status code, once in a specified number as `random`.|
45
+ |`?sleep=60` |health check after sleeping specified seconds.|
46
+
47
+ NOTE: Other patterns than listed in the above example will be ignored.
48
+ NOTE: "sleep" parameter is disabled in default for avoiding DoS attack. To enabling this, you set environment variables "RFM_ENABLE_SLEEP" as "1".
49
+
32
50
  ## License
33
51
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,112 @@
1
+ require 'active_support/cache/file_store'
2
+
3
+ module Ready
4
+ module For
5
+ module Migration
6
+ module HealthActionInspectable
7
+
8
+ def self.cache
9
+ @cache ||= ActiveSupport::Cache::MemoryStore.new
10
+ end
11
+
12
+ def self.sleep_enabled?
13
+ return false if ENV['RFM_ENABLE_SLEEP'].nil?
14
+ b = [ false, 0,
15
+ "0", :"0",
16
+ "f", :f,
17
+ "F", :F,
18
+ "false", :false,
19
+ "FALSE", :FALSE,
20
+ "off", :off,
21
+ "OFF", :OFF,
22
+ ].include?(ENV['RFM_ENABLE_SLEEP'])
23
+ return !b
24
+ end
25
+
26
+ def readiness
27
+ if params.has_key?(:status)
28
+ case
29
+ when params.has_key?(:after)
30
+ perform_inspection_with_status_and_after
31
+ when params.has_key?(:sleep)
32
+ perform_inspection_with_status_and_sleep
33
+ when params.has_key?(:random)
34
+ perform_inspection_with_status_and_random
35
+ else
36
+ perform_inspection_with_status
37
+ end
38
+ elsif params.has_key?(:sleep)
39
+ perform_inspection_with_sleep
40
+ end
41
+
42
+ super unless performed?
43
+ end
44
+
45
+ private
46
+
47
+ def get_status_value
48
+ raw = params.dig(:status)
49
+ status = raw.to_i if raw && /^\d{3}$/.match?(raw)
50
+ status
51
+ end
52
+
53
+ def get_after_value
54
+ raw = params.dig(:after)
55
+ value = raw.to_i if raw && /^\d+$/.match?(raw)
56
+ value
57
+ end
58
+
59
+ def get_random_value
60
+ raw = params.dig(:random)
61
+ value = raw.to_i if raw && /^\d+$/.match?(raw)
62
+ value
63
+ end
64
+
65
+ def get_sleep_value
66
+ raw = params.dig(:sleep)
67
+ value = raw.to_i if raw && /^\d+$/.match?(raw)
68
+ value
69
+ end
70
+
71
+ def perform_inspection_with_status_and_after
72
+ return unless status = get_status_value
73
+ return unless value = get_after_value
74
+
75
+ first_access_time = Ready::For::Migration::HealthActionInspectable.cache.fetch(:first_access_time) { Time.now }
76
+ if first_access_time + value < Time.now
77
+ head status
78
+ end
79
+ end
80
+
81
+ def perform_inspection_with_status_and_sleep
82
+ return unless status = get_status_value
83
+ return unless value = get_sleep_value
84
+
85
+ sleep(value) if Ready::For::Migration::HealthActionInspectable.sleep_enabled?
86
+ head status
87
+ end
88
+
89
+ def perform_inspection_with_status_and_random
90
+ return unless status = get_status_value
91
+ return unless value = get_random_value
92
+
93
+ if value == 1 || rand(value - 1) == 0
94
+ head status
95
+ end
96
+ end
97
+
98
+ def perform_inspection_with_status
99
+ return unless status = get_status_value
100
+ head status
101
+ end
102
+
103
+ def perform_inspection_with_sleep
104
+ return unless value = get_sleep_value
105
+
106
+ sleep(value) if Ready::For::Migration::HealthActionInspectable.sleep_enabled?
107
+ end
108
+
109
+ end
110
+ end
111
+ end
112
+ end
@@ -2,6 +2,7 @@ module Ready
2
2
  module For
3
3
  module Migration
4
4
  class HealthController < ::ActionController::Base
5
+ prepend HealthActionInspectable
5
6
 
6
7
  def readiness
7
8
  if ApplicationRecord.connection.migration_context.needs_migration?
@@ -2,6 +2,10 @@ module Ready
2
2
  module For
3
3
  module Migration
4
4
  class Engine < ::Rails::Engine
5
+
6
+ initializer 'ready-for-migration.initialize_cache' do
7
+ Ready::For::Migration::HealthActionInspectable.cache.write(:first_access_time, Time.now)
8
+ end
5
9
  end
6
10
  end
7
11
  end
@@ -1,7 +1,7 @@
1
1
  module Ready
2
2
  module For
3
3
  module Migration
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.3'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ready-for-migration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - bizside-developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-28 00:00:00.000000000 Z
11
+ date: 2023-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -60,6 +60,7 @@ files:
60
60
  - MIT-LICENSE
61
61
  - README.md
62
62
  - Rakefile
63
+ - app/controllers/concerns/ready/for/migration/health_action_inspectable.rb
63
64
  - app/controllers/ready/for/migration/health_controller.rb
64
65
  - config/routes.rb
65
66
  - lib/ready/for/migration.rb
@@ -84,8 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  requirements: []
87
- rubyforge_project:
88
- rubygems_version: 2.7.6.2
88
+ rubygems_version: 3.1.6
89
89
  signing_key:
90
90
  specification_version: 4
91
91
  summary: ready for migration