barnyard_harvester 0.0.10 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- data/barnyard_harvester.gemspec +1 -0
- data/lib/barnyard_harvester/generic_queue.rb +72 -0
- data/lib/barnyard_harvester/mongodb.rb +9 -9
- data/lib/barnyard_harvester/{rabbitmq_queue.rb → queue.rb} +11 -14
- data/lib/barnyard_harvester/version.rb +1 -1
- data/lib/barnyard_harvester.rb +14 -10
- data/spec/hash_spec.rb +1 -1
- data/spec/loader_spec.rb +15 -10
- data/spec/{mongo_spec.rb → mongo_rabbitmq_spec.rb} +10 -19
- data/spec/queue_rabbitmq_spec.rb +37 -0
- data/spec/queue_sqs_spec.rb +39 -0
- data/spec/{redis_spec.rb → redis_hash_spec.rb} +1 -1
- data/spec/redis_rabbitmq_spec.rb +0 -7
- metadata +41 -27
- data/lib/barnyard_harvester/resque_queue.rb +0 -101
- data/lib/barnyard_harvester/sqs_queue.rb +0 -112
data/barnyard_harvester.gemspec
CHANGED
@@ -0,0 +1,72 @@
|
|
1
|
+
module BarnyardHarvester
|
2
|
+
|
3
|
+
class GenericQueue
|
4
|
+
|
5
|
+
def initialize(args)
|
6
|
+
@queueing = args.fetch(:queueing) { raise "You must provide :queueing" }
|
7
|
+
|
8
|
+
case @queueing
|
9
|
+
when :sqs
|
10
|
+
require "aws-sdk"
|
11
|
+
@sqs_settings = args.fetch(:sqs_settings) { raise "You must provide :sqs_settings" }
|
12
|
+
@sqs = AWS::SQS.new(@sqs_settings)
|
13
|
+
when :rabbitmq
|
14
|
+
require "bunny"
|
15
|
+
@rabbitmq_settings = args.fetch(:rabbitmq_settings) { raise "You must provide :rabbitmq_settings" }
|
16
|
+
@rabbitmq_settings[:logging] = true if @debug
|
17
|
+
@bunny = Bunny.new(@rabbitmq_settings)
|
18
|
+
@bunny.start
|
19
|
+
when :hash
|
20
|
+
@queues = Hash.new
|
21
|
+
else
|
22
|
+
raise "Unknown queueing method. #{@queuing}"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def push(queue_name, message)
|
28
|
+
case @queueing
|
29
|
+
when :sqs
|
30
|
+
queue = @sqs.queues.create(queue_name)
|
31
|
+
queue.send_message(message)
|
32
|
+
when :rabbitmq
|
33
|
+
@bunny.queue(queue_name).publish(message)
|
34
|
+
when :hash
|
35
|
+
@queues[queue_name] = Array.new unless @queues.has_key?(queue_name)
|
36
|
+
@queues[queue_name] << message
|
37
|
+
File.open("#{queue_name}.yml", "w") { |file| file.puts(@queues[queue_name].to_yaml) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def pop(queue_name)
|
42
|
+
case @queueing
|
43
|
+
when :sqs
|
44
|
+
msg = @sqs.queues.create(queue_name).receive_message
|
45
|
+
unless msg.nil?
|
46
|
+
msg.delete
|
47
|
+
msg.body
|
48
|
+
else
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
when :rabbitmq
|
53
|
+
msg = @bunny.queue(queue_name).pop[:payload]
|
54
|
+
if msg == :queue_empty
|
55
|
+
return nil
|
56
|
+
else
|
57
|
+
msg
|
58
|
+
end
|
59
|
+
when :hash
|
60
|
+
msg = @queue.pop
|
61
|
+
File.open("#{queue_name}.yml", "w") { |file| file.puts(@queues[queue_name].to_yaml) }
|
62
|
+
msg
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def empty(queue_name)
|
67
|
+
while pop(queue_name)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -42,15 +42,15 @@ module BarnyardHarvester
|
|
42
42
|
@collection
|
43
43
|
end
|
44
44
|
|
45
|
-
def log_run(harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
45
|
+
#def log_run(harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
|
46
|
+
#
|
47
|
+
# begin
|
48
|
+
# Resque.enqueue(HarvesterLogs, Time.now, harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
|
49
|
+
# rescue Exception => e
|
50
|
+
# @log.fatal "#{self.class} Fail in Resque.enqueue of HarvesterLogs. #{e.backtrace}"
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
#end
|
54
54
|
|
55
55
|
def delete(primary_key)
|
56
56
|
check_key primary_key
|
@@ -1,6 +1,7 @@
|
|
1
|
-
module BarnyardHarvester
|
2
1
|
|
3
|
-
|
2
|
+
require "barnyard_harvester/generic_queue"
|
3
|
+
|
4
|
+
module BarnyardHarvester
|
4
5
|
|
5
6
|
QUEUE_FARMER = "barnyard-farmer"
|
6
7
|
QUEUE_HARVESTER = "barnyard-harvests"
|
@@ -9,10 +10,10 @@ module BarnyardHarvester
|
|
9
10
|
|
10
11
|
class Queue
|
11
12
|
|
12
|
-
def enqueue(queue,
|
13
|
+
def enqueue(queue, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value)
|
13
14
|
|
14
15
|
payload = Hash.new
|
15
|
-
payload[:queued_at] =
|
16
|
+
payload[:queued_at] = Time.now
|
16
17
|
payload[:harvester_uuid] = harvester_uuid
|
17
18
|
payload[:change_uuid] = change_uuid
|
18
19
|
payload[:crop_number] = crop_number
|
@@ -21,10 +22,10 @@ module BarnyardHarvester
|
|
21
22
|
payload[:value] = value
|
22
23
|
payload[:old_value] = old_value
|
23
24
|
|
24
|
-
json_payload = payload
|
25
|
+
json_payload = payload.to_json
|
25
26
|
|
26
|
-
@
|
27
|
-
@
|
27
|
+
@q.push(queue,json_payload)
|
28
|
+
@q.push(QUEUE_CHANGE,json_payload)
|
28
29
|
|
29
30
|
end
|
30
31
|
|
@@ -41,7 +42,7 @@ module BarnyardHarvester
|
|
41
42
|
payload[:add_count] = add_count
|
42
43
|
payload[:delete_count] = delete_count
|
43
44
|
|
44
|
-
@
|
45
|
+
@q.push(QUEUE_HARVESTER,payload.to_json)
|
45
46
|
|
46
47
|
end
|
47
48
|
|
@@ -50,19 +51,15 @@ module BarnyardHarvester
|
|
50
51
|
@debug = args.fetch(:debug) { false }
|
51
52
|
@log = args.fetch(:logger) { Logger.new(STDOUT) }
|
52
53
|
@crop_number = args.fetch(:crop_number) { raise "You must provide :crop_number" }
|
53
|
-
@rabbitmq_settings = args.fetch(:rabbitmq_settings) { raise "You must provide :rabbitmq_settings" }
|
54
|
-
|
55
|
-
@bunny = Bunny.new(@rabbitmq_settings)
|
56
|
-
@bunny.start
|
57
54
|
|
58
|
-
@
|
55
|
+
@q = BarnyardHarvester::GenericQueue.new(args)
|
59
56
|
|
60
57
|
end
|
61
58
|
|
62
59
|
def push(harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value=Hash.new)
|
63
60
|
check_key primary_key
|
64
61
|
|
65
|
-
enqueue(QUEUE_FARMER,
|
62
|
+
enqueue(QUEUE_FARMER, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value.to_json, old_value.to_json)
|
66
63
|
|
67
64
|
message = "RabbitQueue: #{QUEUE_FARMER}, Now: #{DateTime.now}, Harvester:#{harvester_uuid}, Change:#{change_uuid} crop_number: #{crop_number}, key: #{primary_key}, transaction_type: #{transaction_type})"
|
68
65
|
|
data/lib/barnyard_harvester.rb
CHANGED
@@ -2,6 +2,7 @@ require "uuid"
|
|
2
2
|
require "logger"
|
3
3
|
|
4
4
|
require "barnyard_harvester/version"
|
5
|
+
require "barnyard_harvester/queue"
|
5
6
|
|
6
7
|
module BarnyardHarvester
|
7
8
|
|
@@ -20,21 +21,24 @@ module BarnyardHarvester
|
|
20
21
|
|
21
22
|
def initialize(args)
|
22
23
|
|
24
|
+
@queueing = args.fetch(:queueing) { raise "You must provide :queueing" }
|
23
25
|
@crop_number = args.fetch(:crop_number) { raise "You must provide :crop_number" }
|
24
26
|
@redis_settings = args.fetch(:redis_settings) { DEFAULT_REDIS_SETTINGS }
|
25
27
|
@debug = args.fetch(:debug) { false }
|
26
28
|
@log = args.fetch(:logger) { Logger.new(STDOUT) }
|
27
29
|
|
28
|
-
@
|
30
|
+
@q = BarnyardHarvester::Queue.new(args)
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
#@queueing = args[:queueing]
|
33
|
+
#
|
34
|
+
#case @queueing
|
35
|
+
# when :rabbitmq
|
36
|
+
# @rabbitmq_settings = args.fetch(:rabbitmq_settings) { raise "You must provide :rabbitmq_settings" }
|
37
|
+
# when :sqs
|
38
|
+
# @sqs_settings = args.fetch(:sqs_settings) { raise "You must provide :sqs_settings" }
|
39
|
+
# else
|
40
|
+
# @queueing = :resque
|
41
|
+
#end
|
38
42
|
|
39
43
|
@backend = args.fetch(:backend) { :redis }
|
40
44
|
|
@@ -43,7 +47,7 @@ module BarnyardHarvester
|
|
43
47
|
end
|
44
48
|
|
45
49
|
require "barnyard_harvester/#{@backend.to_s}_helper" if File.exist? "barnyard_harvester/#{@backend.to_s}_helper"
|
46
|
-
require "barnyard_harvester/#{@queueing.to_s}_queue"
|
50
|
+
# require "barnyard_harvester/#{@queueing.to_s}_queue"
|
47
51
|
require "barnyard_harvester/#{@backend.to_s}"
|
48
52
|
|
49
53
|
# YAML::ENGINE.yamler = 'syck'
|
data/spec/hash_spec.rb
CHANGED
@@ -22,7 +22,7 @@ describe BarnyardHarvester do
|
|
22
22
|
my_logger = Logger.new(STDOUT)
|
23
23
|
my_logger.level = Logger::INFO
|
24
24
|
|
25
|
-
h = BarnyardHarvester::Sync.new(:queueing => :
|
25
|
+
h = BarnyardHarvester::Sync.new(:queueing => :hash,
|
26
26
|
:backend => backend,
|
27
27
|
:debug => false,
|
28
28
|
:crop_number => 1,
|
data/spec/loader_spec.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
require "barnyard_harvester"
|
2
2
|
|
3
|
+
RABBITMQ_SETTINGS = {
|
4
|
+
:host => "localhost"
|
5
|
+
# :port => 6163
|
6
|
+
}
|
7
|
+
|
3
8
|
describe BarnyardHarvester do
|
4
9
|
|
5
10
|
it "no parameters should raise error" do
|
@@ -7,31 +12,31 @@ describe BarnyardHarvester do
|
|
7
12
|
end
|
8
13
|
|
9
14
|
it "passing only :crop_number => 1 should return BarnyardHarvester::Sync object" do
|
10
|
-
BarnyardHarvester::Sync.new(crop_number: 1).class.should eq(BarnyardHarvester::Sync)
|
15
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1).class.should eq(BarnyardHarvester::Sync)
|
11
16
|
end
|
12
17
|
|
13
18
|
it "default backend should be :redis" do
|
14
|
-
BarnyardHarvester::Sync.new(crop_number: 1).backend.should eq(:redis)
|
19
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1).backend.should eq(:redis)
|
15
20
|
end
|
16
21
|
|
17
22
|
it "passing backend :hash should be :hash" do
|
18
|
-
BarnyardHarvester::Sync.new(crop_number: 1, backend: :hash).backend.should eq(:hash)
|
23
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1, backend: :hash).backend.should eq(:hash)
|
19
24
|
end
|
20
25
|
|
21
26
|
it "passing bogus backend should raise an error" do
|
22
|
-
lambda{BarnyardHarvester::Sync.new(crop_number: 1, backend: :foobar)}.should raise_error
|
27
|
+
lambda{BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1, backend: :foobar)}.should raise_error
|
23
28
|
end
|
24
29
|
|
25
30
|
it "crop_number should be 1001" do
|
26
|
-
BarnyardHarvester::Sync.new(crop_number: 1001).crop_number.should eq(1001)
|
31
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1001).crop_number.should eq(1001)
|
27
32
|
end
|
28
33
|
|
29
34
|
it "all counters should be zero" do
|
30
|
-
BarnyardHarvester::Sync.new(crop_number: 1).change_count.should eq(0)
|
31
|
-
BarnyardHarvester::Sync.new(crop_number: 1).add_count.should eq(0)
|
32
|
-
BarnyardHarvester::Sync.new(crop_number: 1).delete_count.should eq(0)
|
33
|
-
BarnyardHarvester::Sync.new(crop_number: 1).source_count.should eq(0)
|
34
|
-
BarnyardHarvester::Sync.new(crop_number: 1).cache_count.should eq(0)
|
35
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1).change_count.should eq(0)
|
36
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1).add_count.should eq(0)
|
37
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1).delete_count.should eq(0)
|
38
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1).source_count.should eq(0)
|
39
|
+
BarnyardHarvester::Sync.new(rabbitmq_settings: RABBITMQ_SETTINGS, queueing: :rabbitmq, crop_number: 1).cache_count.should eq(0)
|
35
40
|
end
|
36
41
|
|
37
42
|
end
|
@@ -7,12 +7,6 @@ require "json"
|
|
7
7
|
|
8
8
|
CROP_NUMBER = 1
|
9
9
|
|
10
|
-
REDIS_SETTINGS = {
|
11
|
-
:host => "localhost",
|
12
|
-
:port => 6379,
|
13
|
-
:db => CROP_NUMBER
|
14
|
-
}
|
15
|
-
|
16
10
|
MONGODB_SETTINGS = {
|
17
11
|
:host => "localhost",
|
18
12
|
:collection => "test_collection",
|
@@ -21,14 +15,19 @@ MONGODB_SETTINGS = {
|
|
21
15
|
}
|
22
16
|
|
23
17
|
MONGODB_REPLICA_SET_SETTINGS = {
|
24
|
-
:host => ["
|
18
|
+
:host => ["mongo1:27017", "mongo2:27017", "mongo3:27017"],
|
25
19
|
:collection => "test_collection",
|
26
20
|
:db => "test_database",
|
27
|
-
:user => "
|
28
|
-
:password => "
|
21
|
+
:user => "username",
|
22
|
+
:password => "password",
|
29
23
|
:collection => "test_collection"
|
30
24
|
}
|
31
25
|
|
26
|
+
RABBITMQ_SETTINGS = {
|
27
|
+
:host => "localhost"
|
28
|
+
# :port => 6163
|
29
|
+
}
|
30
|
+
|
32
31
|
$mongo_settings = MONGODB_SETTINGS
|
33
32
|
|
34
33
|
describe BarnyardHarvester do
|
@@ -41,11 +40,11 @@ describe BarnyardHarvester do
|
|
41
40
|
my_logger.level = Logger::INFO
|
42
41
|
|
43
42
|
h = BarnyardHarvester::Sync.new(:backend => backend,
|
44
|
-
:queueing => :
|
43
|
+
:queueing => :rabbitmq,
|
45
44
|
:debug => false,
|
46
45
|
:mongodb_settings => $mongo_settings,
|
47
46
|
:crop_number => CROP_NUMBER,
|
48
|
-
:
|
47
|
+
:rabbitmq_settings => RABBITMQ_SETTINGS,
|
49
48
|
:logger => my_logger)
|
50
49
|
|
51
50
|
h.run do
|
@@ -125,8 +124,6 @@ describe BarnyardHarvester do
|
|
125
124
|
h.source_count.should eq(data.count)
|
126
125
|
h.cache_count.should eq(data.count)
|
127
126
|
|
128
|
-
h.my_barn.log_run("#{file}-#{Random.rand(100)}", @crop_number, Time.now, Time.now, h.source_count, h.change_count, h.add_count, h.delete_count)
|
129
|
-
|
130
127
|
end
|
131
128
|
|
132
129
|
it "test change one record" do
|
@@ -143,8 +140,6 @@ describe BarnyardHarvester do
|
|
143
140
|
h.source_count.should eq(data.count)
|
144
141
|
h.cache_count.should eq(data.count)
|
145
142
|
|
146
|
-
h.my_barn.log_run("#{file}-#{Random.rand(100)}", @crop_number, Time.now, Time.now, h.source_count, h.change_count, h.add_count, h.delete_count)
|
147
|
-
|
148
143
|
end
|
149
144
|
|
150
145
|
it "test delete one record" do
|
@@ -161,8 +156,6 @@ describe BarnyardHarvester do
|
|
161
156
|
h.source_count.should eq(data.count)
|
162
157
|
h.cache_count.should eq(data.count + 1)
|
163
158
|
|
164
|
-
h.my_barn.log_run("#{file}-#{Random.rand(100)}", @crop_number, Time.now, Time.now, h.source_count, h.change_count, h.add_count, h.delete_count)
|
165
|
-
|
166
159
|
end
|
167
160
|
|
168
161
|
it "test delete all records and add one" do
|
@@ -181,8 +174,6 @@ describe BarnyardHarvester do
|
|
181
174
|
h.source_count.should eq(1)
|
182
175
|
h.cache_count.should eq(init_data.count + 1)
|
183
176
|
|
184
|
-
h.my_barn.log_run("#{file}-#{Random.rand(100)}", @crop_number, Time.now, Time.now, h.source_count, h.change_count, h.add_count, h.delete_count)
|
185
|
-
|
186
177
|
end
|
187
178
|
|
188
179
|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "barnyard_harvester"
|
2
|
+
|
3
|
+
RABBITMQ_SETTINGS = {
|
4
|
+
:host => "localhost"
|
5
|
+
# :port => 6163
|
6
|
+
}
|
7
|
+
|
8
|
+
QUEUE_NAME = "test001"
|
9
|
+
|
10
|
+
describe "Test RabbitMQ" do
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
|
14
|
+
@q = BarnyardHarvester::GenericQueue.new(queueing: :rabbitmq, rabbitmq_settings: RABBITMQ_SETTINGS)
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
it "Push 10 items on the queue" do
|
19
|
+
|
20
|
+
@q.empty(QUEUE_NAME)
|
21
|
+
|
22
|
+
10.times do |i|
|
23
|
+
@q.push(QUEUE_NAME, "My Message #{i}")
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
it "Pop 10 items" do
|
29
|
+
|
30
|
+
10.times do |i|
|
31
|
+
msg = @q.pop(QUEUE_NAME)
|
32
|
+
puts msg
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "barnyard_harvester"
|
2
|
+
|
3
|
+
SQS_SETTINGS = {
|
4
|
+
:sqs_endpoint => "sqs.us-west-1.amazonaws.com",
|
5
|
+
:access_key_id => ENV["AWS_ACCESS_KEY_ID"],
|
6
|
+
:secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
|
7
|
+
}
|
8
|
+
|
9
|
+
QUEUE_NAME = "test001"
|
10
|
+
|
11
|
+
describe "Test SQS" do
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
|
15
|
+
@q = BarnyardHarvester::GenericQueue.new(queueing: :sqs, sqs_settings: SQS_SETTINGS)
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
it "Push 10 items on the queue" do
|
20
|
+
|
21
|
+
@q.empty(QUEUE_NAME)
|
22
|
+
|
23
|
+
10.times do |i|
|
24
|
+
@q.push(QUEUE_NAME, "My Message #{i}")
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
it "Pop 10 items" do
|
30
|
+
|
31
|
+
10.times do |i|
|
32
|
+
msg = @q.pop(QUEUE_NAME)
|
33
|
+
puts msg
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
@@ -21,7 +21,7 @@ describe BarnyardHarvester do
|
|
21
21
|
my_logger = Logger.new(STDOUT)
|
22
22
|
my_logger.level = Logger::INFO
|
23
23
|
|
24
|
-
h = BarnyardHarvester::Sync.new(:backend => backend, :debug => false, :crop_number => CROP_NUMBER, :redis_settings => REDIS_SETTINGS, :logger => my_logger)
|
24
|
+
h = BarnyardHarvester::Sync.new(:queueing => :hash, :backend => backend, :debug => false, :crop_number => CROP_NUMBER, :redis_settings => REDIS_SETTINGS, :logger => my_logger)
|
25
25
|
|
26
26
|
h.run do
|
27
27
|
data.each do |primary_key, value|
|
data/spec/redis_rabbitmq_spec.rb
CHANGED
@@ -93,8 +93,6 @@ describe BarnyardHarvester do
|
|
93
93
|
h.source_count.should eq(data.count)
|
94
94
|
h.cache_count.should eq(data.count)
|
95
95
|
|
96
|
-
# h.log_run("#{file}-#{Random.rand(100)}", @crop_number, Time.now, Time.now, h.source_count, h.change_count, h.add_count, h.delete_count)
|
97
|
-
|
98
96
|
end
|
99
97
|
|
100
98
|
it "test change one record" do
|
@@ -111,8 +109,6 @@ describe BarnyardHarvester do
|
|
111
109
|
h.source_count.should eq(data.count)
|
112
110
|
h.cache_count.should eq(data.count)
|
113
111
|
|
114
|
-
# h.my_barn.log_run("#{file}-#{Random.rand(100)}", @crop_number, Time.now, Time.now, h.source_count, h.change_count, h.add_count, h.delete_count)
|
115
|
-
|
116
112
|
end
|
117
113
|
|
118
114
|
it "test delete one record" do
|
@@ -150,13 +146,10 @@ describe BarnyardHarvester do
|
|
150
146
|
h.source_count.should eq(1)
|
151
147
|
h.cache_count.should eq(init_data.count + 1)
|
152
148
|
|
153
|
-
# h.my_barn.log_run("#{file}-#{Random.rand(100)}", @crop_number, Time.now, Time.now, h.source_count, h.change_count, h.add_count, h.delete_count)
|
154
|
-
|
155
149
|
end
|
156
150
|
|
157
151
|
|
158
152
|
after(:each) do
|
159
153
|
end
|
160
154
|
|
161
|
-
|
162
155
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: barnyard_harvester
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70330836174420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70330836174420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: resque
|
27
|
-
requirement: &
|
27
|
+
requirement: &70330836173200 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70330836173200
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: crack
|
38
|
-
requirement: &
|
38
|
+
requirement: &70330836169660 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70330836169660
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: json
|
49
|
-
requirement: &
|
49
|
+
requirement: &70330836167600 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70330836167600
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: uuid
|
60
|
-
requirement: &
|
60
|
+
requirement: &70330836182240 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70330836182240
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: bson_ext
|
71
|
-
requirement: &
|
71
|
+
requirement: &70330836179360 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - =
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 1.6.0
|
77
77
|
type: :runtime
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70330836179360
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: mongo
|
82
|
-
requirement: &
|
82
|
+
requirement: &70330836189880 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - =
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: 1.6.0
|
88
88
|
type: :runtime
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70330836189880
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: amqp
|
93
|
-
requirement: &
|
93
|
+
requirement: &70330836188800 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,21 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :runtime
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70330836188800
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: bunny
|
104
|
+
requirement: &70330836187660 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :runtime
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: *70330836187660
|
102
113
|
- !ruby/object:Gem::Dependency
|
103
114
|
name: aws-sdk
|
104
|
-
requirement: &
|
115
|
+
requirement: &70330836186540 !ruby/object:Gem::Requirement
|
105
116
|
none: false
|
106
117
|
requirements:
|
107
118
|
- - ! '>='
|
@@ -109,7 +120,7 @@ dependencies:
|
|
109
120
|
version: '0'
|
110
121
|
type: :runtime
|
111
122
|
prerelease: false
|
112
|
-
version_requirements: *
|
123
|
+
version_requirements: *70330836186540
|
113
124
|
description: Performs harvests on data sources and detects adds, changes and deletes.
|
114
125
|
email:
|
115
126
|
- supercoder@gmail.com
|
@@ -128,14 +139,13 @@ files:
|
|
128
139
|
- lib/.DS_Store
|
129
140
|
- lib/barnyard_harvester.rb
|
130
141
|
- lib/barnyard_harvester/dynamodb.rb
|
142
|
+
- lib/barnyard_harvester/generic_queue.rb
|
131
143
|
- lib/barnyard_harvester/hash.rb
|
132
144
|
- lib/barnyard_harvester/hash_queue.rb
|
133
145
|
- lib/barnyard_harvester/mongodb.rb
|
134
146
|
- lib/barnyard_harvester/mongodb_helper.rb
|
135
|
-
- lib/barnyard_harvester/
|
147
|
+
- lib/barnyard_harvester/queue.rb
|
136
148
|
- lib/barnyard_harvester/redis.rb
|
137
|
-
- lib/barnyard_harvester/resque_queue.rb
|
138
|
-
- lib/barnyard_harvester/sqs_queue.rb
|
139
149
|
- lib/barnyard_harvester/version.rb
|
140
150
|
- lib/test.yml
|
141
151
|
- spec/dynamodb_sqs_spec.rb
|
@@ -147,9 +157,11 @@ files:
|
|
147
157
|
- spec/hash_spec.rb
|
148
158
|
- spec/loader_spec.rb
|
149
159
|
- spec/mongo_helper_spec.rb
|
150
|
-
- spec/
|
160
|
+
- spec/mongo_rabbitmq_spec.rb
|
161
|
+
- spec/queue_rabbitmq_spec.rb
|
162
|
+
- spec/queue_sqs_spec.rb
|
163
|
+
- spec/redis_hash_spec.rb
|
151
164
|
- spec/redis_rabbitmq_spec.rb
|
152
|
-
- spec/redis_spec.rb
|
153
165
|
- spec/redis_sqs_spec.rb
|
154
166
|
- spec/spec_helper.rb
|
155
167
|
homepage: https://github.com/jongillies/barnyard/tree/master/barnyard_harvester
|
@@ -186,8 +198,10 @@ test_files:
|
|
186
198
|
- spec/hash_spec.rb
|
187
199
|
- spec/loader_spec.rb
|
188
200
|
- spec/mongo_helper_spec.rb
|
189
|
-
- spec/
|
201
|
+
- spec/mongo_rabbitmq_spec.rb
|
202
|
+
- spec/queue_rabbitmq_spec.rb
|
203
|
+
- spec/queue_sqs_spec.rb
|
204
|
+
- spec/redis_hash_spec.rb
|
190
205
|
- spec/redis_rabbitmq_spec.rb
|
191
|
-
- spec/redis_spec.rb
|
192
206
|
- spec/redis_sqs_spec.rb
|
193
207
|
- spec/spec_helper.rb
|
@@ -1,101 +0,0 @@
|
|
1
|
-
module BarnyardHarvester
|
2
|
-
|
3
|
-
class ChangeLogs
|
4
|
-
@queue = :logs_change
|
5
|
-
end
|
6
|
-
|
7
|
-
class HarvesterLogs
|
8
|
-
@queue = :logs_harvester
|
9
|
-
end
|
10
|
-
|
11
|
-
class DeliveryLogs
|
12
|
-
@queue = :logs_delivery
|
13
|
-
end
|
14
|
-
|
15
|
-
class TransactionLogs
|
16
|
-
@queue = :logs_transaction
|
17
|
-
end
|
18
|
-
class Queue
|
19
|
-
|
20
|
-
class Enqueue
|
21
|
-
def initialize(queue, queued_at, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value)
|
22
|
-
Resque.enqueue(queue, queued_at, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value)
|
23
|
-
Resque.enqueue(ChangeLogs, queued_at, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def log_run(harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
|
28
|
-
|
29
|
-
Resque.enqueue(HarvesterLogs, harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
def initialize(args)
|
34
|
-
|
35
|
-
@debug = args.fetch(:debug) { false }
|
36
|
-
@log = args.fetch(:logger) { Logger.new(STDOUT) }
|
37
|
-
@crop_number = args.fetch(:crop_number) { raise "You must provide :crop_number" }
|
38
|
-
|
39
|
-
resque_class_name = "Distribute"
|
40
|
-
|
41
|
-
# If the class does not exist, the rescue block will create it.
|
42
|
-
# The Class Queue is inherited by the AddQueue, ChangeQueue and DeleteQueue, but
|
43
|
-
# we only want to create one "resque" queue for this instantiation
|
44
|
-
begin
|
45
|
-
Object.const_get(resque_class_name)
|
46
|
-
rescue
|
47
|
-
# Set the queue name to this apol_harvester's id prefixed with a Q_
|
48
|
-
#Object.const_set(resque_class_name, Class.new { @queue = "Q_#{args[:crop_number]}"})
|
49
|
-
Object.const_set(resque_class_name, Class.new { @queue = "Farmer" })
|
50
|
-
end
|
51
|
-
|
52
|
-
@resque_queue = Object.const_get(resque_class_name)
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
|
57
|
-
def push(harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value=Hash.new)
|
58
|
-
check_key primary_key
|
59
|
-
|
60
|
-
Enqueue.new(@resque_queue, DateTime.now, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value.to_json, old_value.to_json)
|
61
|
-
|
62
|
-
message = "RedisQueue: #{@resque_queue}, Now: #{DateTime.now}, Harvester:#{harvester_uuid}, Change:#{change_uuid} crop_number: #{crop_number}, key: #{primary_key}, transaction_type: #{transaction_type})"
|
63
|
-
|
64
|
-
if @log.level == Logger::DEBUG
|
65
|
-
message += ", value: #{value.to_json}, old_value: #{old_value.to_json}"
|
66
|
-
@log.debug message
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# Flush any data if needed.
|
71
|
-
#
|
72
|
-
def flush
|
73
|
-
end
|
74
|
-
|
75
|
-
private
|
76
|
-
|
77
|
-
# Raise an exception here if the key must conform to a specific format
|
78
|
-
#
|
79
|
-
def check_key(primary_key)
|
80
|
-
# Example: raise "key must be a string object" unless key.is_a? String
|
81
|
-
primary_key
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
# AddQueue
|
87
|
-
#
|
88
|
-
class AddQueue < Queue
|
89
|
-
end
|
90
|
-
|
91
|
-
# ChangeQueue
|
92
|
-
#
|
93
|
-
class ChangeQueue < Queue
|
94
|
-
end
|
95
|
-
|
96
|
-
# DeleteQueue
|
97
|
-
#
|
98
|
-
class DeleteQueue < Queue
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
@@ -1,112 +0,0 @@
|
|
1
|
-
module BarnyardHarvester
|
2
|
-
|
3
|
-
require "aws-sdk"
|
4
|
-
|
5
|
-
QUEUE_FARMER = "barnyard-farmer"
|
6
|
-
QUEUE_HARVESTER = "barnyard-harvests"
|
7
|
-
QUEUE_TRANSACTION = "barnyard-transactions"
|
8
|
-
QUEUE_CHANGE = "barnyard-changes"
|
9
|
-
|
10
|
-
class Queue
|
11
|
-
|
12
|
-
def enqueue(queued_at, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value)
|
13
|
-
|
14
|
-
payload = Hash.new
|
15
|
-
payload[:queued_at] = queued_at
|
16
|
-
payload[:harvester_uuid] = harvester_uuid
|
17
|
-
payload[:change_uuid] = change_uuid
|
18
|
-
payload[:crop_number] = crop_number
|
19
|
-
payload[:primary_key] = primary_key
|
20
|
-
payload[:transaction_type] = transaction_type
|
21
|
-
payload[:value] = value
|
22
|
-
payload[:old_value] = old_value
|
23
|
-
|
24
|
-
json_payload = payload.to_json
|
25
|
-
|
26
|
-
@log.debug "Starting send_message to farmer..."
|
27
|
-
@farmer_queue.send_message(json_payload)
|
28
|
-
@log.debug "Done send_message to farmer..."
|
29
|
-
|
30
|
-
@log.debug "Starting send_message to changes..."
|
31
|
-
@change_queue.send_message(json_payload)
|
32
|
-
@log.debug "Done send_message to changes..."
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
def log_run(harvester_uuid, crop_number, began_at, ended_at, source_count, change_count, add_count, delete_count)
|
37
|
-
|
38
|
-
payload = Hash.new
|
39
|
-
payload[:time] = Time.now
|
40
|
-
payload[:harvester_uuid] = harvester_uuid
|
41
|
-
payload[:crop_number] = crop_number
|
42
|
-
payload[:began_at] = began_at
|
43
|
-
payload[:ended_at] = ended_at
|
44
|
-
payload[:source_count] = source_count
|
45
|
-
payload[:change_count] = change_count
|
46
|
-
payload[:add_count] = add_count
|
47
|
-
payload[:delete_count] = delete_count
|
48
|
-
|
49
|
-
@harvester_queue.send_message(payload.to_json)
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def initialize(args)
|
54
|
-
|
55
|
-
@debug = args.fetch(:debug) { false }
|
56
|
-
@log = args.fetch(:logger) { Logger.new(STDOUT) }
|
57
|
-
@crop_number = args.fetch(:crop_number) { raise "You must provide :crop_number" }
|
58
|
-
@sqs_settings = args.fetch(:sqs_settings) { raise "You must provide :sqs_settings" }
|
59
|
-
|
60
|
-
@sqs = AWS::SQS.new(@sqs_settings)
|
61
|
-
|
62
|
-
@farmer_queue = @sqs.queues.create(QUEUE_FARMER)
|
63
|
-
@harvester_queue = @sqs.queues.create(QUEUE_HARVESTER)
|
64
|
-
@change_queue = @sqs.queues.create(QUEUE_CHANGE)
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
def push(harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value, old_value=Hash.new)
|
69
|
-
check_key primary_key
|
70
|
-
|
71
|
-
enqueue(DateTime.now, harvester_uuid, change_uuid, crop_number, primary_key, transaction_type, value.to_json, old_value.to_json)
|
72
|
-
|
73
|
-
message = "SQS: #{QUEUE_FARMER}, Now: #{DateTime.now}, Harvester:#{harvester_uuid}, Change:#{change_uuid} crop_number: #{crop_number}, key: #{primary_key}, transaction_type: #{transaction_type})"
|
74
|
-
|
75
|
-
if @log.level == Logger::DEBUG
|
76
|
-
message += ", value: #{value.to_json}, old_value: #{old_value.to_json}"
|
77
|
-
@log.debug message
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# Flush any data if needed.
|
82
|
-
#
|
83
|
-
def flush
|
84
|
-
end
|
85
|
-
|
86
|
-
private
|
87
|
-
|
88
|
-
# Raise an exception here if the key must conform to a specific format
|
89
|
-
#
|
90
|
-
def check_key(primary_key)
|
91
|
-
# Example: raise "key must be a string object" unless key.is_a? String
|
92
|
-
primary_key
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
# AddQueue
|
98
|
-
#
|
99
|
-
class AddQueue < Queue
|
100
|
-
end
|
101
|
-
|
102
|
-
# ChangeQueue
|
103
|
-
#
|
104
|
-
class ChangeQueue < Queue
|
105
|
-
end
|
106
|
-
|
107
|
-
# DeleteQueue
|
108
|
-
#
|
109
|
-
class DeleteQueue < Queue
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|