metacosm 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/metacosm/redis.rb +14 -0
- data/lib/metacosm/remote_simulation.rb +29 -19
- data/lib/metacosm/simulation.rb +27 -25
- data/lib/metacosm/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 507c3712499d247e71327f2f613360b595c75715
|
4
|
+
data.tar.gz: 2e8eedf08f22ae677cf80a02ffa2d5466a52914c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6acfd5b2586c6258354cf2c9ee970637f9e1dc46d67ef4c5d4707f1133d49affef2f4ea72407dc86ad3fd1b24079a454d983c7d64f465fdf5edb9ff919896a1c
|
7
|
+
data.tar.gz: ca4b957d8aa2cc5023e81f6a7a7ca3b4c6ac8edfdc0d57ca6ec1af6b9e2425f9af8d8d463659801801d8aabf13445a070d2b90376f53732bfecfac90a0856d54
|
data/README.md
CHANGED
@@ -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
|
-
|
14
|
-
redis
|
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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
40
|
-
|
48
|
+
received_events.push(event)
|
49
|
+
end
|
41
50
|
|
42
|
-
|
43
|
-
|
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
|
data/lib/metacosm/simulation.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Metacosm
|
2
2
|
class Simulation
|
3
3
|
# TODO protected?
|
4
|
-
def redis_connection
|
5
|
-
|
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
|
-
|
76
|
-
|
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
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
data/lib/metacosm/version.rb
CHANGED
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.
|
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-
|
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
|