double_decker 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 84c51fbc0973e6d5c7fc7f6522de3c6a015423fef30a9bc59726bea16612db7e
4
- data.tar.gz: c4ffeff4e80278ee3213d12022bd5766ca0033edf55926334c0b4a3a0be00684
3
+ metadata.gz: 720e2cbc7fe7d4e394fa6334c31323777fbd849146b3ea71b919b4683fbedc7b
4
+ data.tar.gz: c076994dcb91a33c6d58539eb0e69e3dd4a27262fd7e8f299ca84da389cec7a9
5
5
  SHA512:
6
- metadata.gz: 4bf1066987f51577ae81fe50923f83217b696c501ca6f6ca29e33d76ac03497287ce57f055a092bf3ac11d4c59d0eb813501ab12967662f7bfeb12cabf27ac04
7
- data.tar.gz: 26423775150edf5cba75744e82bcfaf89e9fda5927808066442e0d5a8049723f5aef7a2c60af45ed512355d1427daaed474a079ac595b11dfb011f6acae6d6b7
6
+ metadata.gz: 9702a9da87d92ad5aa9271ac64937f5e975c17ca321b462311413d66e06413f5d3434986b488562917bb1575de73735b7580e6eb90ec39b71f34b1e02437d89e
7
+ data.tar.gz: 15877c849403d831319ffce729adfab623140c470bd23d3f574cf7bdc1cda68c9b6c6c47c596082b9d1103dc828b1c3c1d293b12388b7b4374f790e936c83d3a
@@ -0,0 +1,65 @@
1
+ module DoubleDecker
2
+ class Agent
3
+ attr_reader :id, :end_time
4
+
5
+ def initialize(bus, bus_data, expected_agents, merge_proc: nil)
6
+ @bus = bus
7
+ @bus_data = bus_data
8
+ @expected_agents = expected_agents
9
+ @finished = nil
10
+ @merge_proc = merge_proc
11
+ @id = setup!
12
+ merge({})
13
+ end
14
+
15
+ def merge_like(&block)
16
+ @merge_proc = block
17
+ end
18
+
19
+ def merge(hash)
20
+ @bus_data.merge(id, hash, &@merge_proc)
21
+ end
22
+
23
+ def to_h
24
+ @bus_data.to_h[id.to_s]
25
+ end
26
+
27
+ def on_finished(&block)
28
+ @finished = block
29
+ end
30
+
31
+ def wait_for_expected_agents(max_tries=0)
32
+ if @expected_agents
33
+ puts "AGENTS: #{@bus_data.active_agents} EXPECTED: #{@expected_agents}"
34
+ loop do
35
+ break if (@bus_data.active_agents == @expected_agents.to_i) || (max_tries == 5)
36
+ sleep 1
37
+ max_tries += 1
38
+ end
39
+ end
40
+ end
41
+
42
+ def finish!(end_time = DateTime.now)
43
+ @end_time = end_time
44
+ @finished&.call(to_h)
45
+ if last
46
+ @bus.finished&.call(@bus_data.to_h)
47
+ @bus_data.teardown_bus
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def last
54
+ teardown!.zero?
55
+ end
56
+
57
+ def teardown!
58
+ @bus_data.teardown_agent
59
+ end
60
+
61
+ def setup!
62
+ @bus_data.setup_agent
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,34 @@
1
+ require "redis"
2
+ require "date"
3
+ require "json"
4
+ require_relative "./agent"
5
+ require_relative "./bus_data"
6
+
7
+ module DoubleDecker
8
+ class Bus
9
+ attr_reader :run_id, :expected_agents, :finished
10
+
11
+ def initialize(run_id, url: "redis://localhost", expected_agents: nil, redis: Redis.new(url: url))
12
+ @store = redis
13
+ @run_id = run_id
14
+ @expected_agents = expected_agents
15
+ setup!
16
+ end
17
+
18
+ def register(&merge_proc)
19
+ agent = Agent.new(self, BusData.new(run_id, @store), expected_agents, merge_proc: merge_proc)
20
+ agent.wait_for_expected_agents
21
+ agent
22
+ end
23
+
24
+ def on_finished(&block)
25
+ @finished = block
26
+ end
27
+
28
+ protected
29
+
30
+ def setup!
31
+ @store.get(run_id) || @store.set(run_id, {}.to_json)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ require "json"
2
+
3
+ module DoubleDecker
4
+ class BusData
5
+ attr_reader :run_id, :store
6
+
7
+ def initialize(run_id, store)
8
+ @run_id = run_id
9
+ @store = store
10
+ end
11
+
12
+ def to_h
13
+ data = store.get(run_id)
14
+ data && JSON.parse(data)
15
+ end
16
+
17
+ def setup_agent
18
+ store.incr("#{run_id}_agents").to_i
19
+ end
20
+
21
+ def teardown_bus
22
+ store.del(run_id)
23
+ store.del("#{run_id}_agents")
24
+ end
25
+
26
+ def teardown_agent
27
+ store.decr("#{run_id}_agents").to_i
28
+ end
29
+
30
+ def active_agents
31
+ store.get("#{run_id}_agents").to_i
32
+ end
33
+
34
+ def merge(agent_id, hash, &block)
35
+ block.call(hash) if block
36
+ data = to_h || {}
37
+ data[agent_id.to_s] ||= {}
38
+ data[agent_id.to_s].merge!(hash)
39
+ store.set(run_id, data.to_json)
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: double_decker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Gregory
@@ -17,6 +17,9 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - src/double_decker.rb
20
+ - src/double_decker/agent.rb
21
+ - src/double_decker/bus.rb
22
+ - src/double_decker/bus_data.rb
20
23
  homepage: https://rubygems.org/gems/double_decker
21
24
  licenses:
22
25
  - MIT