services 0.4.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/services.rb +3 -2
- data/lib/services/{logger.rb → logger/file.rb} +5 -7
- data/lib/services/logger/redis.rb +28 -0
- data/lib/services/modules/call_logger.rb +4 -3
- data/lib/services/railtie.rb +0 -4
- data/lib/services/version.rb +1 -1
- data/services.gemspec +22 -21
- data/spec/services/logger/redis_spec.rb +55 -0
- data/spec/spec_helper.rb +4 -2
- metadata +22 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06b81795775cc40d634101b8736c934f9bb64b75
|
4
|
+
data.tar.gz: 5a80029a0b5da7660e4ecad40e6868f5ea444919
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5148f72b9f1bd8ee35592bb106e0e25222c284d0eed74a94ec41dfd2daffe3fc2f90223f701e2dc2b24b42f33f27dbd38c0653adf1b703602738cccef1642bc
|
7
|
+
data.tar.gz: 03b295a1abdda5f1377c720ffe4d292a360dd108dcd2ec0fc347477a44304382775f38b183c384b6e0069f668975ebd59cca3ce4a5d03d4fa15e8a475ae89a1a
|
data/README.md
CHANGED
@@ -49,7 +49,7 @@ The SQL `Services::BaseFinder` (discussed further down) generates is optimized f
|
|
49
49
|
|
50
50
|
The following service takes one or more users or user IDs as an argument.
|
51
51
|
|
52
|
-
```
|
52
|
+
```ruby
|
53
53
|
module Services
|
54
54
|
module Users
|
55
55
|
class Delete < Services::Base
|
@@ -72,7 +72,7 @@ It's good practice to always return the objects a service has been operating on
|
|
72
72
|
|
73
73
|
Another example, this time using `Services::BaseFinder`:
|
74
74
|
|
75
|
-
```
|
75
|
+
```ruby
|
76
76
|
module Services
|
77
77
|
module Users
|
78
78
|
class Find < Services::BaseFinder
|
data/lib/services.rb
CHANGED
@@ -7,13 +7,14 @@ module Services
|
|
7
7
|
|
8
8
|
with_configuration do
|
9
9
|
has :host, classes: String
|
10
|
-
has :
|
10
|
+
has :logger
|
11
11
|
has :redis
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
require_relative 'services/version'
|
16
|
-
require_relative 'services/logger'
|
16
|
+
require_relative 'services/logger/file'
|
17
|
+
require_relative 'services/logger/redis'
|
17
18
|
begin
|
18
19
|
require_relative 'services/asyncable'
|
19
20
|
rescue Services::BackgroundProcessorNotFound
|
@@ -1,17 +1,15 @@
|
|
1
1
|
require 'active_support/tagged_logging'
|
2
2
|
|
3
3
|
module Services
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
log_file = File.join(
|
4
|
+
module Logger
|
5
|
+
class File
|
6
|
+
def initialize(log_dir)
|
7
|
+
log_file = ::File.join(log_dir, 'services.log')
|
8
8
|
@logger = ActiveSupport::TaggedLogging.new(::Logger.new(log_file))
|
9
9
|
@logger.clear_tags!
|
10
10
|
end
|
11
|
-
end
|
12
11
|
|
13
|
-
|
14
|
-
unless @logger.nil?
|
12
|
+
def log(message, tags = [], severity = :info)
|
15
13
|
@logger.tagged Time.now, severity.upcase, *tags do
|
16
14
|
@logger.send severity, message
|
17
15
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Services
|
2
|
+
module Logger
|
3
|
+
class Redis
|
4
|
+
def initialize(redis, key = 'logs')
|
5
|
+
@redis, @key = redis, key
|
6
|
+
end
|
7
|
+
|
8
|
+
def log(message, tags = [], severity = :info)
|
9
|
+
value = {
|
10
|
+
time: Time.now.to_i,
|
11
|
+
message: message,
|
12
|
+
severity: severity,
|
13
|
+
tags: tags
|
14
|
+
}
|
15
|
+
@redis.lpush @key, value.to_json
|
16
|
+
end
|
17
|
+
|
18
|
+
def clear
|
19
|
+
@redis.multi do
|
20
|
+
@redis.lrange @key, 0, -1
|
21
|
+
@redis.del @key
|
22
|
+
end.first.map do |json|
|
23
|
+
JSON.load json
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -2,12 +2,14 @@ module Services
|
|
2
2
|
class Base
|
3
3
|
module CallLogger
|
4
4
|
def call(*args)
|
5
|
+
return super if Services.configuration.logger.nil?
|
6
|
+
|
5
7
|
log "START with args #{args}"
|
6
8
|
log "CALLED BY #{caller || '(not found)'}"
|
7
9
|
start = Time.now
|
8
10
|
begin
|
9
11
|
result = super
|
10
|
-
rescue
|
12
|
+
rescue => e
|
11
13
|
log_exception e
|
12
14
|
raise e
|
13
15
|
ensure
|
@@ -19,8 +21,7 @@ module Services
|
|
19
21
|
private
|
20
22
|
|
21
23
|
def log(message, severity = :info)
|
22
|
-
|
23
|
-
@logger.log [self.class, @id], message, severity
|
24
|
+
Services.configuration.logger.log message, [self.class, @id], severity
|
24
25
|
end
|
25
26
|
|
26
27
|
def log_exception(e, cause = false)
|
data/lib/services/railtie.rb
CHANGED
data/lib/services/version.rb
CHANGED
data/services.gemspec
CHANGED
@@ -6,27 +6,28 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
6
6
|
require 'services/version'
|
7
7
|
|
8
8
|
Gem::Specification.new do |gem|
|
9
|
-
gem.name
|
10
|
-
gem.version
|
11
|
-
gem.platform
|
12
|
-
gem.author
|
13
|
-
gem.email
|
14
|
-
gem.summary
|
15
|
-
gem.description
|
16
|
-
gem.homepage
|
17
|
-
gem.license
|
9
|
+
gem.name = 'services'
|
10
|
+
gem.version = Services::VERSION
|
11
|
+
gem.platform = Gem::Platform::RUBY
|
12
|
+
gem.author = 'Manuel Meurer'
|
13
|
+
gem.email = 'manuel@krautcomputing.com'
|
14
|
+
gem.summary = 'A nifty service layer for your Rails app'
|
15
|
+
gem.description = 'A nifty service layer for your Rails app'
|
16
|
+
gem.homepage = 'http://krautcomputing.github.io/services'
|
17
|
+
gem.license = 'MIT'
|
18
|
+
gem.required_ruby_version = '~> 2.0'
|
19
|
+
gem.files = `git ls-files`.split($/)
|
20
|
+
gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
|
21
|
+
gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
|
22
|
+
gem.require_paths = ['lib']
|
18
23
|
|
19
|
-
gem.
|
20
|
-
gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
|
21
|
-
gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
|
22
|
-
gem.require_paths = ['lib']
|
23
|
-
|
24
|
-
gem.add_development_dependency 'rake', '>= 0.9.0'
|
24
|
+
gem.add_development_dependency 'rake', '>= 0.9.0'
|
25
25
|
gem.add_development_dependency 'guard-rspec', '~> 4.2'
|
26
|
-
gem.add_development_dependency 'rspec',
|
27
|
-
gem.add_development_dependency 'sidekiq',
|
28
|
-
gem.add_development_dependency 'redis',
|
29
|
-
gem.add_development_dependency 'tries',
|
30
|
-
gem.
|
31
|
-
gem.add_runtime_dependency
|
26
|
+
gem.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
gem.add_development_dependency 'sidekiq', '~> 3.0'
|
28
|
+
gem.add_development_dependency 'redis', '~> 3.0'
|
29
|
+
gem.add_development_dependency 'tries', '~> 0.3'
|
30
|
+
gem.add_development_dependency 'timecop', '~> 0.7'
|
31
|
+
gem.add_runtime_dependency 'rails', '>= 3.0.0'
|
32
|
+
gem.add_runtime_dependency 'gem_config', '~> 0.3'
|
32
33
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Services::Logger::Redis do
|
4
|
+
let(:tags) { %w(foo bar baz) }
|
5
|
+
let(:message) { "One day baby we'll be old" }
|
6
|
+
let(:severity) { 'critical' }
|
7
|
+
let(:key) { 'custom_log_key' }
|
8
|
+
let(:redis) { Redis.new }
|
9
|
+
let(:logger) { described_class.new(redis, key) }
|
10
|
+
|
11
|
+
def log_entries
|
12
|
+
redis.lrange(key, 0, -1).map do |json|
|
13
|
+
JSON.load json
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#log' do
|
18
|
+
it 'logs properly' do
|
19
|
+
Timecop.freeze do
|
20
|
+
payload = {
|
21
|
+
'time' => Time.now.to_i,
|
22
|
+
'message' => message,
|
23
|
+
'severity' => severity,
|
24
|
+
'tags' => tags
|
25
|
+
}
|
26
|
+
expect do
|
27
|
+
logger.log message, tags, severity
|
28
|
+
end.to change { log_entries }.from([]).to([payload])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#clear' do
|
34
|
+
before do
|
35
|
+
(2.days.ago.to_i..Time.now.to_i).step(1.hour) do |timestamp|
|
36
|
+
time = Time.at(timestamp)
|
37
|
+
Timecop.freeze time do
|
38
|
+
tags = [time.strftime('%a')]
|
39
|
+
logger.log tags, time.to_s(:long)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
expect(log_entries.size).to be > 0
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'returns all log entries' do
|
46
|
+
expect(log_entries).to eq(logger.clear)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'clears all log entries' do
|
50
|
+
expect do
|
51
|
+
logger.clear
|
52
|
+
end.to change { log_entries }.to([])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,8 @@ require 'rspec'
|
|
2
2
|
require 'tries'
|
3
3
|
require 'redis'
|
4
4
|
require 'sidekiq'
|
5
|
+
require 'timecop'
|
6
|
+
require 'active_support/core_ext'
|
5
7
|
|
6
8
|
require_relative '../lib/services'
|
7
9
|
|
@@ -26,8 +28,8 @@ sidekiq_pidfile = support_dir.join('sidekiq.pid')
|
|
26
28
|
sidekiq_timeout = 20
|
27
29
|
|
28
30
|
Services.configure do |config|
|
29
|
-
config.redis
|
30
|
-
config.
|
31
|
+
config.redis = Redis.new
|
32
|
+
config.logger = Services::Logger::File.new(log_dir)
|
31
33
|
end
|
32
34
|
|
33
35
|
Sidekiq.configure_client do |config|
|
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.
|
4
|
+
version: 1.0.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-11-
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: timecop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.7'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.7'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: rails
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -139,7 +153,8 @@ files:
|
|
139
153
|
- lib/services/asyncable.rb
|
140
154
|
- lib/services/base.rb
|
141
155
|
- lib/services/base_finder.rb
|
142
|
-
- lib/services/logger.rb
|
156
|
+
- lib/services/logger/file.rb
|
157
|
+
- lib/services/logger/redis.rb
|
143
158
|
- lib/services/modules/call_logger.rb
|
144
159
|
- lib/services/modules/exception_wrapper.rb
|
145
160
|
- lib/services/modules/uniqueness_checker.rb
|
@@ -147,6 +162,7 @@ files:
|
|
147
162
|
- lib/services/version.rb
|
148
163
|
- services.gemspec
|
149
164
|
- spec/services/base_spec.rb
|
165
|
+
- spec/services/logger/redis_spec.rb
|
150
166
|
- spec/services/modules/call_logger_spec.rb
|
151
167
|
- spec/services/modules/exception_wrapper_spec.rb
|
152
168
|
- spec/services/modules/uniqueness_checker_spec.rb
|
@@ -167,9 +183,9 @@ require_paths:
|
|
167
183
|
- lib
|
168
184
|
required_ruby_version: !ruby/object:Gem::Requirement
|
169
185
|
requirements:
|
170
|
-
- - "
|
186
|
+
- - "~>"
|
171
187
|
- !ruby/object:Gem::Version
|
172
|
-
version: '0'
|
188
|
+
version: '2.0'
|
173
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
190
|
requirements:
|
175
191
|
- - ">="
|
@@ -183,6 +199,7 @@ specification_version: 4
|
|
183
199
|
summary: A nifty service layer for your Rails app
|
184
200
|
test_files:
|
185
201
|
- spec/services/base_spec.rb
|
202
|
+
- spec/services/logger/redis_spec.rb
|
186
203
|
- spec/services/modules/call_logger_spec.rb
|
187
204
|
- spec/services/modules/exception_wrapper_spec.rb
|
188
205
|
- spec/services/modules/uniqueness_checker_spec.rb
|