bellbro 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/bellbro.gemspec +1 -1
- data/lib/bellbro.rb +2 -13
- data/lib/bellbro/service.rb +7 -6
- data/lib/bellbro/sidekiq_utils.rb +4 -2
- data/lib/bellbro/trackable.rb +3 -5
- data/lib/bellbro/version.rb +1 -1
- data/lib/bellbro/worker.rb +2 -1
- data/spec/support/{bellbro.rb → shout.rb} +1 -1
- metadata +19 -12
- data/lib/bellbro/bell.rb +0 -7
- data/lib/bellbro/initialize.rb +0 -61
- data/lib/bellbro/keyable.rb +0 -70
- data/lib/bellbro/redis_pool.rb +0 -31
- data/lib/bellbro/retryable.rb +0 -67
- data/lib/bellbro/ringable.rb +0 -14
- data/lib/bellbro/settings.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ab7478116c23d04ae5d103a43267dc6ad90563c
|
4
|
+
data.tar.gz: 259f5b67b770f5d6a4fa997eac99c3d2cb17ca92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d6c331e559941c713cacf0e4325bc5a55742b953c10d5a82494cd8cc8d6b98da78127e262e0877af93e43fae4aeeb4da6dbd112b40fe3f946ff79ac28e6523d
|
7
|
+
data.tar.gz: 39a37fb409cf9005de5b7ec609b7f6afb382e519eda35ec8bf1716e51c199c504c074a0764c72c36d7c9b5e90cdf5df59c6a289cd31dc6401dcff32d995434e4
|
data/Gemfile
CHANGED
data/bellbro.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_dependency "sidekiq"
|
22
22
|
spec.add_dependency "redis"
|
23
23
|
spec.add_dependency "airbrake"
|
24
|
-
|
24
|
+
spec.add_dependency 'retryable'
|
25
25
|
|
26
26
|
spec.add_development_dependency "bundler", "~> 1.7"
|
27
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
data/lib/bellbro.rb
CHANGED
@@ -6,26 +6,15 @@ require 'yaml'
|
|
6
6
|
require 'digest'
|
7
7
|
require 'sidekiq'
|
8
8
|
require 'airbrake'
|
9
|
+
require 'retryable'
|
10
|
+
require 'shout'
|
9
11
|
|
10
12
|
%w(
|
11
|
-
bellbro/settings.rb
|
12
13
|
bellbro/hooks.rb
|
13
|
-
bellbro/keyable.rb
|
14
|
-
bellbro/retryable.rb
|
15
|
-
bellbro/ringable.rb
|
16
14
|
bellbro/trackable.rb
|
17
|
-
bellbro/redis_pool.rb
|
18
15
|
bellbro/sidekiq_utils.rb
|
19
|
-
bellbro/bell.rb
|
20
16
|
bellbro/service.rb
|
21
17
|
bellbro/worker.rb
|
22
|
-
bellbro/initialize.rb
|
23
18
|
).each do |path|
|
24
19
|
require File.join(File.dirname(__FILE__),path)
|
25
|
-
end
|
26
|
-
|
27
|
-
module Bellbro
|
28
|
-
def self.logger
|
29
|
-
Bellbro::Settings.logger
|
30
|
-
end
|
31
20
|
end
|
data/lib/bellbro/service.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'digest'
|
2
2
|
|
3
3
|
module Bellbro
|
4
|
-
class Service
|
4
|
+
class Service
|
5
5
|
include Bellbro::SidekiqUtils
|
6
6
|
include Bellbro::Trackable
|
7
|
+
include Shout
|
7
8
|
|
8
9
|
attr_reader :thread, :thread_error, :jid
|
9
10
|
|
@@ -36,7 +37,7 @@ module Bellbro
|
|
36
37
|
begin
|
37
38
|
run
|
38
39
|
rescue Exception => @thread_error
|
39
|
-
|
40
|
+
log "#{@thread_error.inspect}", type: :error
|
40
41
|
Airbrake.notify(@thread_error)
|
41
42
|
raise @thread_error
|
42
43
|
end
|
@@ -45,13 +46,13 @@ module Bellbro
|
|
45
46
|
|
46
47
|
def stop
|
47
48
|
@done = true
|
48
|
-
|
49
|
+
log "Stopping #{self.class} service..."
|
49
50
|
@thread.join
|
50
|
-
|
51
|
+
log "#{self.class.to_s.capitalize} service stopped."
|
51
52
|
end
|
52
53
|
|
53
54
|
def run
|
54
|
-
|
55
|
+
log "Starting #{self.class} service."
|
55
56
|
self.class.mutex.synchronize { track }
|
56
57
|
begin
|
57
58
|
self.class.mutex.synchronize { start_jobs }
|
@@ -64,7 +65,7 @@ module Bellbro
|
|
64
65
|
def start_jobs
|
65
66
|
each_job do |job|
|
66
67
|
jid = worker_class.perform_async(job)
|
67
|
-
|
68
|
+
log "Starting job #{jid} #{worker_class.name} with #{job.inspect}."
|
68
69
|
record_incr(:jobs_started)
|
69
70
|
end
|
70
71
|
end
|
@@ -1,14 +1,16 @@
|
|
1
|
+
require 'retryable'
|
2
|
+
|
1
3
|
module Bellbro
|
2
4
|
module SidekiqUtils
|
3
5
|
|
4
6
|
def _workers
|
5
|
-
retryable(on: Redis::TimeoutError) do
|
7
|
+
Retryable.retryable(on: Redis::TimeoutError) do
|
6
8
|
workers_for_class("#{self.name}")
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
12
|
def _jobs
|
11
|
-
retryable(on: Redis::TimeoutError) do
|
13
|
+
Retryable.retryable(on: Redis::TimeoutError) do
|
12
14
|
jobs_for_class("#{self.name}")
|
13
15
|
end
|
14
16
|
end
|
data/lib/bellbro/trackable.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module Bellbro
|
2
2
|
module Trackable
|
3
|
-
include Bellbro::Retryable
|
4
|
-
|
5
3
|
attr_reader :record
|
6
4
|
|
7
5
|
def self.included(base)
|
@@ -32,13 +30,13 @@ module Bellbro
|
|
32
30
|
end
|
33
31
|
|
34
32
|
def status_update(force = false)
|
35
|
-
return unless @log_record_schema &&
|
33
|
+
return unless @log_record_schema && Shout.logger
|
36
34
|
return unless force || ((@count += 1) % @write_interval) == 0
|
37
|
-
retryable { write_log(@record.to_json) }
|
35
|
+
Retryable.retryable { write_log(@record.to_json) }
|
38
36
|
end
|
39
37
|
|
40
38
|
def write_log(line)
|
41
|
-
|
39
|
+
Shout.logger.info line
|
42
40
|
end
|
43
41
|
|
44
42
|
def record_set(attr, value)
|
data/lib/bellbro/version.rb
CHANGED
data/lib/bellbro/worker.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bellbro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Stokes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
name: retryable
|
76
|
+
prerelease: false
|
77
|
+
type: :runtime
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
requirement: !ruby/object:Gem::Requirement
|
71
85
|
requirements:
|
@@ -136,21 +150,14 @@ files:
|
|
136
150
|
- Rakefile
|
137
151
|
- bellbro.gemspec
|
138
152
|
- lib/bellbro.rb
|
139
|
-
- lib/bellbro/bell.rb
|
140
153
|
- lib/bellbro/hooks.rb
|
141
|
-
- lib/bellbro/initialize.rb
|
142
|
-
- lib/bellbro/keyable.rb
|
143
|
-
- lib/bellbro/redis_pool.rb
|
144
|
-
- lib/bellbro/retryable.rb
|
145
|
-
- lib/bellbro/ringable.rb
|
146
154
|
- lib/bellbro/service.rb
|
147
|
-
- lib/bellbro/settings.rb
|
148
155
|
- lib/bellbro/sidekiq_utils.rb
|
149
156
|
- lib/bellbro/trackable.rb
|
150
157
|
- lib/bellbro/version.rb
|
151
158
|
- lib/bellbro/worker.rb
|
152
159
|
- spec/spec_helper.rb
|
153
|
-
- spec/support/
|
160
|
+
- spec/support/shout.rb
|
154
161
|
- spec/support/sidekiq.rb
|
155
162
|
- spec/trackable_spec.rb
|
156
163
|
- spec/worker_spec.rb
|
@@ -174,13 +181,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
181
|
version: '0'
|
175
182
|
requirements: []
|
176
183
|
rubyforge_project:
|
177
|
-
rubygems_version: 2.
|
184
|
+
rubygems_version: 2.1.9
|
178
185
|
signing_key:
|
179
186
|
specification_version: 4
|
180
187
|
summary: Helps with sidekiq.
|
181
188
|
test_files:
|
182
189
|
- spec/spec_helper.rb
|
183
|
-
- spec/support/
|
190
|
+
- spec/support/shout.rb
|
184
191
|
- spec/support/sidekiq.rb
|
185
192
|
- spec/trackable_spec.rb
|
186
193
|
- spec/worker_spec.rb
|
data/lib/bellbro/bell.rb
DELETED
data/lib/bellbro/initialize.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
module Bellbro
|
2
|
-
def self.initialize_redis!
|
3
|
-
return unless defined?(Rails)
|
4
|
-
filename = "#{Rails.root}/config/redis.yml"
|
5
|
-
return unless File.exists?(filename)
|
6
|
-
config = YAML.load_file(filename)[Rails.env].symbolize_keys
|
7
|
-
configure_bellbro(config)
|
8
|
-
setup_pool(config)
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.configure_bellbro(config)
|
12
|
-
redis_config = ThreadSafe::Cache.new
|
13
|
-
|
14
|
-
config.each do |name, c|
|
15
|
-
base_url = Figaro.env.send c['url'].downcase
|
16
|
-
db = c['database']
|
17
|
-
url = generate_full_url(base_url, db)
|
18
|
-
|
19
|
-
redis_config[name] = {
|
20
|
-
size: c['pool'],
|
21
|
-
url: url,
|
22
|
-
database: db,
|
23
|
-
timeout: c['timeout'] || 5,
|
24
|
-
network_timeout: c['network_timeout'] || 5
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
|
-
Bellbro::Settings.configure do |con|
|
29
|
-
con.redis_config = redis_config
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.setup_pool(config)
|
34
|
-
redis_pool = ThreadSafe::Cache.new
|
35
|
-
redis_config = Bellbro::Settings.redis_config
|
36
|
-
|
37
|
-
config.each_key do |name|
|
38
|
-
next if name == :sidekiq
|
39
|
-
puts "# Configuring pool #{name} with size #{redis_config[name][:size]} on #{redis_config[name][:url]}"
|
40
|
-
redis_pool[name] = ConnectionPool.new(
|
41
|
-
redis_config[name].slice(:size, :timeout)
|
42
|
-
) do
|
43
|
-
Redis.new(
|
44
|
-
redis_config[name].slice(:url, :network_timeout)
|
45
|
-
)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
Bellbro::Settings.configure do |con|
|
50
|
-
con.redis_pool = redis_pool
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.generate_full_url(base_url, db)
|
55
|
-
if base_url[-1,1] == "/"
|
56
|
-
"#{base_url}#{db}"
|
57
|
-
else
|
58
|
-
"#{base_url}/#{db}"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
data/lib/bellbro/keyable.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
module Bellbro
|
2
|
-
module Keyable
|
3
|
-
def self.included(base)
|
4
|
-
base.class_eval do
|
5
|
-
extend ClassMethods
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
def respond_to?(method)
|
10
|
-
return true if @respond_to && @respond_to.include?(method.to_sym)
|
11
|
-
super
|
12
|
-
end
|
13
|
-
|
14
|
-
module ClassMethods
|
15
|
-
def delegate_all_keys(*methods)
|
16
|
-
delegate_getter_keys(*methods)
|
17
|
-
delegate_setter_keys(*methods)
|
18
|
-
delegate_status_keys(*methods)
|
19
|
-
end
|
20
|
-
|
21
|
-
def delegate_getter_keys(*methods)
|
22
|
-
options = extract_methods(methods)
|
23
|
-
options[:methods].each do |key|
|
24
|
-
@respond_to << key
|
25
|
-
define_method key do
|
26
|
-
send(options[:to])[key]
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def delegate_setter_keys(*methods)
|
32
|
-
options = extract_methods(methods)
|
33
|
-
options[:methods].each do |key|
|
34
|
-
@respond_to << "#{key}="
|
35
|
-
define_method "#{key}=" do |value|
|
36
|
-
send(options[:to])[key] = value
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def delegate_status_keys(*methods)
|
42
|
-
options = extract_methods(methods)
|
43
|
-
options[:methods].each do |key|
|
44
|
-
@respond_to << "#{key}?"
|
45
|
-
define_method "#{key}?" do
|
46
|
-
!!send(options[:to])[key]
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def extract_methods(methods)
|
54
|
-
options = methods.pop
|
55
|
-
unless options.is_a?(Hash) && to = options[:to].try(:to_sym)
|
56
|
-
raise ArgumentError, 'Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter).'
|
57
|
-
end
|
58
|
-
|
59
|
-
# Set up the data hash, if needed
|
60
|
-
if respond_to?(to) && !send(to).respond_to?(:[]) && !send(to).respond_to?(:[]=)
|
61
|
-
raise ArgumentError, 'Target must be a hash-like object.'
|
62
|
-
end
|
63
|
-
|
64
|
-
@respond_to ||= []
|
65
|
-
{ methods: methods.map(&:to_sym), to: to }
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
data/lib/bellbro/redis_pool.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
module Bellbro
|
2
|
-
module RedisPool
|
3
|
-
def with_connection
|
4
|
-
self.class.with_connection do |c|
|
5
|
-
yield c
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.included(klass)
|
10
|
-
class << klass
|
11
|
-
|
12
|
-
def set_db(default)
|
13
|
-
@default_db_name = default.to_sym
|
14
|
-
end
|
15
|
-
|
16
|
-
def with_connection
|
17
|
-
retryable(sleep: 0.5) do
|
18
|
-
model_pool.with do |c|
|
19
|
-
yield c
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def model_pool
|
25
|
-
@model_pool ||= Bellbro::Settings.redis_pool[@default_db_name]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/lib/bellbro/retryable.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
module Bellbro
|
2
|
-
module Retryable
|
3
|
-
def self.included(klass)
|
4
|
-
klass.extend(self)
|
5
|
-
end
|
6
|
-
|
7
|
-
# Options:
|
8
|
-
# * :tries - Number of retries to perform. Defaults to 1.
|
9
|
-
# * :on - The Exception on which a retry will be performed. Defaults to Exception, which retries on any Exception.
|
10
|
-
#
|
11
|
-
# Example
|
12
|
-
# =======
|
13
|
-
# retryable(:tries => 1, :on => OpenURI::HTTPError) do
|
14
|
-
# # your code here
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
|
18
|
-
def retryable(options = {}, &block)
|
19
|
-
opts = { :tries => 5, :on => Exception, :sleep => 1 }.merge(options)
|
20
|
-
retry_exception, retries, interval = opts[:on], opts[:tries], opts[:sleep]
|
21
|
-
|
22
|
-
begin
|
23
|
-
return yield
|
24
|
-
rescue retry_exception
|
25
|
-
sleep interval
|
26
|
-
retry unless (retries -= 1).zero?
|
27
|
-
end
|
28
|
-
yield
|
29
|
-
end
|
30
|
-
|
31
|
-
def retryable_with_success(options = {}, &block)
|
32
|
-
opts = { :tries => 5, :on => Exception, :sleep => 1 }.merge(options)
|
33
|
-
retry_exception, retries, interval = opts[:on], opts[:tries], opts[:sleep]
|
34
|
-
|
35
|
-
success = false
|
36
|
-
begin
|
37
|
-
yield
|
38
|
-
success = true
|
39
|
-
rescue retry_exception
|
40
|
-
sleep interval
|
41
|
-
retry unless (retries -= 1).zero?
|
42
|
-
end
|
43
|
-
success
|
44
|
-
end
|
45
|
-
|
46
|
-
def retryable_with_aws(options = {}, &block)
|
47
|
-
# This is for use with AWS-based models. I find I get a lot of these timeouts and
|
48
|
-
# OpenSSL errors, and sometimes the connection dies and I need to create a new
|
49
|
-
# connection object. I put this in a method called aws_connect!.
|
50
|
-
#
|
51
|
-
opts = { :tries => 10, :on => Exception, :sleep => 1 }.merge(options)
|
52
|
-
retry_exception, retries, interval = opts[:on], opts[:tries], opts[:sleep]
|
53
|
-
|
54
|
-
begin
|
55
|
-
return yield
|
56
|
-
rescue OpenSSL::SSL::SSLError, Timeout::Error
|
57
|
-
sleep interval
|
58
|
-
aws_connect!
|
59
|
-
retry unless (retries -= 1).zero?
|
60
|
-
rescue retry_exception
|
61
|
-
sleep interval
|
62
|
-
retry unless (retries -= 1).zero?
|
63
|
-
end
|
64
|
-
yield
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
data/lib/bellbro/ringable.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
module Bellbro
|
2
|
-
module Ringable
|
3
|
-
def self.included(klass)
|
4
|
-
klass.extend(self)
|
5
|
-
end
|
6
|
-
|
7
|
-
def ring(logline, opts={})
|
8
|
-
domain_insert = @domain ? "[#{@domain}]": ""
|
9
|
-
error_insert = (opts[:type] == :error) ? "##ERROR## " : ""
|
10
|
-
complete_logline = "[#{self.class}](#{Thread.current.object_id})#{domain_insert}: #{error_insert}#{logline}"
|
11
|
-
Bellbro.logger.info complete_logline
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
data/lib/bellbro/settings.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
module Bellbro
|
2
|
-
module Settings
|
3
|
-
|
4
|
-
class SettingsData < Struct.new(
|
5
|
-
:logger, :env, :redis_config, :redis_pool
|
6
|
-
)
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.configuration
|
10
|
-
@configuration ||= Bellbro::Settings::SettingsData.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.configure
|
14
|
-
yield configuration
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.env
|
18
|
-
return unless configured?
|
19
|
-
configuration.env
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.test?
|
23
|
-
return unless configured?
|
24
|
-
configuration.env == 'test'
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.redis_pool
|
28
|
-
return unless configured?
|
29
|
-
configuration.redis_pool
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.redis_config
|
33
|
-
return unless configured?
|
34
|
-
configuration.redis_config
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.logger
|
38
|
-
return unless configured?
|
39
|
-
configuration.logger
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.configured?
|
43
|
-
!!configuration
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|