services 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Guardfile +1 -1
- data/lib/services/modules/call_logger.rb +10 -2
- data/lib/services/version.rb +1 -1
- data/services.gemspec +1 -1
- data/spec/services/base_spec.rb +34 -2
- data/spec/spec_helper.rb +27 -20
- data/spec/support/test_services.rb +11 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbbec960a41d81815da15861aa4e5abf3941cd46
|
4
|
+
data.tar.gz: 4e390c06247de93c8eb6e45050392566767a2903
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 998625c59ca525eb537f52d5317cff7d0ac8cb68f787bb1b46dbf16f33a00917eae854ee24e27763a13422e22b6b62a4e65bda26213e8a527f0d356a46e7adea
|
7
|
+
data.tar.gz: 73db3259b704dc5b18a3468561c634cfc84d71ffea2288df47523dd9eda51379ae498a96aa470751f2daffc85d5ee2dc5948d312888d10278aa9dfb9138832ec
|
data/Guardfile
CHANGED
@@ -2,7 +2,8 @@ module Services
|
|
2
2
|
class Base
|
3
3
|
module CallLogger
|
4
4
|
def call(*args)
|
5
|
-
log "START with args
|
5
|
+
log "START with args #{args}"
|
6
|
+
log "CALLED BY #{caller}"
|
6
7
|
start = Time.now
|
7
8
|
begin
|
8
9
|
result = super
|
@@ -24,7 +25,7 @@ module Services
|
|
24
25
|
|
25
26
|
def log_exception(e, cause = false)
|
26
27
|
log "#{'caused by: ' if cause}#{e.class}: #{e.message}"
|
27
|
-
if e.respond_to?(:cause) &&
|
28
|
+
if e.respond_to?(:cause) && e.cause
|
28
29
|
e.backtrace.take(5).each do |line|
|
29
30
|
log " #{line}"
|
30
31
|
end
|
@@ -35,6 +36,13 @@ module Services
|
|
35
36
|
end
|
36
37
|
end
|
37
38
|
end
|
39
|
+
|
40
|
+
def caller
|
41
|
+
caller_location = caller_locations(2, 1).first
|
42
|
+
caller_path = caller_location.path
|
43
|
+
caller_path = caller_path.sub(%r(\A#{Regexp.escape Rails.root.to_s}/), '') if defined?(Rails)
|
44
|
+
[caller_path, caller_location.lineno].join(':')
|
45
|
+
end
|
38
46
|
end
|
39
47
|
end
|
40
48
|
end
|
data/lib/services/version.rb
CHANGED
data/services.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
|
22
22
|
gem.require_paths = ['lib']
|
23
23
|
|
24
|
-
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'rake', '>= 0.9.0'
|
25
25
|
gem.add_development_dependency 'guard-rspec', '~> 4.2'
|
26
26
|
gem.add_development_dependency 'rspec', '~> 3.0'
|
27
27
|
gem.add_development_dependency 'sidekiq', '~> 3.0'
|
data/spec/services/base_spec.rb
CHANGED
@@ -170,9 +170,41 @@ describe Services::Base do
|
|
170
170
|
end
|
171
171
|
end
|
172
172
|
|
173
|
-
context 'logging
|
173
|
+
context 'logging' do
|
174
|
+
it 'logs start with args and end with duration' do
|
175
|
+
service = EmptyService.new
|
176
|
+
logs = []
|
177
|
+
allow(service).to receive(:log) do |message, *|
|
178
|
+
logs << message
|
179
|
+
end
|
180
|
+
service.call 'foo', 'bar'
|
181
|
+
expect(logs.first).to eq('START with args ["foo", "bar"]')
|
182
|
+
expect(logs.last).to eq('END after 0.0 seconds')
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'logs the caller' do
|
186
|
+
service_calling_service, called_service = ServiceCallingService.new, EmptyService.new
|
187
|
+
logs = []
|
188
|
+
allow(called_service).to receive(:log) do |message, *|
|
189
|
+
logs << message
|
190
|
+
end
|
191
|
+
|
192
|
+
# When Rails is not defined, the complete caller path should be logged
|
193
|
+
service_calling_service.call called_service
|
194
|
+
expect(logs).to include(/\ACALLED BY #{Regexp.escape(PROJECT_ROOT.join(SERVICES_PATH).to_s)}:\d+/)
|
195
|
+
|
196
|
+
# When Rails is defined, only the caller path relative to Rails.root is logged
|
197
|
+
class Rails
|
198
|
+
def self.root; PROJECT_ROOT; end
|
199
|
+
end
|
200
|
+
logs = []
|
201
|
+
service_calling_service.call called_service
|
202
|
+
expect(logs).to include(/\ACALLED BY #{Regexp.escape(SERVICES_PATH)}:\d+/)
|
203
|
+
Object.send :remove_const, :Rails
|
204
|
+
end
|
205
|
+
|
174
206
|
if RUBY_VERSION > '2.1'
|
175
|
-
it 'logs
|
207
|
+
it 'logs exceptions and exception causes' do
|
176
208
|
service = NestedExceptionService.new
|
177
209
|
logs = []
|
178
210
|
allow(service).to receive(:log) do |message, *|
|
data/spec/spec_helper.rb
CHANGED
@@ -2,10 +2,15 @@ require 'rspec'
|
|
2
2
|
require 'tries'
|
3
3
|
require 'redis'
|
4
4
|
require 'sidekiq'
|
5
|
+
|
5
6
|
require_relative '../lib/services'
|
6
|
-
require_relative 'support/test_services'
|
7
7
|
require_relative 'support/helpers'
|
8
8
|
|
9
|
+
SERVICES_PATH = File.join('support', 'test_services.rb')
|
10
|
+
require_relative SERVICES_PATH
|
11
|
+
|
12
|
+
PROJECT_ROOT = Pathname.new(File.expand_path('..', __FILE__))
|
13
|
+
|
9
14
|
support_dir = Pathname.new(File.expand_path('../support', __FILE__))
|
10
15
|
log_dir = support_dir.join('log')
|
11
16
|
|
@@ -35,18 +40,19 @@ RSpec.configure do |config|
|
|
35
40
|
config.order = 'random'
|
36
41
|
|
37
42
|
config.before :suite do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
43
|
+
unless ENV['TRAVIS']
|
44
|
+
# Start Redis
|
45
|
+
redis_options = {
|
46
|
+
daemonize: 'yes',
|
47
|
+
port: redis_port,
|
48
|
+
dir: support_dir,
|
49
|
+
dbfilename: 'redis.rdb',
|
50
|
+
logfile: log_dir.join('redis.log'),
|
51
|
+
pidfile: redis_pidfile
|
52
|
+
}
|
53
|
+
redis = support_dir.join('redis-server')
|
54
|
+
system "#{redis} #{options_hash_to_string(redis_options)}"
|
55
|
+
end
|
50
56
|
|
51
57
|
# Start Sidekiq
|
52
58
|
sidekiq_options = {
|
@@ -69,13 +75,14 @@ RSpec.configure do |config|
|
|
69
75
|
sleep 1
|
70
76
|
end
|
71
77
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
78
|
+
unless ENV['TRAVIS']
|
79
|
+
# Stop Redis
|
80
|
+
redis_cli = support_dir.join('redis-cli')
|
81
|
+
system "#{redis_cli} -p #{redis_port} shutdown"
|
82
|
+
while File.exist?(redis_pidfile)
|
83
|
+
puts 'Waiting for Redis to shut down...'
|
84
|
+
sleep 1
|
85
|
+
end
|
79
86
|
end
|
80
87
|
|
81
88
|
# Truncate log files
|
@@ -47,12 +47,23 @@ module Services
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
class EmptyService < Services::Base
|
51
|
+
def call(*args)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
50
55
|
class ErrorService < Services::Base
|
51
56
|
def call
|
52
57
|
raise Error.new('I am a service error.')
|
53
58
|
end
|
54
59
|
end
|
55
60
|
|
61
|
+
class ServiceCallingService < Services::Base
|
62
|
+
def call(service)
|
63
|
+
service.call
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
56
67
|
class UniqueService < Services::Base
|
57
68
|
def call
|
58
69
|
check_uniqueness!
|
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.2.
|
4
|
+
version: 0.2.7
|
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-08-
|
11
|
+
date: 2014-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.9.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.9.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: guard-rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -172,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
172
|
version: '0'
|
173
173
|
requirements: []
|
174
174
|
rubyforge_project:
|
175
|
-
rubygems_version: 2.
|
175
|
+
rubygems_version: 2.4.1
|
176
176
|
signing_key:
|
177
177
|
specification_version: 4
|
178
178
|
summary: ''
|