hirefire-resource 0.7.0 → 0.7.1
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 +4 -4
- data/hirefire-resource.gemspec +1 -1
- data/lib/hirefire/middleware.rb +74 -61
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0bf414cb7e6529a04049ad3e7a7575a1932ec91fc8be57d8a2ce1ccc0922ab49
|
|
4
|
+
data.tar.gz: 8c78e71660e89df170ca061a328bfaff3d19714d55bf3a8fac04a59fbd9f2569
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2f84e86124b9467d166a839b3c90fd979ac13ccb4f50005dfa0e61e77763191206714a569fe59ea991da41b287d3a165a4740ea49f603ecc65caebc2d924c654
|
|
7
|
+
data.tar.gz: ce69dbda35b2762f727caa5e0d9fb5c76b3890ffa33cec9c20e46cc021d5b54f5842f337fc7264b00453dcf7074c3befd0db1970f89964ffb3703944d1837bb5
|
data/hirefire-resource.gemspec
CHANGED
data/lib/hirefire/middleware.rb
CHANGED
|
@@ -3,104 +3,108 @@
|
|
|
3
3
|
module HireFire
|
|
4
4
|
class Middleware
|
|
5
5
|
|
|
6
|
-
|
|
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 [
|
|
8
|
+
# @param [Proc] app call with `env` to continue down the middleware stack.
|
|
19
9
|
#
|
|
20
10
|
def initialize(app)
|
|
21
|
-
@app
|
|
22
|
-
@token
|
|
23
|
-
|
|
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
|
-
#
|
|
29
|
-
#
|
|
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
|
|
39
|
-
|
|
40
|
-
elsif
|
|
41
|
-
|
|
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
|
-
|
|
48
|
-
|
|
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
|
-
# @
|
|
46
|
+
# @param [String] path_info the requested path.
|
|
47
|
+
# @return [Boolean] true if the requested path matches the test path.
|
|
54
48
|
#
|
|
55
|
-
def
|
|
56
|
-
|
|
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
|
-
|
|
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
|
-
# @
|
|
55
|
+
# @param [String] path_info the requested path.
|
|
56
|
+
# @return [Boolean] true if the requested path matches the info path.
|
|
68
57
|
#
|
|
69
|
-
def
|
|
70
|
-
|
|
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
|
-
#
|
|
62
|
+
# The provided path with @path_prefix stripped off.
|
|
79
63
|
#
|
|
80
|
-
# @
|
|
64
|
+
# @param [String] path_info the requested path.
|
|
65
|
+
# @return [String] the path without the @path_prefix.
|
|
81
66
|
#
|
|
82
|
-
def
|
|
67
|
+
def get_path(path_info)
|
|
83
68
|
if @path_prefix
|
|
84
|
-
|
|
69
|
+
path_info.gsub(@path_prefix, "")
|
|
85
70
|
else
|
|
86
|
-
|
|
71
|
+
path_info
|
|
87
72
|
end
|
|
88
73
|
end
|
|
89
74
|
|
|
90
|
-
#
|
|
75
|
+
# Builds the response for the test path.
|
|
91
76
|
#
|
|
92
|
-
# @return [
|
|
77
|
+
# @return [String] in text/html format.
|
|
93
78
|
#
|
|
94
|
-
def
|
|
95
|
-
|
|
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
|
-
#
|
|
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 [
|
|
90
|
+
# @return [String] in application/json format.
|
|
101
91
|
#
|
|
102
|
-
def
|
|
103
|
-
|
|
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.
|
|
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-
|
|
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
|