unicorn-prewarm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df1ceb61b86e6ae9c1c76928d93b01ca418716b2
4
+ data.tar.gz: b56c934f7c3021cb5949efcc891c22446169afb4
5
+ SHA512:
6
+ metadata.gz: 1f3447ad96d5fdd9f0253f1d6061c85f912939d0a6059d4e5d1df8a4dbf72d227a25cf4ed2960b944a9a3ad171a0f5b0255e8f26e7108719a9addb48cb28fc57
7
+ data.tar.gz: 667a4f3887a406d8e1016e6fee2e4c282ded357467ae896f86a5d6844b6289d5a9cf4255be9c80d38e4e42da16df0ea7841094d5c1d0a556c1b9c125708434db
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 2.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## 0.1.0 - 2014-11-18
5
+
6
+ ### Added
7
+ - Unicorn Worker prewarming
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unicorn-prewarm.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Michal Cichra, 3scale Networks S. L., 3scale Inc.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Unicorn::Prewarm [![Build Status](https://travis-ci.org/3scale/unicorn-prewarm.svg?branch=master)](https://travis-ci.org/3scale/unicorn-prewarm) [![Code Climate](https://codeclimate.com/github/3scale/unicorn-prewarm/badges/gpa.svg)]
2
+
3
+ Prewarm your workers before they receive real traffic.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'unicorn-prewarm'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install unicorn-prewarm
20
+
21
+ ## Usage
22
+
23
+ In unicorn config `after_fork` do:
24
+
25
+ ```ruby
26
+ after_fork do |server,worker|
27
+ response = Unicorn.prewarm(server)
28
+ # check if the response is OK, it shall be Net::HTTPSuccess
29
+ end
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/3scale/unicorn-prewarm/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ # nothing
8
+ end
9
+
10
+ task default: :spec
11
+
@@ -0,0 +1 @@
1
+ require 'unicorn/prewarm'
@@ -0,0 +1,63 @@
1
+ require 'unicorn'
2
+ require 'unicorn/prewarm/version'
3
+
4
+ module Unicorn
5
+ def self.prewarm(server)
6
+ get = Net::HTTP::Get.new('/')
7
+ fake = Unicorn::Prewarm::FakeSocket.new(get)
8
+ server.send(:process_client, fake)
9
+ fake.response
10
+ end
11
+
12
+ module Prewarm
13
+ class NetStringIO < StringIO
14
+ def readline
15
+ super.chomp
16
+ end
17
+
18
+ def readuntil(idx, ignore_eof)
19
+ buff = ''
20
+ loop do
21
+ char = read(1)
22
+ return buff unless char
23
+ buff << char
24
+ return buff if char == idx
25
+ end
26
+ end
27
+ end
28
+
29
+ class FakeSocket
30
+ HTTP_VERSION = '1.1'.freeze
31
+
32
+ def initialize(req, version = HTTP_VERSION, path = req.path)
33
+ @req = req
34
+ @version = version
35
+ @path = path
36
+ @out = StringIO.new
37
+ end
38
+
39
+ def kgio_read!(_size, buff = '')
40
+ sock = StringIO.new(buff)
41
+ @req.send(:write_header, sock, @version, @path)
42
+ buff
43
+ end
44
+
45
+ def kgio_addr
46
+ '127.0.0.1'
47
+ end
48
+
49
+ def write(line)
50
+ @out << line
51
+ end
52
+
53
+ def response
54
+ res = NetStringIO.new(@out.string)
55
+ Net::HTTPResponse.read_new(res)
56
+ end
57
+
58
+ def closed?
59
+ true
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module Unicorn
2
+ module Prewarm
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'unicorn/prewarm'
2
+
3
+ RSpec.describe 'unicorn' do
4
+ let(:server) { Unicorn::HttpServer.new(app) }
5
+ let(:app){ Proc.new{ [200, [''], []] } }
6
+
7
+ it 'calls the app' do
8
+ expect(Unicorn.prewarm(server)).to be_a(Net::HTTPSuccess)
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unicorn/prewarm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'unicorn-prewarm'
8
+ spec.version = Unicorn::Prewarm::VERSION
9
+ spec.authors = ['Michal Cichra']
10
+ spec.email = ['michal@3scale.net']
11
+ spec.summary = %q{Make requests to unicorn workers before they receive real traffic.}
12
+ spec.description = %q{To prewarm workers, warm caches and load everything before real requests.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z 2> /dev/null`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.1'
24
+
25
+ spec.add_dependency 'unicorn', '~> 4.8.0'
26
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicorn-prewarm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michal Cichra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: unicorn
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.8.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.8.0
69
+ description: To prewarm workers, warm caches and load everything before real requests.
70
+ email:
71
+ - michal@3scale.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/unicorn-prewarm.rb
84
+ - lib/unicorn/prewarm.rb
85
+ - lib/unicorn/prewarm/version.rb
86
+ - spec/prewarm_spec.rb
87
+ - unicorn-prewarm.gemspec
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Make requests to unicorn workers before they receive real traffic.
112
+ test_files:
113
+ - spec/prewarm_spec.rb