metacosm 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e645a9a08cbfe919ac910c227a009362cea85489
4
- data.tar.gz: ddc00033d2b1753af206e7c94ce826a87a65d08e
3
+ metadata.gz: 507c3712499d247e71327f2f613360b595c75715
4
+ data.tar.gz: 2e8eedf08f22ae677cf80a02ffa2d5466a52914c
5
5
  SHA512:
6
- metadata.gz: 144418e13b1af3653d7bb6e8a41349f47c4c9dc4c2a13affac698a611cf00d7d97531348346e454d93a0aa1ba05fc85c248d086c15911714f99d7dfc0ae01aa0
7
- data.tar.gz: a743ea6ca08471c22efd146b2d0b3698b101060eefab3a49b3d36382f384b4f4c8dddbe61ea94959ded1d85d79ff41e4fe9f6002e5c7f5f67c6e3dd4a5cc21ea
6
+ metadata.gz: 6acfd5b2586c6258354cf2c9ee970637f9e1dc46d67ef4c5d4707f1133d49affef2f4ea72407dc86ad3fd1b24079a454d983c7d64f465fdf5edb9ff919896a1c
7
+ data.tar.gz: ca4b957d8aa2cc5023e81f6a7a7ca3b4c6ac8edfdc0d57ca6ec1af6b9e2425f9af8d8d463659801801d8aabf13445a070d2b90376f53732bfecfac90a0856d54
data/README.md CHANGED
@@ -31,6 +31,8 @@ Models only transform their state in response to commands, so their state can be
31
31
 
32
32
  ## Requirements
33
33
 
34
+ Ruby 2.2.1 or higher and Redis. Gosu dependencies are needed for the client.
35
+
34
36
  ## Install
35
37
 
36
38
  $ gem install metacosm
@@ -0,0 +1,14 @@
1
+ require 'connection_pool'
2
+
3
+ REDIS = ConnectionPool.new(size: 5) do
4
+ if ENV['REDISTOGO_URL']
5
+ puts "---> using redis to go!"
6
+ uri = URI.parse(ENV["REDISTOGO_URL"])
7
+ puts "---> parsed uri: #{uri}"
8
+ Redis.new(:host => uri.host, :port => uri.port, :password => uri.password, :thread_safe => true)
9
+ else
10
+ puts "---> using default redis settings..."
11
+ Redis.new
12
+ end
13
+ end
14
+
@@ -9,9 +9,18 @@ module Metacosm
9
9
  def apply(command); fire command end
10
10
 
11
11
  def fire(command)
12
+ puts "---> Firing command at remote sim..."
13
+ puts "[command: #{command.inspect}]"
12
14
  command_dto = command.attrs.merge(handler_module: command.handler_module_name, handler_class_name: command.handler_class_name)
13
- redis = redis_connection
14
- redis.publish(@command_queue_name, Marshal.dump(command_dto))
15
+
16
+ puts "---> Sending command over redis conn: #{redis.inspect}"
17
+ Thread.new do
18
+ REDIS.with do |redis|
19
+ redis.publish(@command_queue_name, Marshal.dump(command_dto))
20
+ end
21
+ end
22
+ puts "---> Sent!"
23
+ true
15
24
  end
16
25
 
17
26
  def received_events
@@ -21,26 +30,27 @@ module Metacosm
21
30
  def setup_connection
22
31
  @remote_listener_thread = Thread.new do
23
32
  begin
24
- redis = redis_connection
25
- redis.subscribe(@event_stream_name) do |on|
26
- on.subscribe do |channel, subscriptions|
27
- puts "Subscribed to remote simulation event stream ##{channel} (#{subscriptions} subscriptions)"
28
- end
33
+ REDIS.with do |redis|
34
+ redis.subscribe(@event_stream_name) do |on|
35
+ on.subscribe do |channel, subscriptions|
36
+ puts "Subscribed to remote simulation event stream ##{channel} (#{subscriptions} subscriptions)"
37
+ end
29
38
 
30
- on.message do |channel, message|
31
- event = Marshal.load(message)
32
- listener_module_name = event.delete(:listener_module)
33
- listener_class_name = event.delete(:listener_class_name)
34
- module_name = listener_module_name
35
- module_name = "Object" if module_name.empty?
36
- listener = (module_name.constantize).const_get(listener_class_name).new(self)
37
- listener.receive(event)
39
+ on.message do |channel, message|
40
+ event = Marshal.load(message)
41
+ listener_module_name = event.delete(:listener_module)
42
+ listener_class_name = event.delete(:listener_class_name)
43
+ module_name = listener_module_name
44
+ module_name = "Object" if module_name.empty?
45
+ listener = (module_name.constantize).const_get(listener_class_name).new(self)
46
+ listener.receive(event)
38
47
 
39
- received_events.push(event)
40
- end
48
+ received_events.push(event)
49
+ end
41
50
 
42
- on.unsubscribe do |channel, subscriptions|
43
- puts "Unsubscribed from remote simulation event stream ##{channel} (#{subscriptions} subscriptions)"
51
+ on.unsubscribe do |channel, subscriptions|
52
+ puts "Unsubscribed from remote simulation event stream ##{channel} (#{subscriptions} subscriptions)"
53
+ end
44
54
  end
45
55
  end
46
56
  rescue ::Redis::BaseConnectionError => error
@@ -1,9 +1,9 @@
1
1
  module Metacosm
2
2
  class Simulation
3
3
  # TODO protected?
4
- def redis_connection
5
- Redis.new
6
- end
4
+ # def redis_connection
5
+ # Redis.new
6
+ # end
7
7
 
8
8
  def params
9
9
  @params ||= {}
@@ -72,8 +72,9 @@ module Metacosm
72
72
 
73
73
  if !@event_publication_channel.nil?
74
74
  event_dto = event.attrs.merge(listener_module: event.listener_module_name, listener_class_name: event.listener_class_name)
75
- redis = redis_connection
76
- redis.publish(@event_publication_channel, Marshal.dump(event_dto))
75
+ REDIS.with do |redis|
76
+ redis.publish(@event_publication_channel, Marshal.dump(event_dto))
77
+ end
77
78
  end
78
79
 
79
80
  if !local_events_disabled?
@@ -99,28 +100,29 @@ module Metacosm
99
100
  def subscribe_for_commands(channel:)
100
101
  p [ :subscribe_to_command_channel, channel: channel ]
101
102
  @command_subscription_thread = Thread.new do
102
- redis = redis_connection
103
- begin
104
- redis.subscribe(channel) do |on|
105
- on.subscribe do |chan, subscriptions|
106
- puts "Subscribed to ##{chan} (#{subscriptions} subscriptions)"
107
- end
108
-
109
- on.message do |chan, message|
110
- # puts "##{chan}: #{message}"
111
- command_data = Marshal.load(message)
112
- p [ :got_message, command_data: command_data ]
113
- apply(command_data)
114
- end
115
-
116
- on.unsubscribe do |chan, subscriptions|
117
- puts "Unsubscribed from ##{chan} (#{subscriptions} subscriptions)"
103
+ REDIS.with do |redis|
104
+ begin
105
+ redis.subscribe(channel) do |on|
106
+ on.subscribe do |chan, subscriptions|
107
+ puts "Subscribed to ##{chan} (#{subscriptions} subscriptions)"
108
+ end
109
+
110
+ on.message do |chan, message|
111
+ # puts "##{chan}: #{message}"
112
+ command_data = Marshal.load(message)
113
+ p [ :got_message, command_data: command_data ]
114
+ apply(command_data)
115
+ end
116
+
117
+ on.unsubscribe do |chan, subscriptions|
118
+ puts "Unsubscribed from ##{chan} (#{subscriptions} subscriptions)"
119
+ end
118
120
  end
121
+ rescue Redis::BaseConnectionError => error
122
+ puts "#{error}, retrying in 1s"
123
+ sleep 1
124
+ retry
119
125
  end
120
- rescue Redis::BaseConnectionError => error
121
- puts "#{error}, retrying in 1s"
122
- sleep 1
123
- retry
124
126
  end
125
127
  end
126
128
  end
@@ -1,4 +1,4 @@
1
1
  module Metacosm
2
2
  # metacosm version
3
- VERSION = "0.3.2"
3
+ VERSION = "0.3.3"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metacosm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-17 00:00:00.000000000 Z
11
+ date: 2016-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: passive_record
@@ -163,6 +163,7 @@ files:
163
163
  - gemspec.yml
164
164
  - lib/metacosm.rb
165
165
  - lib/metacosm/model.rb
166
+ - lib/metacosm/redis.rb
166
167
  - lib/metacosm/remote_simulation.rb
167
168
  - lib/metacosm/simulation.rb
168
169
  - lib/metacosm/support/spec_harness.rb