alephant-publisher 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9781530bb38777e05f67c596a0f7b1226373ac03
4
- data.tar.gz: cf18cba21835ae5ef44e73777d028d0cfb1656a8
3
+ metadata.gz: 278b7dbd7d1001987418dec5721269cab8336ec3
4
+ data.tar.gz: 582cc75dfce85ecef619366d75ba5e6db13c2446
5
5
  SHA512:
6
- metadata.gz: 421d05fc124317bdd184db2d64c5f492c1bce4e2450ed6c2c7e4001f22be8392c1c7837eb7330f788edf38d857a415331512918252d32760e6960b27a2daf99f
7
- data.tar.gz: d36a0e7c12755bfd5849a0399e4aadd5adea945b959f77e1588e2d51791c3d13392b8f319f2b9621abfb4b34c4a482b47bb640f953a60f34ca5e66d83e979397
6
+ metadata.gz: fb43706d6b88b6d63794d8d7256a956b52ebd21513dacd9b3a1e9f5fbfe348a2642f0fcaff29cad45326f2fb9b811cd0ba5aeb04347a212cb1f126acb08b68b0
7
+ data.tar.gz: 8693925153211a73d3ae5c3ac9e81663196a3d0c414e8c414ee5d67270815d329078f831f9864ca0d50b6a69cf2b5e582627932395755fd0bed800018dd2dc37
@@ -0,0 +1,19 @@
1
+ module Alephant
2
+ module Publisher
3
+ module SQSHelper
4
+ class Archiver
5
+ attr_reader :cache
6
+
7
+ def initialize(cache)
8
+ @cache = cache
9
+ end
10
+
11
+ def see(m)
12
+
13
+ m
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ module Alephant
2
+ module Publisher
3
+ module SQSHelper
4
+ class Archiver
5
+ attr_reader :cache
6
+
7
+ def initialize(cache)
8
+ @cache = cache
9
+ end
10
+
11
+ def see(message)
12
+ return if message.nil?
13
+
14
+ message.tap do |m|
15
+ cache.put(
16
+ "archive/#{m.id}",
17
+ m.body,
18
+ message_meta_for(m)
19
+ )
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def message_meta_for(m)
26
+ {
27
+ :id => m.id,
28
+ :md5 => m.md5,
29
+ :logged_at => Time.now.to_s,
30
+ :queue => m.queue.url,
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ require 'aws-sdk'
2
+ require 'alephant/logger'
3
+
4
+ module Alephant
5
+ module Publisher
6
+ module SQSHelper
7
+ class Queue
8
+ WAIT_TIME = 5
9
+ VISABILITY_TIMEOUT = 300
10
+
11
+ include Logger
12
+
13
+ attr_reader :queue, :timeout, :wait_time, :archiver
14
+
15
+ def initialize(queue, archiver, timeout = VISABILITY_TIMEOUT, wait_time = WAIT_TIME)
16
+ @queue = queue
17
+ @archiver = archiver
18
+ @timeout = timeout
19
+ @wait_time = wait_time
20
+
21
+ logger.info("Queue#initialize: reading from #{queue.url}")
22
+ end
23
+
24
+ def message
25
+ recieve.tap do |m|
26
+ logger.info("Queue#message: received #{m.id}")
27
+ archive m
28
+ end
29
+ end
30
+
31
+ def archive(m)
32
+ archiver.see(m)
33
+ end
34
+
35
+ def recieve
36
+ queue.receive_message({
37
+ :visibility_timeout => timeout,
38
+ :wait_time_seconds => wait_time
39
+ })
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Publisher
3
- VERSION = "0.2.5"
3
+ VERSION = "0.2.6"
4
4
  end
5
5
  end
@@ -59,7 +59,7 @@ module Alephant
59
59
  end
60
60
 
61
61
  def store(id, content, location)
62
- cache.put(location, content)
62
+ cache.put(location, content, :msg_id => message.id)
63
63
  lookup.write(id, options, seq_id, location)
64
64
  end
65
65
 
@@ -2,7 +2,8 @@ require_relative 'env'
2
2
 
3
3
  require 'alephant/publisher/version'
4
4
  require 'alephant/publisher/options'
5
- require 'alephant/publisher/queue'
5
+ require 'alephant/publisher/sqs_helper/queue'
6
+ require 'alephant/publisher/sqs_helper/archive'
6
7
  require 'alephant/publisher/writer'
7
8
  require 'alephant/logger'
8
9
 
@@ -18,13 +19,14 @@ module Alephant
18
19
  VISIBILITY_TIMEOUT = 60
19
20
  RECEIVE_WAIT_TIME = 15
20
21
 
21
- attr_reader :queue, :executor
22
+ attr_reader :queue, :executor, :opts
22
23
 
23
24
  def initialize(opts)
24
25
  @opts = opts
25
26
 
26
- @queue = Queue.new(
27
- opts.queue[:sqs_queue_url],
27
+ @queue = SQSHelper::Queue.new(
28
+ aws_queue,
29
+ archiver,
28
30
  opts.queue[:visibility_timeout] || VISIBILITY_TIMEOUT,
29
31
  opts.queue[:receive_wait_time] || RECEIVE_WAIT_TIME,
30
32
  )
@@ -36,6 +38,21 @@ module Alephant
36
38
 
37
39
  private
38
40
 
41
+ def archiver
42
+ SQSHelper::Archiver.new(archive_cache)
43
+ end
44
+
45
+ def archive_cache
46
+ Cache.new(
47
+ opts.writer[:s3_bucket_id],
48
+ opts.writer[:s3_object_path]
49
+ )
50
+ end
51
+
52
+ def aws_queue
53
+ AWS::SQS.new.queues[opts.queue[:sqs_queue_url]]
54
+ end
55
+
39
56
  def process(msg)
40
57
  unless msg.nil?
41
58
  Writer.new(@opts.writer, msg).run!
@@ -46,3 +63,4 @@ module Alephant
46
63
  end
47
64
  end
48
65
  end
66
+
@@ -5,12 +5,12 @@ describe Alephant::Publisher do
5
5
  let(:instance) { Alephant::Publisher.create(options) }
6
6
 
7
7
  before(:each) do
8
- Alephant::Publisher::Queue.any_instance.stub(:initialize)
8
+ Alephant::Publisher::SQSHelper::Queue.any_instance.stub(:initialize)
9
9
  end
10
10
 
11
11
  describe "#initialize(opts = {}, logger)" do
12
12
  it "sets parser, sequencer, queue and writer" do
13
- expect(instance.queue).to be_a Alephant::Publisher::Queue
13
+ expect(instance.queue).to be_a Alephant::Publisher::SQSHelper::Queue
14
14
  end
15
15
  end
16
16
  end
@@ -13,13 +13,11 @@ describe Alephant::Publisher::RenderMapper do
13
13
 
14
14
  describe "initialize(view_base_path)" do
15
15
  context "view_base_path = invalid_path" do
16
- it "should raise InvalidViewPath" do
16
+ it "should raise an error" do
17
17
  File.stub(:directory?).and_return(false)
18
18
  expect {
19
19
  subject.new(component_id, './invalid_path')
20
- }.to raise_error(
21
- 'Invalid path'
22
- )
20
+ }.to raise_error
23
21
  end
24
22
  end
25
23
 
data/spec/writer_spec.rb CHANGED
@@ -73,7 +73,7 @@ describe Alephant::Publisher::Writer do
73
73
  "sequence" => "1",
74
74
  "vary" => "foo"
75
75
  }
76
- Struct.new(:body).new(data.to_json)
76
+ Struct.new(:body,:id).new(data.to_json,'id')
77
77
  end
78
78
 
79
79
  let(:expected_location) do
@@ -104,7 +104,7 @@ describe Alephant::Publisher::Writer do
104
104
  Alephant::Cache
105
105
  .any_instance
106
106
  .should_receive(:put)
107
- .with(expected_location, "content")
107
+ .with(expected_location, "content", :msg_id=>"id")
108
108
  end
109
109
 
110
110
  after do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-publisher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Integralist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-07 00:00:00.000000000 Z
11
+ date: 2014-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -363,9 +363,11 @@ files:
363
363
  - alephant-publisher.gemspec
364
364
  - lib/alephant/env.rb
365
365
  - lib/alephant/publisher.rb
366
+ - lib/alephant/publisher/archive.rb
366
367
  - lib/alephant/publisher/options.rb
367
- - lib/alephant/publisher/queue.rb
368
368
  - lib/alephant/publisher/render_mapper.rb
369
+ - lib/alephant/publisher/sqs_helper/archive.rb
370
+ - lib/alephant/publisher/sqs_helper/queue.rb
369
371
  - lib/alephant/publisher/version.rb
370
372
  - lib/alephant/publisher/writer.rb
371
373
  - spec/fixtures/components/foo/models/bar.rb
@@ -373,7 +375,6 @@ files:
373
375
  - spec/fixtures/components/foo/templates/bar.mustache
374
376
  - spec/fixtures/components/foo/templates/foo.mustache
375
377
  - spec/publisher_spec.rb
376
- - spec/queue_spec.rb
377
378
  - spec/render_mapper_spec.rb
378
379
  - spec/spec_helper.rb
379
380
  - spec/writer_spec.rb
@@ -407,7 +408,6 @@ test_files:
407
408
  - spec/fixtures/components/foo/templates/bar.mustache
408
409
  - spec/fixtures/components/foo/templates/foo.mustache
409
410
  - spec/publisher_spec.rb
410
- - spec/queue_spec.rb
411
411
  - spec/render_mapper_spec.rb
412
412
  - spec/spec_helper.rb
413
413
  - spec/writer_spec.rb
@@ -1,42 +0,0 @@
1
- require 'aws-sdk'
2
- require 'alephant/logger'
3
-
4
- module Alephant
5
- module Publisher
6
- class Queue
7
- WAIT_TIME = 5
8
- VISABILITY_TIMEOUT = 300
9
-
10
- include ::Alephant::Logger
11
-
12
- attr_reader :timeout, :wait_time
13
- attr_accessor :q
14
-
15
- def initialize(id, timeout = VISABILITY_TIMEOUT, wait_time = WAIT_TIME)
16
- @timeout = timeout
17
- @wait_time = wait_time
18
- @sqs = AWS::SQS.new
19
- @q = @sqs.queues[id]
20
-
21
- unless @q.exists?
22
- @q = @sqs.queues.create(id)
23
- sleep_until_queue_exists
24
- logger.info("Queue.initialize: created queue with id #{id}")
25
- end
26
-
27
- logger.info("Queue.initialize: ended with id #{id}")
28
- end
29
-
30
- def sleep_until_queue_exists
31
- sleep 1 until @q.exists?
32
- end
33
-
34
- def message
35
- @q.receive_message({
36
- :visibility_timeout => @timeout,
37
- :wait_time_seconds => @wait_time
38
- })
39
- end
40
- end
41
- end
42
- end
data/spec/queue_spec.rb DELETED
@@ -1,84 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Alephant::Publisher::Queue do
4
- subject { Alephant::Publisher::Queue }
5
-
6
- describe "initialize(id)" do
7
- it "sets @q to an instance of AWS:SQS::Queue" do
8
- AWS::SQS::Queue
9
- .any_instance
10
- .stub(:exists?)
11
- .and_return(true)
12
-
13
- instance = subject.new(:id)
14
- expect(instance.q).to be_a(AWS::SQS::Queue)
15
- end
16
-
17
- context "@q.exists? == false" do
18
- it "@q = AWS::SQS.new.queues.create(id), then sleep_until_queue_exists" do
19
- queue = double()
20
- queue.stub(:exists?).and_return(false)
21
-
22
- queue_collection = double()
23
- queue_collection
24
- .should_receive(:create)
25
- .with(:id)
26
- .and_return(queue)
27
-
28
- sqs = double()
29
- sqs.should_receive(:queues)
30
- .and_return({ :id => queue }, queue_collection)
31
-
32
- AWS::SQS
33
- .should_receive(:new)
34
- .and_return(sqs)
35
-
36
- subject
37
- .any_instance
38
- .should_receive(:sleep_until_queue_exists)
39
-
40
- instance = subject.new(:id)
41
- end
42
- end
43
- end
44
-
45
- describe "sleep_until_queue_exists" do
46
- context "@q.exists? == true" do
47
- it "should not call sleep" do
48
- AWS::SQS::Queue
49
- .any_instance
50
- .stub(:exists?)
51
- .and_return(true)
52
-
53
- Alephant::Publisher::Queue
54
- .any_instance
55
- .stub(:sleep)
56
-
57
- Alephant::Publisher::Queue
58
- .any_instance
59
- .should_not_receive(:sleep)
60
-
61
- subject.new(:id).sleep_until_queue_exists
62
- end
63
- end
64
- context "@q.exists? == false" do
65
- it "should call sleep(1)" do
66
- AWS::SQS::Queue
67
- .any_instance
68
- .stub(:exists?)
69
- .and_return(true, false, true)
70
-
71
- Alephant::Publisher::Queue
72
- .any_instance
73
- .stub(:sleep)
74
-
75
- Alephant::Publisher::Queue
76
- .any_instance
77
- .should_receive(:sleep)
78
- .with(1)
79
-
80
- subject.new(:id).sleep_until_queue_exists
81
- end
82
- end
83
- end
84
- end