warm-blanket 1.0.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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/COPYING +674 -0
- data/COPYING.LESSER +165 -0
- data/Gemfile +4 -0
- data/README.md +139 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/pry +17 -0
- data/bin/rspec +17 -0
- data/lib/warm-blanket.rb +53 -0
- data/lib/warm_blanket/orchestrator.rb +136 -0
- data/lib/warm_blanket/requester.rb +98 -0
- data/lib/warm_blanket/version.rb +23 -0
- data/lib/warm_blanket/wait_for_port.rb +67 -0
- data/warm-blanket.gemspec +32 -0
- metadata +201 -0
data/COPYING.LESSER
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# WarmBlanket
|
2
|
+
|
3
|
+
WarmBlanket is a Ruby gem for warming up web services on boot. Its main target are JRuby web services, although it is not JRuby-specific in any way.
|
4
|
+
|
5
|
+
* [WarmBlanket](#warmblanket)
|
6
|
+
* [How the magic happens](#how-the-magic-happens)
|
7
|
+
* [Why do we need to warm up web services?](#why-do-we-need-to-warm-up-web-services)
|
8
|
+
* [What does WarmBlanket do?](#what-does-warmblanket-do)
|
9
|
+
* [How does WarmBlanket work?](#how-does-warmblanket-work)
|
10
|
+
* [Limitations/caveats](#limitationscaveats)
|
11
|
+
* [How can I make use of it?](#how-can-i-make-use-of-it)
|
12
|
+
* [1. Installation](#1-installation)
|
13
|
+
* [2. Configuration settings](#2-configuration-settings)
|
14
|
+
* [Configuring endpoints to be called](#configuring-endpoints-to-be-called)
|
15
|
+
* [3. Trigger warmup](#3-trigger-warmup)
|
16
|
+
* [Development](#development)
|
17
|
+
* [Contributing](#contributing)
|
18
|
+
|
19
|
+
<sub><sup>ToC created with [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)</sup></sub>
|
20
|
+
|
21
|
+
# How the magic happens
|
22
|
+
|
23
|
+
## Why do we need to warm up web services?
|
24
|
+
|
25
|
+
When the Java Virtual Machine (JVM) starts, it starts by interpreting Java bytecode. As it starts to detect code that runs often, it just-in-time compiles that code into native machine code, improving performance.
|
26
|
+
|
27
|
+
This is a known challenge for most JVMs, and the same applies to JRuby applications, which also run on the JVM.
|
28
|
+
|
29
|
+
A widely-documented solution to this problem is to perform a warm-up step when starting a service:
|
30
|
+
|
31
|
+
* <https://landing.google.com/sre/book/chapters/load-balancing-datacenter.html#unpredictable-performance-factors-JMs7i7trCj>
|
32
|
+
* <http://www.brendangregg.com/blog/2016-09-28/java-warmup.html>
|
33
|
+
* <https://devcenter.heroku.com/articles/warming-up-a-java-process>
|
34
|
+
|
35
|
+
## What does WarmBlanket do?
|
36
|
+
|
37
|
+
WarmBlanket warms services by performing repeated web requests for a configurable number of seconds. After that time, it closes shop and you'll never hear about it until the next service restart or deploy.
|
38
|
+
|
39
|
+
## How does WarmBlanket work?
|
40
|
+
|
41
|
+
WarmBlanket spawns a configurable number of background threads that run inside the service process, and then uses an http client to perform local requests to the web server, simulating load.
|
42
|
+
|
43
|
+
As it simulates requests, the JVM is warmed up and thus when real requests come in, no performance degradation is observed.
|
44
|
+
|
45
|
+
## Limitations/caveats
|
46
|
+
|
47
|
+
We strongly recommend that any services using WarmBlanket, if deployed on Heroku, use [Preboot](https://devcenter.heroku.com/articles/preboot). Preboot allows a service instance to be warmed up for 3 minutes before Heroku starts sending live traffic its way, which is preferable to doing it live.
|
48
|
+
|
49
|
+
# How can I make use of it?
|
50
|
+
|
51
|
+
To make use of WarmBlanket, you'll need to follow the next sections, which will guide you through installing, configuring and enabling the gem.
|
52
|
+
|
53
|
+
## 1. Installation
|
54
|
+
|
55
|
+
To install using Bundler, add the following to your `Gemfile`:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
gem 'warm-blanket', '~> 1.0'
|
59
|
+
```
|
60
|
+
|
61
|
+
WarmBlanket uses [semantic versioning](http://semver.org/).
|
62
|
+
|
63
|
+
## 2. Configuration settings
|
64
|
+
|
65
|
+
This gem can be configured via the following environment variables:
|
66
|
+
|
67
|
+
* `PORT`: Local webserver port (automatically set on Heroku)
|
68
|
+
* `WARMBLANKET_ENABLED`: Enable warm blanket (defaults to `false`; `true` or `1` enables)
|
69
|
+
* `WARMBLANKET_WARMUP_THREADS`: Number of warm up threads to use (defaults to `2`)
|
70
|
+
* `WARMBLANKET_WARMUP_TIME_SECONDS`: Time, in seconds, during which to warm up the service (defaults to `150`)
|
71
|
+
|
72
|
+
### Configuring endpoints to be called
|
73
|
+
|
74
|
+
Configure endpoints to be called as follows (on a `config/warm_blanket.rb`:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
require 'warm-blanket'
|
78
|
+
|
79
|
+
WarmBlanket.configure do |config|
|
80
|
+
common_headers = {
|
81
|
+
'X-Api-Key': ENV.fetch('API_KEYS').split(',').first,
|
82
|
+
}
|
83
|
+
|
84
|
+
config.endpoints = [
|
85
|
+
{get: '/foo', headers: common_headers},
|
86
|
+
{get: '/', headers: common_headers},
|
87
|
+
]
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
Other HTTP verbs are supported (and you can pass in a `body` key if needed), but be careful about side effects from such verbs. And if there's no side effect from a `POST` or `PUT`, do consider if it shouldn't be a `GET` instead ;)
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
# Example POST request with body
|
95
|
+
#
|
96
|
+
# Notice that you need to both:
|
97
|
+
# * set the Content-Type manually (if needed)
|
98
|
+
# * JSON-encode the body (if needed)
|
99
|
+
|
100
|
+
WarmBlanket.configure do |config|
|
101
|
+
common_headers = {
|
102
|
+
'X-Api-Key': ENV.fetch('API_KEY').split(',').first,
|
103
|
+
'Content-Type': 'application/json',
|
104
|
+
}
|
105
|
+
|
106
|
+
post_body = MultiJson.dump(
|
107
|
+
account_id: 'dummy_account',
|
108
|
+
user_id: 'dummy_user_id',
|
109
|
+
)
|
110
|
+
|
111
|
+
config.endpoints = [
|
112
|
+
{post: '/some_endoint', headers: common_headers, body: post_body},
|
113
|
+
]
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
## 3. Trigger warmup
|
118
|
+
|
119
|
+
Add the following to the end of your `config.ru` file:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
WarmBlanket.trigger_warmup
|
123
|
+
```
|
124
|
+
|
125
|
+
# Development
|
126
|
+
|
127
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
128
|
+
|
129
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
130
|
+
|
131
|
+
# Contributors
|
132
|
+
|
133
|
+
Open-sourced with :heart: by Talkdesk!
|
134
|
+
|
135
|
+
Maintained by [Ivo Anjo](https://github.com/ivoanjo/) and the [Talkdesk Engineering](http://github.com/Talkdesk/) team.
|
136
|
+
|
137
|
+
# Contributing
|
138
|
+
|
139
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/Talkdesk/warm-blanket>.
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/pry
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'pry' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'pathname'
|
11
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'bundler/setup'
|
16
|
+
|
17
|
+
load Gem.bin_path('pry', 'pry')
|
data/bin/rspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rspec' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'pathname'
|
11
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'bundler/setup'
|
16
|
+
|
17
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
data/lib/warm-blanket.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# WarmBlanket: Ruby gem for warming up web services on boot
|
2
|
+
# Copyright (C) 2017 Talkdesk, Inc. <tech@talkdesk.com>
|
3
|
+
#
|
4
|
+
# This file is part of WarmBlanket.
|
5
|
+
#
|
6
|
+
# WarmBlanket is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
8
|
+
# the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# WarmBlanket is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public License
|
17
|
+
# along with WarmBlanket. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
|
19
|
+
require 'warm_blanket/version'
|
20
|
+
require 'warm_blanket/orchestrator'
|
21
|
+
|
22
|
+
require 'dry-configurable'
|
23
|
+
require 'logging'
|
24
|
+
|
25
|
+
module WarmBlanket
|
26
|
+
extend Dry::Configurable
|
27
|
+
|
28
|
+
# Endpoints to be called for warmup, see README
|
29
|
+
setting :endpoints, [], reader: true
|
30
|
+
|
31
|
+
setting :logger, Logging.logger[self], reader: true
|
32
|
+
|
33
|
+
# Local webserver port
|
34
|
+
setting :port, ENV['PORT'], reader: true
|
35
|
+
|
36
|
+
# Enable warmup
|
37
|
+
setting :enabled, ENV['WARMBLANKET_ENABLED'], reader: true
|
38
|
+
|
39
|
+
# Number of threads to use
|
40
|
+
setting :warmup_threads, Integer(ENV['WARMBLANKET_WARMUP_THREADS'] || 2), reader: true
|
41
|
+
|
42
|
+
# Time, in seconds, during which to warm up the service
|
43
|
+
setting :warmup_time_seconds, Float(ENV['WARMBLANKET_WARMUP_TIME_SECONDS'] || 150), reader: true
|
44
|
+
|
45
|
+
def self.trigger_warmup(logger: WarmBlanket.config.logger, orchestrator_factory: Orchestrator)
|
46
|
+
unless [true, 'true', '1'].include?(WarmBlanket.config.enabled)
|
47
|
+
logger.info "WarmBlanket not enabled, ignoring trigger_warmup"
|
48
|
+
return false
|
49
|
+
end
|
50
|
+
|
51
|
+
orchestrator_factory.new.call
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# WarmBlanket: Ruby gem for warming up web services on boot
|
2
|
+
# Copyright (C) 2017 Talkdesk, Inc. <tech@talkdesk.com>
|
3
|
+
#
|
4
|
+
# This file is part of WarmBlanket.
|
5
|
+
#
|
6
|
+
# WarmBlanket is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
8
|
+
# the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# WarmBlanket is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public License
|
17
|
+
# along with WarmBlanket. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
|
19
|
+
# frozen_string_literal: true
|
20
|
+
|
21
|
+
require 'warm_blanket/requester'
|
22
|
+
require 'warm_blanket/wait_for_port'
|
23
|
+
|
24
|
+
module WarmBlanket
|
25
|
+
# Orchestrates threads to wait for the port to open and to perform the warmup requests
|
26
|
+
class Orchestrator
|
27
|
+
|
28
|
+
DEFAULT_HEADERS = {
|
29
|
+
'X-Forwarded-Proto': 'https',
|
30
|
+
'X-Request-Id': 'WarmBlanket',
|
31
|
+
'X-Client-Id': 'WarmBlanket',
|
32
|
+
}.freeze
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :requester_factory
|
37
|
+
attr_reader :wait_for_port_factory
|
38
|
+
attr_reader :logger
|
39
|
+
attr_reader :endpoints
|
40
|
+
attr_reader :hostname
|
41
|
+
attr_reader :port
|
42
|
+
attr_reader :warmup_threads
|
43
|
+
attr_reader :warmup_deadline
|
44
|
+
|
45
|
+
public
|
46
|
+
|
47
|
+
def initialize(
|
48
|
+
requester_factory: Requester,
|
49
|
+
wait_for_port_factory: WaitForPort,
|
50
|
+
logger: WarmBlanket.config.logger,
|
51
|
+
endpoints: WarmBlanket.config.endpoints,
|
52
|
+
hostname: 'localhost',
|
53
|
+
port: WarmBlanket.config.port,
|
54
|
+
warmup_threads: WarmBlanket.config.warmup_threads,
|
55
|
+
warmup_time_seconds: WarmBlanket.config.warmup_time_seconds
|
56
|
+
)
|
57
|
+
raise "Warmup threads cannot be less than 1 (got #{warmup_threads})" if warmup_threads < 1
|
58
|
+
|
59
|
+
@requester_factory = requester_factory
|
60
|
+
@wait_for_port_factory = wait_for_port_factory
|
61
|
+
@logger = logger
|
62
|
+
@endpoints = endpoints
|
63
|
+
@hostname = hostname
|
64
|
+
@port = port
|
65
|
+
@warmup_threads = warmup_threads
|
66
|
+
@warmup_deadline = Time.now + warmup_time_seconds
|
67
|
+
end
|
68
|
+
|
69
|
+
def call
|
70
|
+
safely_spawn_thread do
|
71
|
+
logger.debug 'Started orchestrator thread'
|
72
|
+
orchestrate
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def safely_spawn_thread(&block)
|
79
|
+
Thread.new do
|
80
|
+
begin
|
81
|
+
block.call
|
82
|
+
rescue => e
|
83
|
+
logger.error "Caught error that caused background thread to die #{e.class}: #{e.message}"
|
84
|
+
logger.debug "#{e.backtrace.join("\n")}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def orchestrate
|
90
|
+
success = wait_for_port_to_open
|
91
|
+
|
92
|
+
spawn_warmup_threads if success
|
93
|
+
end
|
94
|
+
|
95
|
+
def wait_for_port_to_open
|
96
|
+
wait_for_port_factory.new(port: port, time_deadline: warmup_deadline).call
|
97
|
+
end
|
98
|
+
|
99
|
+
def spawn_warmup_threads
|
100
|
+
# Create remaining threads
|
101
|
+
(warmup_threads - 1).times do
|
102
|
+
safely_spawn_thread do
|
103
|
+
perform_warmup_requests
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Reuse current thread
|
108
|
+
perform_warmup_requests
|
109
|
+
end
|
110
|
+
|
111
|
+
def perform_warmup_requests
|
112
|
+
success = false
|
113
|
+
|
114
|
+
if Time.now >= warmup_deadline
|
115
|
+
logger.warn "Warmup deadline already passed, will skip warmup"
|
116
|
+
return
|
117
|
+
end
|
118
|
+
|
119
|
+
logger.debug "Starting warmup requests (remaining deadline: #{[warmup_deadline - Time.now, 0].max})"
|
120
|
+
|
121
|
+
requester = requester_factory.new(
|
122
|
+
base_url: "http://#{hostname}:#{port}",
|
123
|
+
default_headers: DEFAULT_HEADERS,
|
124
|
+
endpoints: endpoints,
|
125
|
+
)
|
126
|
+
|
127
|
+
while Time.now < warmup_deadline
|
128
|
+
requester.call
|
129
|
+
end
|
130
|
+
|
131
|
+
success = true
|
132
|
+
ensure
|
133
|
+
logger.info "Finished warmup work #{success ? 'successfully' : 'with error'}"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|