services 0.0.2 → 0.1.0

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: 27e055eee440bd9ed9a3dbd75cd00f7d3cb698f7
4
- data.tar.gz: 2181326e31959eac2771f156dfbf70cd1823c879
3
+ metadata.gz: df1ba6063f4bba38b277be74e5ec56cc71872078
4
+ data.tar.gz: 63d57f90d5d3372c9c88e0aedc79b25a39f3c185
5
5
  SHA512:
6
- metadata.gz: d7f8e924430f1cdcbcbcdf1ff641c6aa11ec7bc48a48e485af11116ae4e34a7423e4f3331bcd032a77fa6e030cf10f1611633cb8cfb8f034fcb7723903790b63
7
- data.tar.gz: 04003c6e3ad8fb6ef39e48ec0c86d35ae039098884d3fe904e3bc2ff0d2efec903c75c12bb6907a62abb124a014d87157e4a81200e3c66f77474666e94c3381f
6
+ metadata.gz: b2ba9c8855a2c23bf6b559eb6023a8f72b0026e873e6307b7a3532ede57e9553bf3c8451dee1825f793ec183580a060bb4ff123604860dc6f1cd9d7b8a9f9636
7
+ data.tar.gz: 296aed48e3ce98785a24830bb944ab8763a0987cf38e31c91510372ebec2fbcd3dccd920bc23750e362976e9d146904db9e32e92e0acd62478df3dbcc205062e
@@ -2,21 +2,33 @@ module Services
2
2
  class Base
3
3
  module UniquenessChecker
4
4
  def self.prepended(mod)
5
+ mod.instance_eval do
6
+ def check_uniqueness?
7
+ @check_uniqueness
8
+ end
9
+
10
+ def check_uniqueness!
11
+ @check_uniqueness = true
12
+ end
13
+ end
5
14
  mod.const_set :NotUniqueError, Class.new(mod::Error)
6
15
  end
7
16
 
8
17
  def call(*args)
9
- # TODO: Check that Services.configuration.redis is set
10
- key = unique_key(args)
11
- if Services.configuration.redis.exists(key)
12
- raise self.class::NotUniqueError
13
- else
14
- Services.configuration.redis.setex key, 60 * 60, Time.now
15
- begin
16
- super
17
- ensure
18
- Services.configuration.redis.del key
18
+ if self.class.check_uniqueness?
19
+ key = unique_key(args)
20
+ if Services.configuration.redis.exists(key)
21
+ raise self.class::NotUniqueError
22
+ else
23
+ Services.configuration.redis.setex key, 60 * 60, Time.now
24
+ begin
25
+ super
26
+ ensure
27
+ Services.configuration.redis.del key
28
+ end
19
29
  end
30
+ else
31
+ super
20
32
  end
21
33
  end
22
34
 
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
data/services.gemspec CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
23
23
 
24
24
  gem.add_development_dependency 'rake'
25
25
  gem.add_development_dependency 'guard-rspec', '~> 4.2'
26
- gem.add_development_dependency 'rspec', '>= 3.0.0.beta2', '< 4'
26
+ gem.add_development_dependency 'rspec', '>= 3.0.0.rc1', '< 4'
27
27
  gem.add_development_dependency 'sidekiq', '~> 3.0'
28
28
  gem.add_development_dependency 'redis', '~> 3.0'
29
29
  gem.add_runtime_dependency 'rails', '>= 3.0.0'
@@ -64,12 +64,24 @@ describe Services::Base do
64
64
  end
65
65
 
66
66
  context 'checking for uniqueness' do
67
- it 'raises an error when the same job is executed twice' do
68
- LongRunningService.perform_async
69
- sleep 0.5 # Wait for Sidekiq to start processing the job
70
- expect do
71
- LongRunningService.call
72
- end.to raise_error(LongRunningService::NotUniqueError)
67
+ context 'when the service was set to check for uniqueness' do
68
+ it 'raises an error when the same job is executed twice' do
69
+ UniqueService.perform_async
70
+ sleep 0.5 # Wait for Sidekiq to start processing the job
71
+ expect do
72
+ UniqueService.call
73
+ end.to raise_error(UniqueService::NotUniqueError)
74
+ end
75
+ end
76
+
77
+ context 'when the service was not set to check for uniqueness' do
78
+ it 'does not raise an error when the same job is executed twice' do
79
+ NonUniqueService.perform_async
80
+ sleep 0.5 # Wait for Sidekiq to start processing the job
81
+ expect do
82
+ NonUniqueService.call
83
+ end.to_not raise_error
84
+ end
73
85
  end
74
86
  end
75
87
  end
data/spec/spec_helper.rb CHANGED
@@ -32,7 +32,7 @@ RSpec.configure do |config|
32
32
  config.filter_run :focus
33
33
  config.order = 'random'
34
34
 
35
- config.before :all do
35
+ config.before :suite do
36
36
  # Start Redis
37
37
  redis_options = {
38
38
  daemonize: 'yes',
@@ -59,7 +59,7 @@ RSpec.configure do |config|
59
59
  system "bundle exec sidekiq #{options_hash_to_string(sidekiq_options)}"
60
60
  end
61
61
 
62
- config.after :all do
62
+ config.after :suite do
63
63
  # Stop Sidekiq
64
64
  system "bundle exec sidekiqctl stop #{sidekiq_pidfile} #{sidekiq_timeout}"
65
65
  while File.exist?(sidekiq_pidfile)
@@ -1,5 +1,13 @@
1
- class LongRunningService < Services::Base
1
+ class UniqueService < Services::Base
2
+ check_uniqueness!
3
+
2
4
  def call
3
- sleep 2
5
+ sleep 1
6
+ end
7
+ end
8
+
9
+ class NonUniqueService < Services::Base
10
+ def call
11
+ sleep 1
4
12
  end
5
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: services
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-18 00:00:00.000000000 Z
11
+ date: 2014-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -44,7 +44,7 @@ dependencies:
44
44
  requirements:
45
45
  - - '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0.beta2
47
+ version: 3.0.0.rc1
48
48
  - - <
49
49
  - !ruby/object:Gem::Version
50
50
  version: '4'
@@ -54,7 +54,7 @@ dependencies:
54
54
  requirements:
55
55
  - - '>='
56
56
  - !ruby/object:Gem::Version
57
- version: 3.0.0.beta2
57
+ version: 3.0.0.rc1
58
58
  - - <
59
59
  - !ruby/object:Gem::Version
60
60
  version: '4'