slowpoke 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e81989e90c4ba519f36f46021d180d1f2e5df40
4
+ data.tar.gz: ba0a6e166d707381fe6ebbaa8ddf8e8253f96eb9
5
+ SHA512:
6
+ metadata.gz: 22e578b270ebbe71cb328b219d540463b34f21c6e4eb38095d0a56c2f7f18bd0e901d48e1a5d142eb6abd40f4468cda90f542a97117495588410127b06612d17
7
+ data.tar.gz: aea0511a75fbadb6e892e34f3a9cb1fee7f9c74999082f62106aaa9a6586375cf2629880bac0c4ec570c2e228d1d004bf1214f2542ff0248c1f1fdfbdaf58792
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slowpoke.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Andrew Kane
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,58 @@
1
+ # Slowpoke
2
+
3
+ Timeouts made easy
4
+
5
+ - custom error page
6
+ - database timeouts
7
+ - notifications
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application’s Gemfile:
12
+
13
+ ```ruby
14
+ gem 'slowpoke'
15
+ ```
16
+
17
+ And run:
18
+
19
+ ```sh
20
+ rails generate slowpoke:install
21
+ ```
22
+
23
+ This creates a `public/503.html` you can customize.
24
+
25
+ The default timeout is 15 seconds. Change this with:
26
+
27
+ ```ruby
28
+ Slowpoke.timeout = 10 # seconds
29
+ ```
30
+
31
+ or use:
32
+
33
+ ```ruby
34
+ ENV["TIMEOUT"] = 10
35
+ ```
36
+
37
+ For ActiveRecord (PostgreSQL only), change the database timeout with:
38
+
39
+ ```ruby
40
+ Slowpoke.database_timeout = 5
41
+ ```
42
+
43
+ Subscribe to timeouts
44
+
45
+ ```ruby
46
+ ActiveSupport::Notifications.subscribe "timeout.slowpoke" do |name, start, finish, id, payload|
47
+ # report timeout
48
+ end
49
+ ```
50
+
51
+ ## Contributing
52
+
53
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
54
+
55
+ - [Report bugs](https://github.com/ankane/slowpoke/issues)
56
+ - Fix bugs and [submit pull requests](https://github.com/ankane/slowpoke/pulls)
57
+ - Write, clarify, or fix documentation
58
+ - Suggest or add new features
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ require "rails/generators"
2
+
3
+ module Slowpoke
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def copy_503_html
9
+ template "503.html", "public/503.html"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>This page took too long to load (503)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/503.html -->
52
+ <div class="dialog">
53
+ <h1>This page took too long to load.</h1>
54
+ </div>
55
+ <p>Give it another shot.</p>
56
+ </body>
57
+ </html>
@@ -0,0 +1,9 @@
1
+ module Slowpoke
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "slowpoke" do
5
+ Slowpoke.timeout = (ENV["TIMEOUT"] || 15).to_i
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Slowpoke
2
+ VERSION = "0.0.1"
3
+ end
data/lib/slowpoke.rb ADDED
@@ -0,0 +1,66 @@
1
+ require "slowpoke/version"
2
+ require "rack-timeout"
3
+ require "robustly"
4
+ require "slowpoke/railtie"
5
+ require "action_view/template"
6
+ require "action_controller/base"
7
+ require "active_record/connection_adapters/postgresql_adapter"
8
+ require "action_dispatch/middleware/exception_wrapper"
9
+
10
+ module Slowpoke
11
+ class << self
12
+ attr_reader :timeout
13
+ attr_accessor :database_timeout
14
+ end
15
+
16
+ def self.timeout=(timeout)
17
+ @timeout = timeout
18
+ Rack::Timeout.timeout = timeout
19
+ end
20
+ end
21
+
22
+ Rack::Timeout.unregister_state_change_observer(:logger)
23
+
24
+ ActionDispatch::ExceptionWrapper.rescue_responses["Rack::Timeout::RequestTimeoutError"] = :service_unavailable
25
+
26
+ # hack to bubble timeout errors
27
+ class ActionView::Template
28
+
29
+ def handle_render_error_with_timeout(view, e)
30
+ raise e if e.is_a?(Rack::Timeout::Error)
31
+ handle_render_error_without_timeout(view, e)
32
+ end
33
+ alias_method_chain :handle_render_error, :timeout
34
+
35
+ end
36
+
37
+ class ActionController::Base
38
+
39
+ def rescue_from_timeout(exception)
40
+ ActiveSupport::Notifications.instrument("timeout.slowpoke", {})
41
+ raise exception
42
+ end
43
+ rescue_from Rack::Timeout::Error, with: :rescue_from_timeout
44
+
45
+ end
46
+
47
+ # timeout queries after a minute
48
+ class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
49
+
50
+ def configure_connection_with_statement_timeout
51
+ configure_connection_without_statement_timeout
52
+ safely do
53
+ timeout = Slowpoke.database_timeout || Slowpoke.timeout
54
+ if ActiveRecord::Base.logger
55
+ ActiveRecord::Base.logger.class.send(:include, ::LoggerSilence)
56
+ ActiveRecord::Base.logger.silence do
57
+ execute("SET statement_timeout = #{timeout * 1000}")
58
+ end
59
+ else
60
+ execute("SET statement_timeout = #{timeout * 1000}")
61
+ end
62
+ end
63
+ end
64
+ alias_method_chain :configure_connection, :statement_timeout
65
+
66
+ end
data/slowpoke.gemspec ADDED
@@ -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 'slowpoke/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slowpoke"
8
+ spec.version = Slowpoke::VERSION
9
+ spec.authors = ["Andrew Kane"]
10
+ spec.email = ["andrew@chartkick.com"]
11
+ spec.summary = %q{Timeouts made easy}
12
+ spec.description = %q{Timeouts made easy}
13
+ spec.homepage = "https://github.com/ankane/slowpoke"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.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_dependency "rack-timeout", "= 0.1.0beta3"
22
+ spec.add_dependency "robustly"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slowpoke
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack-timeout
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0beta3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0beta3
27
+ - !ruby/object:Gem::Dependency
28
+ name: robustly
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Timeouts made easy
70
+ email:
71
+ - andrew@chartkick.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/generators/slowpoke/install_generator.rb
82
+ - lib/generators/slowpoke/templates/503.html
83
+ - lib/slowpoke.rb
84
+ - lib/slowpoke/railtie.rb
85
+ - lib/slowpoke/version.rb
86
+ - slowpoke.gemspec
87
+ homepage: https://github.com/ankane/slowpoke
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Timeouts made easy
111
+ test_files: []