alephant-publisher 0.1.3 → 0.1.4

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: be7e1421d4a9edd58b23cf1b67a39d0b77bc711f
4
- data.tar.gz: 10abc065851746ceb2d215e61dae669653ac0432
3
+ metadata.gz: 8a18ed72b3f0273f7e8bedef7d101a3acebd5e79
4
+ data.tar.gz: 310f181c40f274f0cfca9081976c1b37690e6b2c
5
5
  SHA512:
6
- metadata.gz: 06619711d680043e4c6153d5be311bef2e095645faa41d9f1e4841468ec7622cf0b3f2b51f6ec69b09267e0e90e682526fd69664c488ee03f7fad5fc5e8e36f1
7
- data.tar.gz: ca6f76631797d41a818c2844627534ab536eb7f846fe0cdfc4528922a1b02a0cfdda0a2381040e61442a23b5f03479182c387262439944ba414baa97df12de69
6
+ metadata.gz: 5f28f89d431a8f264def3eb1b5c79a2425a49f242edc9e9500e30f490abdc00e8be568c468e87155bbcd2183c8e4d709b565357808e54cbc6f72878ff4c4f826
7
+ data.tar.gz: 5fa2be94fbdebf4c8a530245c957bf18040f0ccc81779af439a2e4726eff72c085a92a7ac5d848166f19351a5f8ad40d52a19d7b5962bc28f5a49c422f6b770d
@@ -2,19 +2,16 @@ require_relative 'env'
2
2
 
3
3
  require 'java'
4
4
 
5
- # setup executor
6
5
  java_import 'java.util.concurrent.ThreadPoolExecutor'
7
6
  java_import 'java.util.concurrent.TimeUnit'
8
7
  java_import 'java.util.concurrent.LinkedBlockingQueue'
9
8
  java_import 'java.util.concurrent.FutureTask'
10
- java_import 'java.util.concurrent.Callable'
11
9
 
10
+ require 'alephant/publisher/version'
11
+ require 'alephant/publisher/queue'
12
+ require 'alephant/publisher/publish_task'
12
13
  require 'alephant/logger'
13
14
 
14
- require "alephant/publisher/version"
15
- require 'alephant/publisher/models/writer'
16
- require 'alephant/publisher/models/queue'
17
-
18
15
  module Alephant
19
16
  module Publisher
20
17
  include ::Alephant::Logger
@@ -23,70 +20,62 @@ module Alephant
23
20
  Publisher.new(opts, logger)
24
21
  end
25
22
 
26
- private
27
-
28
23
  class Publisher
29
- attr_reader :queue
24
+
25
+ VISIBILITY_TIMEOUT = 300
26
+ KEEP_ALIVE_TIMEOUT = 300
27
+ RECEIVE_WAIT_TIME = 15
28
+ POOL_MIN_SIZE = 2
29
+ POOL_MAX_SIZE = 4
30
+
31
+ attr_reader :queue, :executor
30
32
 
31
33
  def initialize(opts, logger)
32
34
  ::Alephant::Logger.set_logger(logger) unless logger.nil?
33
35
 
34
36
  @opts = opts
35
37
  @queue = Queue.new(
36
- opts[:sqs_queue_url]
38
+ opts[:sqs_queue_url],
39
+ opts[:visibility_timeout] || VISIBILITY_TIMEOUT,
40
+ opts[:receive_wait_time] || RECEIVE_WAIT_TIME,
37
41
  )
38
- end
39
42
 
40
- def run!
41
- core_pool_size = @opts[:renderer_pool_min_size] || 2
42
- maximum_pool_size = @opts[:renderer_pool_max_size] || 4
43
- keep_alive_time = @opts[:render_keep_alive_time] || 300
43
+ @writer_opts = opts.select do |k,v|
44
+ [
45
+ :msg_vary_id_path,
46
+ :sequencer_table_name,
47
+ :sequence_id_path,
48
+ :renderer_id,
49
+ :s3_bucket_id,
50
+ :s3_object_path,
51
+ :view_path,
52
+ :lookup_table_name
53
+ ].include? k
54
+ end
44
55
 
45
- executor = ThreadPoolExecutor.new(
46
- core_pool_size,
47
- maximum_pool_size,
48
- keep_alive_time,
56
+ @executor = ThreadPoolExecutor.new(
57
+ @opts[:renderer_pool_min_size] || POOL_MIN_SIZE,
58
+ @opts[:renderer_pool_max_size] || POOL_MAX_SIZE,
59
+ @opts[:render_keep_alive_time] || KEEP_ALIVE_TIMEOUT,
49
60
  TimeUnit::SECONDS,
50
61
  LinkedBlockingQueue.new
51
62
  )
63
+ end
52
64
 
53
- @queue.poll do |msg|
54
- task = FutureTask.new(PublishTask.new(@opts, msg))
55
- executor.execute(task)
65
+ def run!
66
+ while true
67
+ executor.execute(
68
+ FutureTask.new(
69
+ PublishTask.new(
70
+ @writer_opts,
71
+ @queue.message
72
+ )
73
+ )
74
+ )
56
75
  end
57
76
 
58
77
  executor.shutdown()
59
78
  end
60
-
61
- end
62
-
63
- class PublishTask
64
- include Callable
65
-
66
- attr_reader :writer, :msg
67
-
68
- def initialize(opts,msg)
69
- @msg = msg
70
- @writer = Writer.new(
71
- opts.select do |k,v|
72
- [
73
- :msg_vary_id_path,
74
- :sequencer_table_name,
75
- :sequence_id_path,
76
- :renderer_id,
77
- :s3_bucket_id,
78
- :s3_object_path,
79
- :view_path,
80
- :lookup_table_name
81
- ].include? k
82
- end
83
- )
84
- end
85
-
86
- def call
87
- writer.write(msg)
88
- end
89
-
90
79
  end
91
80
  end
92
81
  end
@@ -0,0 +1,29 @@
1
+ require 'java'
2
+
3
+ java_import 'java.util.concurrent.Callable'
4
+
5
+ require 'alephant/publisher/writer'
6
+
7
+ module Alephant
8
+ module Publisher
9
+ class PublishTask
10
+ include Callable
11
+
12
+ attr_reader :opts, :message
13
+
14
+ def initialize(opts, message)
15
+ @message = message
16
+ @opts = opts
17
+ end
18
+
19
+ def call
20
+ unless message.nil?
21
+ Writer.new(opts).write(message)
22
+ message.delete
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+
@@ -4,11 +4,17 @@ require 'alephant/logger'
4
4
  module Alephant
5
5
  module Publisher
6
6
  class Queue
7
+ WAIT_TIME = 5
8
+ VISABILITY_TIMEOUT = 300
9
+
7
10
  include ::Alephant::Logger
8
11
 
12
+ attr_reader :timeout, :wait_time
9
13
  attr_accessor :q
10
14
 
11
- def initialize(id)
15
+ def initialize(id, timeout = VISABILITY_TIMEOUT, wait_time = WAIT_TIME)
16
+ @timeout = timeout
17
+ @wait_time = wait_time
12
18
  @sqs = AWS::SQS.new
13
19
  @q = @sqs.queues[id]
14
20
 
@@ -25,10 +31,12 @@ module Alephant
25
31
  sleep 1 until @q.exists?
26
32
  end
27
33
 
28
- def poll(*args, &block)
29
- logger.info("Queue.poll: polling with arguments #{args}")
30
- @q.poll(*args, &block)
34
+ def message
35
+ @q.receive_message({
36
+ :visibility_timeout => @timeout,
37
+ :wait_time_seconds => @wait_time
38
+ })
31
39
  end
32
40
  end
33
41
  end
34
- end
42
+ end
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Publisher
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -16,7 +16,7 @@ module Alephant
16
16
  @renderer_id = opts[:renderer_id]
17
17
  @sequencer_opts = opts[:sequencer_opts]
18
18
  @batch_sequencer = sequencer_for(@renderer_id, @options)
19
- @version = @batch_sequencer.sequence_id_from(@msg)
19
+ @version = @batch_sequencer.sequence_id_from(msg)
20
20
  end
21
21
 
22
22
  def sequencer_for(id, options)
@@ -3,8 +3,8 @@ require 'alephant/views'
3
3
  require 'alephant/renderer'
4
4
  require 'alephant/lookup'
5
5
 
6
- require 'alephant/publisher/models/write_operation'
7
- require 'alephant/publisher/models/render_mapper'
6
+ require 'alephant/publisher/write_operation'
7
+ require 'alephant/publisher/render_mapper'
8
8
 
9
9
  module Alephant
10
10
  module Publisher
@@ -4,89 +4,22 @@ describe Alephant::Publisher do
4
4
  let(:instance) { Alephant::Publisher.create }
5
5
 
6
6
  before(:each) do
7
- Alephant::Publisher::Writer.any_instance.stub(:initialize)
8
7
  Alephant::Publisher::Queue.any_instance.stub(:initialize)
9
- Alephant::Support::Parser.any_instance.stub(:initialize)
10
- Alephant::Sequencer::Sequencer.any_instance.stub(:initialize)
11
8
  end
12
9
 
13
10
  describe "#initialize(opts = {}, logger)" do
14
11
  it "sets parser, sequencer, queue and writer" do
15
- expect(instance.writer).to be_a Alephant::Publisher::Writer
16
12
  expect(instance.queue).to be_a Alephant::Publisher::Queue
17
- expect(instance.parser).to be_a Alephant::Support::Parser
18
- expect(instance.sequencer).to be_a Alephant::Sequencer::Sequencer
19
13
  end
20
14
  end
15
+ end
21
16
 
22
- describe "#run!" do
23
- it "returns a Thread" do
24
- expect(instance.run!).to be_a(Thread)
25
- end
26
-
27
- it "calls @queue.poll" do
28
- instance.should_receive(:receive).with(:msg)
29
-
30
- expect_any_instance_of(Alephant::Publisher::Queue)
31
- .to receive(:poll)
32
- .and_yield(:msg)
33
-
34
- t = instance.run!
35
- t.join
36
- end
17
+ describe Alephant::Publisher::PublishTask do
18
+ before(:each) do
19
+ Alephant::Publisher::PublishTask.any_instance
20
+ .stub(:initialize)
37
21
  end
38
22
 
39
- describe "#receive(msg)" do
40
- before(:each) do
41
- Alephant::Support::Parser
42
- .any_instance
43
- .stub(:parse)
44
- .and_return(:parsed_msg)
45
-
46
- Alephant::Sequencer::Sequencer
47
- .any_instance
48
- .stub(:sequence_id_from)
49
- .and_return(:sequence_id)
50
-
51
- Alephant::Sequencer::Sequencer
52
- .any_instance
53
- .stub(:set_last_seen)
54
- end
55
-
56
- context "message is nonsequential" do
57
- before(:each) do
58
- Alephant::Sequencer::Sequencer
59
- .any_instance
60
- .stub(:sequential?)
61
- .and_return(false)
62
- end
63
-
64
- it "should not call write" do
65
- Alephant::Publisher::Writer
66
- .any_instance
67
- .should_not_receive(:write)
68
-
69
- instance.receive(:msg)
70
- end
71
- end
72
-
73
- context "message is sequential" do
74
- before(:each) do
75
- Alephant::Sequencer::Sequencer
76
- .any_instance
77
- .stub(:sequential?)
78
- .and_return(true)
79
- end
80
-
81
- it "calls writer with a parsed message and sequence_id" do
82
- Alephant::Publisher::Writer
83
- .any_instance
84
- .should_receive(:write)
85
- .with(:parsed_msg, :sequence_id)
86
-
87
- instance.receive(:msg)
88
- end
89
- end
90
- end
23
+ it { should respond_to(:call) }
91
24
 
92
25
  end
@@ -1,4 +1,6 @@
1
1
  require 'pry'
2
+
3
+ require 'aws-sdk'
2
4
  require 'alephant/publisher'
3
5
  require 'alephant/publisher/models/writer'
4
6
  require 'alephant/publisher/models/queue'
@@ -1,89 +1,112 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Alephant::Publisher::Writer do
4
+ let(:opts) do
5
+ {
6
+ :s3_bucket_id => :s3_bucket_id,
7
+ :s3_object_path => :s3_object_path,
8
+ :renderer_id => :renderer_id,
9
+ :view_path => :view_path,
10
+ :lookup_table_name => 'lookup_table_name',
11
+ :sequencer_table_name => :sequencer_table_name,
12
+ :sequence_id_path => '$.sequence',
13
+ :msg_vary_id_path => '$.vary',
14
+ }
15
+ end
16
+
4
17
  before(:each) do
18
+ AWS.stub!
19
+
5
20
  Alephant::Publisher::RenderMapper
6
21
  .any_instance
7
- .stub(:initialize)
22
+ .should_receive(:initialize)
23
+ .with(
24
+ opts[:renderer_id],
25
+ opts[:view_path]
26
+ )
8
27
 
9
- Alephant::Cache
10
- .any_instance
11
- .stub(:initialize)
12
- end
28
+ Alephant::Cache
29
+ .any_instance
30
+ .should_receive(:initialize)
31
+ .with(
32
+ opts[:s3_bucket_id],
33
+ opts[:s3_object_path]
34
+ )
13
35
 
14
- subject do
15
- Alephant::Publisher::Writer.new({
16
- :renderer_id => 'renderer_id',
17
- :lookup_table_name => 'lookup_table_name'
18
- })
19
- end
36
+ Alephant::Sequencer::SequenceTable
37
+ .any_instance
38
+ .stub(:create)
20
39
 
21
- describe "#write(data, version)" do
22
- before(:each) do
23
- Alephant::Publisher::RenderMapper
24
- .any_instance
25
- .stub(:generate)
26
- .and_return({
27
- 'component_id' => Struct.new(:render).new('content')
28
- })
40
+ Alephant::Sequencer::Sequencer
41
+ .any_instance
42
+ .stub(:sequencer_id_from)
43
+ .and_return(1)
29
44
 
30
- end
45
+ Alephant::Sequencer::Sequencer
46
+ .any_instance
47
+ .stub(:set_last_seen)
31
48
 
32
- it "should write the correct lookup location" do
33
- options = { :key => :value }
34
- data = { :options => options }
49
+ Alephant::Sequencer::Sequencer
50
+ .any_instance
51
+ .stub(:get_last_seen)
35
52
 
36
- Alephant::Cache.any_instance
37
- .stub(:put)
53
+ Alephant::Lookup::LookupHelper
54
+ .any_instance
55
+ .stub(:create_lookup_table)
38
56
 
39
- Alephant::Lookup
40
- .should_receive(:create)
41
- .with('lookup_table_name')
42
- .and_call_original
57
+ Alephant::Lookup::LookupTable
58
+ .any_instance
59
+ .stub(:table_name)
43
60
 
44
- Alephant::Lookup::LookupHelper.any_instance
45
- .stub(:initialize)
61
+ Alephant::Publisher::RenderMapper
62
+ .any_instance
63
+ .stub(:generate)
64
+ .and_return({
65
+ 'component_id' => Struct.new(:render).new('content')
66
+ })
46
67
 
47
- Alephant::Lookup::LookupTable
48
- .any_instance
49
- .stub(:table_name)
68
+ end
50
69
 
51
- Alephant::Lookup::LookupHelper.any_instance
52
- .should_receive(:batch_write)
53
- .with(
54
- options,
55
- 'renderer_id/component_id/42de5e5c6f74b9fe4d956704a6d9e1c7/0',
56
- 'component_id'
57
- )
70
+ subject do
71
+ Alephant::Publisher::Writer.new(opts)
72
+ end
58
73
 
59
- Alephant::Lookup::LookupHelper.any_instance
60
- .should_receive(:batch_process)
74
+ describe "#write(data, version)" do
75
+ let(:msg) do
76
+ data = {
77
+ "sequence" => "1",
78
+ "vary" => "foo"
79
+ }
80
+ Struct.new(:body).new(data.to_json)
81
+ end
61
82
 
62
- subject.write(data, 0)
83
+ let(:expected_location) do
84
+ 'renderer_id/component_id/218c835cec343537589dbf1619532e4d/1'
63
85
  end
64
86
 
65
- it "should put the correct location, content to cache" do
66
- Alephant::Lookup::LookupHelper
67
- .any_instance
68
- .stub(:initialize)
87
+ it "should write the correct lookup location" do
88
+ Alephant::Cache.any_instance.stub(:put)
69
89
 
70
90
  Alephant::Lookup::LookupHelper
71
91
  .any_instance
72
- .stub(:batch_write)
92
+ .should_receive(:write)
93
+ .with(
94
+ {:variant=>"foo"},
95
+ expected_location
96
+ )
97
+ end
73
98
 
74
- Alephant::Lookup::LookupHelper
75
- .any_instance
76
- .stub(:batch_process)
99
+ it "should put the correct location, content to cache" do
100
+ Alephant::Lookup::LookupHelper.any_instance.stub(:write)
77
101
 
78
- Alephant::Lookup::LookupTable
102
+ Alephant::Cache
79
103
  .any_instance
80
- .stub(:table_name)
81
-
82
- Alephant::Cache.any_instance
83
104
  .should_receive(:put)
84
- .with('renderer_id/component_id/35589a1cc0b3ca90fc52d0e711c0c434/0', 'content')
105
+ .with(expected_location, "content")
106
+ end
85
107
 
86
- subject.write({}, 0)
108
+ after do
109
+ subject.write(msg)
87
110
  end
88
111
  end
89
112
  end
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.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Integralist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-07 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -349,11 +349,12 @@ files:
349
349
  - alephant-publisher.gemspec
350
350
  - lib/alephant/env.rb
351
351
  - lib/alephant/publisher.rb
352
- - lib/alephant/publisher/models/queue.rb
353
- - lib/alephant/publisher/models/render_mapper.rb
354
- - lib/alephant/publisher/models/write_operation.rb
355
- - lib/alephant/publisher/models/writer.rb
352
+ - lib/alephant/publisher/publish_task.rb
353
+ - lib/alephant/publisher/queue.rb
354
+ - lib/alephant/publisher/render_mapper.rb
356
355
  - lib/alephant/publisher/version.rb
356
+ - lib/alephant/publisher/write_operation.rb
357
+ - lib/alephant/publisher/writer.rb
357
358
  - spec/fixtures/components/foo/models/bar.rb
358
359
  - spec/fixtures/components/foo/models/foo.rb
359
360
  - spec/fixtures/components/foo/templates/bar.mustache