bellbro 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/bellbro.gemspec +3 -0
- data/lib/bellbro/bell.rb +1 -1
- data/lib/bellbro/{ringer.rb → ringable.rb} +1 -1
- data/lib/bellbro/service.rb +1 -1
- data/lib/bellbro/trackable.rb +24 -5
- data/lib/bellbro/version.rb +1 -1
- data/lib/bellbro/worker.rb +1 -0
- data/lib/bellbro.rb +21 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/support/bellbro.rb +7 -0
- data/spec/support/sidekiq.rb +7 -0
- data/spec/trackable_spec.rb +89 -0
- data/spec/worker_spec.rb +22 -0
- metadata +55 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68c58a1bb724a51d658a0d973c83c361360c8d86
|
4
|
+
data.tar.gz: 45a769183a5357bd3bcfcbf63bcb0c8ae16c33b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bfaa5bd73233a5a3a6324cad5f9f8683e05164ee71789e90a0a63ae30130ac5ae5d84423d3369ca8d9caac4580b5c0a17b3eedbace79b903a99d8aa93fd70f7
|
7
|
+
data.tar.gz: 51bd64416a5555b91974b44698273304073401e1eb4379e621ee7749c74d0f2ee135f4b5dfbef4b23b929ee56f28efcb5ffd7ae8367326d14655335fc7348e5b
|
data/.gitignore
CHANGED
data/bellbro.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
+
spec.add_dependency "activesupport"
|
20
21
|
spec.add_dependency "sidekiq"
|
21
22
|
spec.add_dependency "redis"
|
22
23
|
spec.add_dependency "airbrake"
|
@@ -24,4 +25,6 @@ Gem::Specification.new do |spec|
|
|
24
25
|
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.7"
|
26
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec"
|
29
|
+
spec.add_development_dependency "yell"
|
27
30
|
end
|
data/lib/bellbro/bell.rb
CHANGED
data/lib/bellbro/service.rb
CHANGED
@@ -5,7 +5,7 @@ module Bellbro
|
|
5
5
|
include Bellbro::SidekiqUtils
|
6
6
|
include Bellbro::Trackable
|
7
7
|
|
8
|
-
SLEEP_INTERVAL = Rails.env.test? ? 1 : 3600
|
8
|
+
SLEEP_INTERVAL = defined?(Rails) && Rails.env.test? ? 1 : 3600
|
9
9
|
LOG_RECORD_SCHEMA = { jobs_started: Integer }
|
10
10
|
|
11
11
|
attr_reader :thread, :thread_error, :jid
|
data/lib/bellbro/trackable.rb
CHANGED
@@ -1,12 +1,27 @@
|
|
1
1
|
module Bellbro
|
2
2
|
module Trackable
|
3
3
|
include Bellbro::Retryable
|
4
|
+
|
4
5
|
attr_reader :record
|
5
6
|
|
7
|
+
def self.included(klass)
|
8
|
+
class << klass
|
9
|
+
def track_with_schema(hash)
|
10
|
+
self.class_eval do
|
11
|
+
@log_record_schema = hash
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def log_record_schema
|
16
|
+
@log_record_schema
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
6
21
|
def track(opts={})
|
7
22
|
return if @log_record_schema # Ignore repeated calls to #track, as in RefreshLinksWorker
|
8
23
|
opts.symbolize_keys!
|
9
|
-
@log_record_schema = self.class
|
24
|
+
@log_record_schema = self.class.log_record_schema
|
10
25
|
@write_interval = opts[:write_interval] || 500
|
11
26
|
@count = 0
|
12
27
|
@tracking = true
|
@@ -16,7 +31,11 @@ module Bellbro
|
|
16
31
|
|
17
32
|
def status_update(force = false)
|
18
33
|
return unless force || ((@count += 1) % @write_interval) == 0
|
19
|
-
retryable {
|
34
|
+
retryable { write_log(@record.to_json) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def write_log(line)
|
38
|
+
Bellbro.logger.info line
|
20
39
|
end
|
21
40
|
|
22
41
|
def record_set(attr, value)
|
@@ -50,7 +69,7 @@ module Bellbro
|
|
50
69
|
agent: {
|
51
70
|
name: "#{self.class.name}",
|
52
71
|
thread: "#{Thread.current.object_id}",
|
53
|
-
jid:
|
72
|
+
jid: jid,
|
54
73
|
},
|
55
74
|
domain: @site.try(:domain) || @domain,
|
56
75
|
complete: false,
|
@@ -58,7 +77,7 @@ module Bellbro
|
|
58
77
|
data: {}
|
59
78
|
}
|
60
79
|
|
61
|
-
|
80
|
+
@log_record_schema.each do |k, v|
|
62
81
|
if v == Integer
|
63
82
|
@record[:data][k] = 0
|
64
83
|
else
|
@@ -74,7 +93,7 @@ module Bellbro
|
|
74
93
|
def validate(attrs)
|
75
94
|
attrs.each do |attr, value|
|
76
95
|
raise "Invalid attribute #{attr}" unless log_record_attributes.include?(attr)
|
77
|
-
raise "Invalid type for #{attr}" unless [value.class, value.class.superclass].include?(
|
96
|
+
raise "Invalid type for #{attr}" unless [value.class, value.class.superclass].include?(@log_record_schema[attr])
|
78
97
|
end
|
79
98
|
end
|
80
99
|
|
data/lib/bellbro/version.rb
CHANGED
data/lib/bellbro/worker.rb
CHANGED
data/lib/bellbro.rb
CHANGED
@@ -1,7 +1,28 @@
|
|
1
1
|
require "bellbro/version"
|
2
|
+
require 'connection_pool'
|
3
|
+
require 'active_support/all'
|
4
|
+
require 'redis'
|
5
|
+
require 'yaml'
|
6
|
+
require 'digest'
|
7
|
+
require 'sidekiq'
|
8
|
+
require 'airbrake'
|
2
9
|
|
3
10
|
module Bellbro
|
4
11
|
def self.logger
|
5
12
|
Bellbro::Settings.logger
|
6
13
|
end
|
7
14
|
end
|
15
|
+
|
16
|
+
%w(
|
17
|
+
bellbro/settings.rb
|
18
|
+
bellbro/retryable.rb
|
19
|
+
bellbro/ringable.rb
|
20
|
+
bellbro/trackable.rb
|
21
|
+
bellbro/redis_pool.rb
|
22
|
+
bellbro/sidekiq_utils.rb
|
23
|
+
bellbro/bell.rb
|
24
|
+
bellbro/service.rb
|
25
|
+
bellbro/worker.rb
|
26
|
+
).each do |path|
|
27
|
+
require File.join(File.dirname(__FILE__),path)
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'active_support'
|
4
|
+
require 'bellbro'
|
5
|
+
require 'rspec'
|
6
|
+
require 'yell'
|
7
|
+
|
8
|
+
Dir[File.join(File.dirname(__FILE__),'../spec/support/**/*.rb')].each { |f| require f }
|
9
|
+
#Dir[File.join(File.dirname(__FILE__),'../spec/factories/**/*.rb')].each { |f| require f }
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.order = 'random'
|
13
|
+
#config.include FactoryGirl::Syntax::Methods
|
14
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sidekiq/testing'
|
3
|
+
Sidekiq::Testing.fake!
|
4
|
+
|
5
|
+
class TrackWorker < Bellbro::Worker
|
6
|
+
track_with_schema(
|
7
|
+
links_created: Integer,
|
8
|
+
listings_updated: Integer,
|
9
|
+
errors: Array,
|
10
|
+
status: String
|
11
|
+
)
|
12
|
+
|
13
|
+
def initialize(domain)
|
14
|
+
@domain = domain
|
15
|
+
end
|
16
|
+
|
17
|
+
def jid
|
18
|
+
"abc123"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe Bellbro::Trackable do
|
23
|
+
before :each do
|
24
|
+
@worker = TrackWorker.new('www.retailer.com')
|
25
|
+
Sidekiq::Worker.clear_all
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#track" do
|
29
|
+
it "should properly initialize the log record" do
|
30
|
+
@worker.track
|
31
|
+
expect(@worker.record[:host]).to eq(Socket.gethostname)
|
32
|
+
expect(@worker.record[:agent][:name]).to eq('TrackWorker')
|
33
|
+
expect(@worker.record[:agent][:jid]).to eq('abc123')
|
34
|
+
expect(@worker.record[:data][:links_created]).to eq(0)
|
35
|
+
expect(@worker.record[:data][:listings_updated]).to eq(0)
|
36
|
+
expect(@worker.record[:data][:errors]).to be_empty
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#record_set" do
|
41
|
+
it "sets a record" do
|
42
|
+
@worker.track
|
43
|
+
@worker.record_set(:status, "Update")
|
44
|
+
expect(@worker.record[:data][:status]).to eq("Update")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#record_incr" do
|
49
|
+
it "increments an integer record" do
|
50
|
+
@worker.track
|
51
|
+
5.times { @worker.record_incr(:links_created) }
|
52
|
+
expect(@worker.record[:data][:links_created]).to eq(5)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#status_update" do
|
57
|
+
it "writes to the standard logger to update status" do
|
58
|
+
@worker.track(write_interval: 1)
|
59
|
+
@worker.record_incr(:links_created)
|
60
|
+
expect(@worker).to receive(:write_log)
|
61
|
+
@worker.status_update
|
62
|
+
end
|
63
|
+
|
64
|
+
it "does not reset the record data after each status update" do
|
65
|
+
@worker.track(write_interval: 1)
|
66
|
+
5.times { @worker.record_incr(:links_created) }
|
67
|
+
expect(@worker.record[:data][:links_created]).to eq(5)
|
68
|
+
2.times { @worker.status_update }
|
69
|
+
expect(@worker.record[:data][:links_created]).to eq(5)
|
70
|
+
expect(@worker.record.to_param).to include('links_created%5D=5')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#validate" do
|
75
|
+
it "raises an error if an attribute is invalid" do
|
76
|
+
@worker.track
|
77
|
+
expect {
|
78
|
+
@worker.send(:validate, {foo: "bar"})
|
79
|
+
}.to raise_error(RuntimeError)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "raises an error if a value is invalid" do
|
83
|
+
@worker.track
|
84
|
+
expect {
|
85
|
+
@worker.send(:validate, {links_created: "bar"})
|
86
|
+
}.to raise_error(RuntimeError)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/spec/worker_spec.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sidekiq/testing'
|
3
|
+
|
4
|
+
describe Bellbro::Worker do
|
5
|
+
before :each do
|
6
|
+
Sidekiq::Testing.disable!
|
7
|
+
clear_sidekiq
|
8
|
+
end
|
9
|
+
|
10
|
+
after :each do
|
11
|
+
clear_sidekiq
|
12
|
+
Sidekiq::Testing.fake!
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#jobs_in_flight_with_domain" do
|
16
|
+
it "counts the number of enqueued jobs and active workers for a domain" do
|
17
|
+
domain = "www.retailer.com"
|
18
|
+
5.times { Bellbro::Worker.perform_async(domain: domain) }
|
19
|
+
expect(Bellbro::Worker.jobs_in_flight_with_domain(domain).count).to eq(5)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bellbro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Stokes
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2015-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - '>='
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
name: activesupport
|
20
|
+
prerelease: false
|
21
|
+
type: :runtime
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
requirement: !ruby/object:Gem::Requirement
|
15
29
|
requirements:
|
@@ -80,6 +94,34 @@ dependencies:
|
|
80
94
|
- - ~>
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
name: rspec
|
104
|
+
prerelease: false
|
105
|
+
type: :development
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
name: yell
|
118
|
+
prerelease: false
|
119
|
+
type: :development
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
83
125
|
description:
|
84
126
|
email:
|
85
127
|
- jon@jonstokes.com
|
@@ -97,13 +139,18 @@ files:
|
|
97
139
|
- lib/bellbro/bell.rb
|
98
140
|
- lib/bellbro/redis_pool.rb
|
99
141
|
- lib/bellbro/retryable.rb
|
100
|
-
- lib/bellbro/
|
142
|
+
- lib/bellbro/ringable.rb
|
101
143
|
- lib/bellbro/service.rb
|
102
144
|
- lib/bellbro/settings.rb
|
103
145
|
- lib/bellbro/sidekiq_utils.rb
|
104
146
|
- lib/bellbro/trackable.rb
|
105
147
|
- lib/bellbro/version.rb
|
106
148
|
- lib/bellbro/worker.rb
|
149
|
+
- spec/spec_helper.rb
|
150
|
+
- spec/support/bellbro.rb
|
151
|
+
- spec/support/sidekiq.rb
|
152
|
+
- spec/trackable_spec.rb
|
153
|
+
- spec/worker_spec.rb
|
107
154
|
homepage: ''
|
108
155
|
licenses:
|
109
156
|
- MIT
|
@@ -128,4 +175,9 @@ rubygems_version: 2.4.5
|
|
128
175
|
signing_key:
|
129
176
|
specification_version: 4
|
130
177
|
summary: Helps with sidekiq.
|
131
|
-
test_files:
|
178
|
+
test_files:
|
179
|
+
- spec/spec_helper.rb
|
180
|
+
- spec/support/bellbro.rb
|
181
|
+
- spec/support/sidekiq.rb
|
182
|
+
- spec/trackable_spec.rb
|
183
|
+
- spec/worker_spec.rb
|