hirefire-resource 0.7.0 → 0.7.1

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: e583f65c0c003cc4543482e3730952319ddc0069d97332f4f40b85701f61eebd
4
- data.tar.gz: efdc10ea1bf9f75333e4605a4ffbc7dd63e40d3e198a552badfb1a79bad21697
3
+ metadata.gz: 0bf414cb7e6529a04049ad3e7a7575a1932ec91fc8be57d8a2ce1ccc0922ab49
4
+ data.tar.gz: 8c78e71660e89df170ca061a328bfaff3d19714d55bf3a8fac04a59fbd9f2569
5
5
  SHA512:
6
- metadata.gz: 03fe9362bc04329ad02fccb8cc2836176bfbd1d06cef683a24dffa12f399386b592efcc1e8271b854c766a3c495a7d9abeda9e0736d59c86cda38723379c5690
7
- data.tar.gz: b80ca0e325253750e54771f9dcf03e60e88c99b999b2dff0f7b596186c23356095dad90fd5ddc0bde1c2ec57fd6cf2475f3d8452c260467f9ef9f7730aaffeef
6
+ metadata.gz: 2f84e86124b9467d166a839b3c90fd979ac13ccb4f50005dfa0e61e77763191206714a569fe59ea991da41b287d3a165a4740ea49f603ecc65caebc2d924c654
7
+ data.tar.gz: ce69dbda35b2762f727caa5e0d9fb5c76b3890ffa33cec9c20e46cc021d5b54f5842f337fc7264b00453dcf7074c3befd0db1970f89964ffb3703944d1837bb5
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "hirefire-resource"
5
- gem.version = "0.7.0"
5
+ gem.version = "0.7.1"
6
6
  gem.platform = Gem::Platform::RUBY
7
7
  gem.authors = "Michael van Rooijen"
8
8
  gem.email = "michael@hirefire.io"
@@ -3,104 +3,108 @@
3
3
  module HireFire
4
4
  class Middleware
5
5
 
6
- INFO_HEADERS = {
7
- "Content-Type" => "application/json",
8
- "Cache-Control" => "must-revalidate, private, max-age=0"
9
- }
10
-
11
- TEST_HEADERS = {
12
- "Content-Type" => "text/html"
13
- }
14
-
15
- # Initialize the HireFire::Middleware and store the `app` in `@app`
16
- # and `ENV["HIREFIRE_TOKEN"]` in `@token` for convenience.
6
+ # Initializes HireFire::Middleware.
17
7
  #
18
- # @param [ActionDispatch::Routing::RouteSet] app.
8
+ # @param [Proc] app call with `env` to continue down the middleware stack.
19
9
  #
20
10
  def initialize(app)
21
- @app = app
22
- @token = ENV["HIREFIRE_TOKEN"]
23
- if defined?(Rails) && Rails.application.config.relative_url_root
24
- @path_prefix = Regexp.new("^" + Regexp.escape(Rails.application.config.relative_url_root))
25
- end
11
+ @app = app
12
+ @token = ENV["HIREFIRE_TOKEN"]
13
+ @path_prefix = get_path_prefix
26
14
  end
27
15
 
28
- # Will respond to the request here if either the `test` or the `info` url was requested.
29
- # Otherwise, fall through to the rest of the middleware below HireFire::Middleware.
16
+ # Intercepts and handles the /hirefire/test, /hirefire/development/info,
17
+ # and /hirefire/HIREFIRE_TOKEN/info paths. If none of these paths match,
18
+ # then then request will continue down the middleware stack.
19
+ #
20
+ # When HireFire::Resource.log_queue_metrics is enabled, and the HTTP_X_REQUEST_START
21
+ # header has been injected at the Heroku Router layer, queue time information will be
22
+ # logged to STDOUT. This data can be used by the HireFire Logdrain with the
23
+ # Web.Logplex.QueueTime autoscaling strategy.
24
+ #
25
+ # Important: Don't set/update instance variables within this- or any underlying methods.
26
+ # Doing so may result in race conditions when using threaded application servers.
30
27
  #
31
28
  # @param [Hash] env containing request information.
32
29
  #
33
30
  def call(env)
34
- @env = env
35
-
36
31
  handle_queue(env["HTTP_X_REQUEST_START"])
37
32
 
38
- if test?
39
- [ 200, TEST_HEADERS, self ]
40
- elsif info?
41
- [ 200, INFO_HEADERS, self ]
33
+ if test_path?(env["PATH_INFO"])
34
+ build_test_response
35
+ elsif info_path?(env["PATH_INFO"])
36
+ build_info_response
42
37
  else
43
38
  @app.call(env)
44
39
  end
45
40
  end
46
41
 
47
- # Returns text/html when the `test` url is requested.
48
- # This is purely to see whether the URL works through the HireFire command-line utility.
49
- #
50
- # Returns a JSON String when the `info` url is requested.
51
- # This url will be requested every minute by HireFire in order to fetch dyno data.
42
+ private
43
+
44
+ # Determines whether or not the test path has been requested.
52
45
  #
53
- # @return [text/html, application/json] based on whether the `test` or `info` url was requested.
46
+ # @param [String] path_info the requested path.
47
+ # @return [Boolean] true if the requested path matches the test path.
54
48
  #
55
- def each(&block)
56
- if test?
57
- block.call "HireFire Middleware Found!"
58
- elsif info?
59
- block.call(dynos)
60
- end
49
+ def test_path?(path_info)
50
+ get_path(path_info) == "/hirefire/test"
61
51
  end
62
52
 
63
- private
64
-
65
- # Generates a JSON string based on the dyno data.
53
+ # Determines whether or not the info path has been requested.
66
54
  #
67
- # @return [String] in JSON format.
55
+ # @param [String] path_info the requested path.
56
+ # @return [Boolean] true if the requested path matches the info path.
68
57
  #
69
- def dynos
70
- dyno_data = HireFire::Resource.dynos.inject(String.new) do |json, dyno|
71
- json << %(,{"name":"#{dyno[:name]}","quantity":#{dyno[:quantity].call || "null"}})
72
- json
73
- end
74
-
75
- "[#{dyno_data.sub(",","")}]"
58
+ def info_path?(path_info)
59
+ get_path(path_info) == "/hirefire/#{@token || "development"}/info"
76
60
  end
77
61
 
78
- # Rack PATH_INFO with any RAILS_RELATIVE_URL_ROOT stripped off
62
+ # The provided path with @path_prefix stripped off.
79
63
  #
80
- # @return [String]
64
+ # @param [String] path_info the requested path.
65
+ # @return [String] the path without the @path_prefix.
81
66
  #
82
- def path
67
+ def get_path(path_info)
83
68
  if @path_prefix
84
- @env["PATH_INFO"].gsub(@path_prefix, "")
69
+ path_info.gsub(@path_prefix, "")
85
70
  else
86
- @env["PATH_INFO"]
71
+ path_info
87
72
  end
88
73
  end
89
74
 
90
- # Returns true if the PATH_INFO matches the test url.
75
+ # Builds the response for the test path.
91
76
  #
92
- # @return [Boolean] true if the requested url matches the test url.
77
+ # @return [String] in text/html format.
93
78
  #
94
- def test?
95
- path == "/hirefire/test"
79
+ def build_test_response
80
+ status = 200
81
+ headers = {"Content-Type" => "text/html"}
82
+ body = "HireFire Middleware Found!"
83
+
84
+ [status, headers, [body]]
96
85
  end
97
86
 
98
- # Returns true if the PATH_INFO matches the info url.
87
+ # Builds the response for the info path containing the configured
88
+ # queues and their sizes based on the HireFire::Resource configuration.
99
89
  #
100
- # @return [Boolean] true if the requested url matches the info url.
90
+ # @return [String] in application/json format.
101
91
  #
102
- def info?
103
- path == "/hirefire/#{@token || "development"}/info"
92
+ def build_info_response
93
+ entry_builder = lambda do |config|
94
+ %({"name":"#{config[:name]}","quantity":#{config[:quantity].call || "null"}})
95
+ end
96
+
97
+ entries = HireFire::Resource.dynos.inject([]) do |buffer, config|
98
+ buffer << entry_builder.call(config)
99
+ end
100
+
101
+ status = 200
102
+ headers = Hash.new
103
+ headers["Content-Type"] = "application/json"
104
+ headers["Cache-Control"] = "must-revalidate, private, max-age=0"
105
+ body = "[" + entries.join(",") + "]"
106
+
107
+ [status, headers, [body]]
104
108
  end
105
109
 
106
110
  # Writes the Heroku Router queue time to STDOUT if a String was provided.
@@ -129,5 +133,14 @@ module HireFire
129
133
  ms = (Time.now.to_f * 1000).to_i - value.to_i
130
134
  ms < 0 ? 0 : ms
131
135
  end
136
+
137
+ # Configures the @path_prefix in order to handle apps
138
+ # mounted under RAILS_RELATIVE_URL_ROOT.
139
+ #
140
+ def get_path_prefix
141
+ if defined?(Rails) && Rails.application.config.relative_url_root
142
+ Regexp.new("^" + Regexp.escape(Rails.application.config.relative_url_root))
143
+ end
144
+ end
132
145
  end
133
146
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hirefire-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael van Rooijen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-06 00:00:00.000000000 Z
11
+ date: 2018-11-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Load- and schedule-based scaling for web- and worker dynos
14
14
  email: michael@hirefire.io