activerecord-pgtimeout 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c8aa74a8beb7f744fbd2f5d64a7ff91d690beae0
4
+ data.tar.gz: 498a0a1f244c35fc1adb30d4a1d4239a782de6f1
5
+ SHA512:
6
+ metadata.gz: f50d3ec09d7ca11a2f5603e613010ba4dddbeab55e9a5051cb0979c7c962614dab785a8e7de465dcabd3bb488267632664b431bde88bf742bd8a6a8e5d7d3456
7
+ data.tar.gz: 64ffcfa9c824b57e87195c86cb3f1f0cd1f6a00514c99c3b3b7750717551bc92256950c314f2ea3baa8a10bc136b83309b1cf08b93b24d5162672d8f25026edf
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-pgtimeout.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # ActiveRecord::PgTimeout
2
+
3
+ This provides a way to wrap one action in a database transaction that will timeout in a pre-configured time. When the operation times out the transaction is cancelled and a custom handler is executed.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord-pgtimeout'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-pgtimeout
20
+
21
+ ## Usage
22
+
23
+ Given that there is an ActiveRecord model called `User`, and I want to fetch all users under 10 seconds:
24
+
25
+ ```ruby
26
+ timeoutable_executor = new ActiveRecord::PgTimeout.new(10_000)
27
+ users = timeoutable_executor.execute(->{ User.all }, ->{ puts "Timed out user loading"; [] })
28
+ ```
29
+
30
+ The threshold is defined in milliseconds, if the users cannot be fecthed in the configured time the timeout handler lambda will be evaluated and returned.
31
+ In the example above whenever the timeout occurs we display a message in the console and return an empty collection.
32
+
33
+
34
+ In most use cases we want to handle the timeout case, but whenever we have a global timeout defined, it might be useful to disable the timeout inside our block and ignore the timeout handler. Consider a database with a default timeout of 60 seconds, and we want to execute a long running migration that need to run for as long as it is needed.
35
+ We could write such migration like this:
36
+
37
+ ```ruby
38
+ timeoutable_executor = new ActiveRecord::PgTimeout.new(0)
39
+ timeoutable_executor.execute(->{ execute "CREATE TABLE long_running AS SELECT * FROM pg_sleep(90);" })
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` 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.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome. Please always provide concrete use cases with any feature request or bug report.
51
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord/pgtimeout/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-pgtimeout"
8
+ spec.version = ActiveRecord::PgTimeout::VERSION
9
+ spec.authors = ["Diogo Biazus", "Marios Koulakis"]
10
+ spec.email = ["diogob@nulogy.com", "mariosk@nulogy.com"]
11
+
12
+ spec.summary = %q{Allows the use of custom timeout settings within the context of a PostgreSQL transaction.}
13
+ spec.description = %q{This gem provides a very simple interface where one can wrap a lambda within a timeoutable transaction and pass a custom handler along it}
14
+ spec.homepage = "https://gitlab.hq.nulogy.com/diogob/activerecord-pg-timeout"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activerecord", ">= 4.0"
24
+ spec.add_dependency "pg"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/pgtimeout"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ class PgTimeout
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ require "active_record"
2
+
3
+ module ActiveRecord
4
+ class PgTimeout
5
+ attr_reader :timeout
6
+
7
+ def initialize(timeout, connection = ActiveRecord::Base.connection)
8
+ @connection = connection
9
+ @timeout = timeout
10
+ end
11
+
12
+ def execute(timeoutable_action, on_timeout)
13
+ @connection.transaction do
14
+ @connection.execute("set local statement_timeout to '#{timeout.to_i}ms'")
15
+ timeoutable_action.()
16
+ end
17
+
18
+ rescue ActiveRecord::StatementInvalid => e
19
+ if e.original_exception.result.error_field(PG::Result::PG_DIAG_SQLSTATE) == "57014"
20
+ on_timeout.()
21
+ else
22
+ raise e
23
+ end
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-pgtimeout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Diogo Biazus
8
+ - Marios Koulakis
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-01-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '4.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '4.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: pg
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.13'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.13'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ description: This gem provides a very simple interface where one can wrap a lambda
85
+ within a timeoutable transaction and pass a custom handler along it
86
+ email:
87
+ - diogob@nulogy.com
88
+ - mariosk@nulogy.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - ".rspec"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - README.md
98
+ - Rakefile
99
+ - activerecord-pgtimeout.gemspec
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/activerecord/pgtimeout.rb
103
+ - lib/activerecord/pgtimeout/version.rb
104
+ homepage: https://gitlab.hq.nulogy.com/diogob/activerecord-pg-timeout
105
+ licenses: []
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Allows the use of custom timeout settings within the context of a PostgreSQL
127
+ transaction.
128
+ test_files: []