alephant-publisher 0.1.0 → 0.1.3

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: 35bd92123f4d88d37f98565eada8b900944314f8
4
- data.tar.gz: 6a878f67cbc84dc067ead0e14bdd7711451bf9b3
3
+ metadata.gz: be7e1421d4a9edd58b23cf1b67a39d0b77bc711f
4
+ data.tar.gz: 10abc065851746ceb2d215e61dae669653ac0432
5
5
  SHA512:
6
- metadata.gz: bbae7c11ceff43c6e67a4a1d9e8180783ea7152f76b48cebf6f706632bb412a0963005876e664b16f43cb4b3ddc5afcdd1ce7365de2b394453f824f526a566db
7
- data.tar.gz: ba18a78fa958da6e2d9cdb5313033f02a56ef7eee8ea999ad2814772686476ba900994cdbe49f18e8d9bdfdf233550ca6ed2a97f936e96c7c47ea992effc89f6
6
+ metadata.gz: 06619711d680043e4c6153d5be311bef2e095645faa41d9f1e4841468ec7622cf0b3f2b51f6ec69b09267e0e90e682526fd69664c488ee03f7fad5fc5e8e36f1
7
+ data.tar.gz: ca6f76631797d41a818c2844627534ab536eb7f846fe0cdfc4528922a1b02a0cfdda0a2381040e61442a23b5f03479182c387262439944ba414baa97df12de69
@@ -0,0 +1,39 @@
1
+ require 'crimp'
2
+
3
+ require 'alephant/sequencer'
4
+ require 'alephant/support/parser'
5
+
6
+ module Alephant
7
+ module Publisher
8
+ class WriteOperation
9
+ attr_reader :msg, :data, :options, :options_hash, :version, :batch_sequencer
10
+
11
+ def initialize(msg, opts)
12
+ @msg = msg
13
+ @data = Support::Parser.new(opts[:msg_vary_path]).parse(msg)
14
+ @options = @data[:options]
15
+ @options_hash = Crimp.signature(@options)
16
+ @renderer_id = opts[:renderer_id]
17
+ @sequencer_opts = opts[:sequencer_opts]
18
+ @batch_sequencer = sequencer_for(@renderer_id, @options)
19
+ @version = @batch_sequencer.sequence_id_from(@msg)
20
+ end
21
+
22
+ def sequencer_for(id, options)
23
+ Sequencer.create(
24
+ @sequencer_opts[:table_name],
25
+ sequencer_id_from(id, options),
26
+ @sequencer_opts[:id_path]
27
+ )
28
+ end
29
+
30
+ private
31
+
32
+ def sequencer_id_from(id,options)
33
+ "#{id}/#{Crimp.signature(options)}"
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+
@@ -1,4 +1,10 @@
1
- require 'crimp'
1
+ require 'alephant/cache'
2
+ require 'alephant/views'
3
+ require 'alephant/renderer'
4
+ require 'alephant/lookup'
5
+
6
+ require 'alephant/publisher/models/write_operation'
7
+ require 'alephant/publisher/models/render_mapper'
2
8
 
3
9
  module Alephant
4
10
  module Publisher
@@ -6,8 +12,6 @@ module Alephant
6
12
  attr_reader :mapper, :cache
7
13
 
8
14
  def initialize(opts)
9
- @renderer_id = opts[:renderer_id]
10
-
11
15
  @cache = Cache.new(
12
16
  opts[:s3_bucket_id],
13
17
  opts[:s3_object_path]
@@ -19,32 +23,51 @@ module Alephant
19
23
  )
20
24
 
21
25
  @lookup_table_name = opts[:lookup_table_name]
26
+
27
+ @renderer_id = opts[:renderer_id]
28
+
29
+ @write_opts = {
30
+ :sequencer_opts => {
31
+ :table_name => opts[:sequencer_table_name],
32
+ :id_path => opts[:sequence_id_path]
33
+ },
34
+ :msg_vary_path => opts[:msg_vary_id_path],
35
+ :renderer_id => opts[:renderer_id]
36
+ }
22
37
  end
23
38
 
24
- def write(data, version = nil)
25
- lookup = Lookup.create(@lookup_table_name)
39
+ def write(msg)
40
+ write_op = WriteOperation.new(msg, @write_opts)
26
41
 
27
- mapper.generate(data).each do |id, r|
28
- store(id, r.render, data[:options], version, lookup)
42
+ write_op.batch_sequencer.sequence(msg) do |msg|
43
+ mapper.generate(write_op.data).each do |component_id, renderer|
44
+ write_component(write_op, component_id, renderer)
45
+ end
29
46
  end
30
-
31
- lookup.batch_process
32
47
  end
33
48
 
34
49
  private
35
50
 
36
- def store(id, content, options, version, lookup)
37
- location = location_for(
38
- id,
39
- Crimp.signature(options),
40
- version
41
- )
51
+ def write_component(write_op, component_id, renderer)
52
+ location = location_for(component_id, write_op.options, write_op.version)
53
+ component_sequencer = write_op.sequencer_for(component_id, write_op.options)
54
+
55
+ component_sequencer.sequence(write_op.msg) do |msg|
56
+ store(component_id, renderer.render, write_op.options, location)
57
+ end
58
+ end
42
59
 
60
+ def store(component_id, content, options, location)
43
61
  cache.put(location, content)
44
- lookup.batch_write(options, location, id)
62
+ lookup_for(component_id).write(options, location)
63
+ end
64
+
65
+ def lookup_for(id)
66
+ Lookup.create(@lookup_table_name, id)
45
67
  end
46
68
 
47
- def location_for(component_id, options_hash, version = nil)
69
+ def location_for(component_id, options, version = nil)
70
+ options_hash = Crimp.signature(options)
48
71
  base_name = "#{@renderer_id}/#{component_id}/#{options_hash}"
49
72
  version ? "#{base_name}/#{version}" : base_name
50
73
  end
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Publisher
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -1,15 +1,17 @@
1
1
  require_relative 'env'
2
2
 
3
- require 'alephant/support/parser'
4
- require 'alephant/sequencer'
5
- require 'alephant/cache'
3
+ require 'java'
4
+
5
+ # setup executor
6
+ java_import 'java.util.concurrent.ThreadPoolExecutor'
7
+ java_import 'java.util.concurrent.TimeUnit'
8
+ java_import 'java.util.concurrent.LinkedBlockingQueue'
9
+ java_import 'java.util.concurrent.FutureTask'
10
+ java_import 'java.util.concurrent.Callable'
11
+
6
12
  require 'alephant/logger'
7
- require 'alephant/views'
8
- require 'alephant/renderer'
9
- require 'alephant/lookup'
10
13
 
11
14
  require "alephant/publisher/version"
12
- require 'alephant/publisher/models/render_mapper'
13
15
  require 'alephant/publisher/models/writer'
14
16
  require 'alephant/publisher/models/queue'
15
17
 
@@ -24,28 +26,53 @@ module Alephant
24
26
  private
25
27
 
26
28
  class Publisher
27
- attr_reader :sequencer, :queue, :writer, :parser
29
+ attr_reader :queue
28
30
 
29
31
  def initialize(opts, logger)
30
32
  ::Alephant::Logger.set_logger(logger) unless logger.nil?
31
33
 
32
- @parser = Support::Parser.new(
33
- opts[:msg_vary_id_path]
34
+ @opts = opts
35
+ @queue = Queue.new(
36
+ opts[:sqs_queue_url]
34
37
  )
38
+ end
35
39
 
36
- @sequencer = Sequencer.create(
37
- opts[:sequencer_table_name],
38
- opts[:sqs_queue_url],
39
- opts[:sequence_id_path]
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
44
+
45
+ executor = ThreadPoolExecutor.new(
46
+ core_pool_size,
47
+ maximum_pool_size,
48
+ keep_alive_time,
49
+ TimeUnit::SECONDS,
50
+ LinkedBlockingQueue.new
40
51
  )
41
52
 
42
- @queue = Queue.new(
43
- opts[:sqs_queue_url]
44
- )
53
+ @queue.poll do |msg|
54
+ task = FutureTask.new(PublishTask.new(@opts, msg))
55
+ executor.execute(task)
56
+ end
57
+
58
+ executor.shutdown()
59
+ end
45
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
46
70
  @writer = Writer.new(
47
71
  opts.select do |k,v|
48
72
  [
73
+ :msg_vary_id_path,
74
+ :sequencer_table_name,
75
+ :sequence_id_path,
49
76
  :renderer_id,
50
77
  :s3_bucket_id,
51
78
  :s3_object_path,
@@ -56,21 +83,8 @@ module Alephant
56
83
  )
57
84
  end
58
85
 
59
- def run!
60
- Thread.new do
61
- @queue.poll { |msg| receive(msg) }
62
- end
63
- end
64
-
65
- def receive(msg)
66
- write msg if sequencer.sequential?(msg)
67
- end
68
-
69
- private
70
-
71
- def write(msg)
72
- writer.write(parser.parse(msg), sequencer.sequence_id_from(msg))
73
- sequencer.set_last_seen(msg)
86
+ def call
87
+ writer.write(msg)
74
88
  end
75
89
 
76
90
  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.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Integralist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-27 00:00:00.000000000 Z
11
+ date: 2014-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -351,6 +351,7 @@ files:
351
351
  - lib/alephant/publisher.rb
352
352
  - lib/alephant/publisher/models/queue.rb
353
353
  - lib/alephant/publisher/models/render_mapper.rb
354
+ - lib/alephant/publisher/models/write_operation.rb
354
355
  - lib/alephant/publisher/models/writer.rb
355
356
  - lib/alephant/publisher/version.rb
356
357
  - spec/fixtures/components/foo/models/bar.rb