travis-lock 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c983c384c38f7bb07cddd92b3ea775570ff2aec
4
- data.tar.gz: 21b952b4ce5c7eeed44d21d4db04e6b4e920f428
3
+ metadata.gz: 438cbc91b63a790889bc58d8a5631f59ff394226
4
+ data.tar.gz: 9df4223ebb7224febae75e1248d075c3387a0b4e
5
5
  SHA512:
6
- metadata.gz: 490a9cb44c09b52426876d131d30c118e3e279a82dd75d7c9cc30d0d6553d5278ba4e9b8d686d34683a18e370727e09a6e2e1ea124365f359a4aaf23a2c2f3c0
7
- data.tar.gz: afccd06931aaa156e0e6fbafa590d3929e0a0fc903f3386554499d15277e862f6ab6d1ee09822ae9cf0042e91c134e1a07959def5a58f3522b8439aaa48d590d
6
+ metadata.gz: f671cffce832b81c01af24dd53859400434e105fdf4cf78f9e863b7b2cc83ca1801d05cfd9f9c8a0296368a72d1bd3a2e529c01396a6eceb9dd6d29af7bdb3ab
7
+ data.tar.gz: dfe0e317939a893bc77c9f4cec36ee571e0039826f79c25d48efb4e16392ebdc7218af74b72c18b5b77d911df7a404ecac717d3397ad744cfa02f4f0b2ac4c41
data/Gemfile CHANGED
@@ -2,6 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
+ gem 'activerecord'
5
6
  gem 'redlock'
6
7
 
7
8
  platform :ruby do
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- travis-lock (0.1.4)
5
- activerecord (~> 4.0)
4
+ travis-lock (0.1.0)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
@@ -26,7 +25,7 @@ GEM
26
25
  i18n (0.7.0)
27
26
  json (1.8.3)
28
27
  metaclass (0.0.4)
29
- minitest (5.8.0)
28
+ minitest (5.8.1)
30
29
  mocha (1.1.0)
31
30
  metaclass (~> 0.0.1)
32
31
  pg (0.18.3)
@@ -54,6 +53,7 @@ PLATFORMS
54
53
  ruby
55
54
 
56
55
  DEPENDENCIES
56
+ activerecord
57
57
  mocha (~> 1.1)
58
58
  pg
59
59
  redlock
@@ -6,7 +6,7 @@ module Travis
6
6
  module Lock
7
7
  class Timeout < StandardError
8
8
  def initialize(name, options)
9
- super("Could not obtain lock for #{name}: #{options.map { |*pair| pair.join('=') }.join(' ')}")
9
+ super("Could not obtain lock for #{name}: #{options.map { |pair| pair.join('=') }.join(' ')}")
10
10
  end
11
11
  end
12
12
 
@@ -16,7 +16,7 @@ module Travis
16
16
 
17
17
  def exclusive(name, options = {}, &block)
18
18
  options[:strategy] ||= Lock.default_strategy || :none
19
- const_get(camelize(options[:strategy])).new(name, options).exclusive(&block)
19
+ Lock.const_get(camelize(options[:strategy])).new(name, options).exclusive(&block)
20
20
  end
21
21
 
22
22
  private
@@ -3,8 +3,12 @@
3
3
  # 13.3.4. Advisory Locks : http://www.postgresql.org/docs/9.3/static/explicit-locking.html
4
4
  # http://www.postgresql.org/docs/9.3/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
5
5
 
6
+ begin
7
+ require 'active_record'
8
+ rescue LoadError
9
+ end
10
+
6
11
  require 'zlib'
7
- require 'active_record'
8
12
  require 'travis/lock/support/retry'
9
13
 
10
14
  module Travis
@@ -8,31 +8,38 @@ module Travis
8
8
  module Lock
9
9
  class Redis
10
10
  class LockError < StandardError
11
+ attr_reader :key
12
+
11
13
  def initialize(key)
14
+ @key = key
12
15
  super("Could not obtain lock for #{key.inspect} on Redis.")
13
16
  end
14
17
  end
15
18
 
16
- extend MonitorMixin
19
+ def self.clients
20
+ @clients ||= {}
21
+ end
17
22
 
18
23
  DEFAULTS = {
19
24
  ttl: 5 * 60,
20
25
  retries: 5,
21
- interval: 0.1
26
+ interval: 0.1,
27
+ timeout: 0.5
22
28
  }
23
29
 
24
- attr_reader :name, :config, :retried
30
+ attr_reader :name, :config, :retried, :monitor
25
31
 
26
32
  def initialize(name, config)
27
33
  @name = name
28
34
  @config = DEFAULTS.merge(config)
29
35
  @retried = 0
36
+ @monitor = Monitor.new
30
37
  end
31
38
 
32
39
  def exclusive
33
40
  retrying do
34
41
  client.lock(name, config[:ttl]) do |lock|
35
- lock ? yield : raise(LockError.new(name))
42
+ lock ? yield(lock) : raise(LockError.new(name))
36
43
  end
37
44
  end
38
45
  end
@@ -40,7 +47,9 @@ module Travis
40
47
  private
41
48
 
42
49
  def client
43
- Redlock::Client.new([url])
50
+ monitor.synchronize do
51
+ self.class.clients[url] ||= Redlock::Client.new([url], redis_timeout: config[:timeout])
52
+ end
44
53
  end
45
54
 
46
55
  def url
@@ -26,7 +26,7 @@ module Travis
26
26
  end
27
27
 
28
28
  def timeout!
29
- fail Timeout.new(name, options)
29
+ fail Timeout.new(name, options || {})
30
30
  end
31
31
  end
32
32
  end
@@ -1,5 +1,5 @@
1
1
  module Travis
2
2
  module Lock
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -1,7 +1,10 @@
1
- require 'active_record'
1
+ begin
2
+ require 'active_record'
2
3
 
3
- ActiveRecord::Base.establish_connection(
4
- adapter: 'postgresql',
5
- database: 'travis_test',
6
- pool: 50
7
- )
4
+ ActiveRecord::Base.establish_connection(
5
+ adapter: 'postgresql',
6
+ database: 'travis_test',
7
+ pool: 50
8
+ )
9
+ rescue LoadError
10
+ end
@@ -4,6 +4,8 @@ describe Travis::Lock::Redis do
4
4
  let(:lock) { described_class.new(name, config) }
5
5
  let(:name) { 'name' }
6
6
 
7
+ after { Travis::Lock::Redis.instance_variable_set(:@clients, nil) }
8
+
7
9
  it 'yields' do
8
10
  lock.exclusive { @called = true }
9
11
  expect(@called).to eq(true)
@@ -9,14 +9,12 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Travis CI"]
10
10
  s.email = "contact@travis-ci.org"
11
11
  s.homepage = "https://github.com/travis-ci/travis-lock"
12
- s.summary = "Travis CI config"
12
+ s.summary = "Locks for use at Travis CI"
13
13
  s.description = "#{s.summary}."
14
- s.license = "MIT"
14
+ s.licenses = ["MIT"]
15
15
 
16
16
  s.files = Dir['{lib/**/*,spec/**/*,[A-Z]*}']
17
17
  s.platform = Gem::Platform::RUBY
18
18
  s.require_path = 'lib'
19
19
  s.rubyforge_project = '[none]'
20
-
21
- s.add_dependency 'activerecord' , '~> 4.0'
22
20
  end
metadata CHANGED
@@ -1,30 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: travis-lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis CI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-23 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activerecord
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '4.0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '4.0'
27
- description: Travis CI config.
11
+ date: 2016-10-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Locks for use at Travis CI.
28
14
  email: contact@travis-ci.org
29
15
  executables: []
30
16
  extensions: []
@@ -69,6 +55,6 @@ rubyforge_project: "[none]"
69
55
  rubygems_version: 2.4.5
70
56
  signing_key:
71
57
  specification_version: 4
72
- summary: Travis CI config
58
+ summary: Locks for use at Travis CI
73
59
  test_files: []
74
60
  has_rdoc: