lib/metacosm/simulation.rb in metacosm-0.2.14 vs lib/metacosm/simulation.rb in metacosm-0.3.0

- old
+ new

@@ -18,11 +18,10 @@ def execute while true if (command=command_queue.pop) apply(command) - sleep 0.01 end Thread.pass end end @@ -82,30 +81,32 @@ end end def subscribe_for_commands(channel:) p [ :subscribe_to_command_channel, channel: channel ] - redis = Redis.new - begin - redis.subscribe(channel) do |on| - on.subscribe do |chan, subscriptions| - puts "Subscribed to ##{chan} (#{subscriptions} subscriptions)" - end + @command_subscription_thread = Thread.new do + redis = Redis.new + begin + redis.subscribe(channel) do |on| + on.subscribe do |chan, subscriptions| + puts "Subscribed to ##{chan} (#{subscriptions} subscriptions)" + end - on.message do |chan, message| - puts "##{chan}: #{message}" - apply(Marshal.load(message)) - end + on.message do |chan, message| + puts "##{chan}: #{message}" + apply(Marshal.load(message)) + end - on.unsubscribe do |chan, subscriptions| - puts "Unsubscribed from ##{chan} (#{subscriptions} subscriptions)" - end - end - rescue Redis::BaseConnectionError => error - puts "#{error}, retrying in 1s" - sleep 1 - retry + on.unsubscribe do |chan, subscriptions| + puts "Unsubscribed from ##{chan} (#{subscriptions} subscriptions)" + end + end + rescue Redis::BaseConnectionError => error + puts "#{error}, retrying in 1s" + sleep 1 + retry + end end end def disable_local_events @local_events_disabled = true @@ -128,10 +129,10 @@ @current ||= new end def clear! @events = [] - @command_queue&.clear + @command_queue && @command_queue.clear end protected def handler_for(command) @handlers ||= {}