Moved application-specific code out from rack_socket to lib/eventbus.rb
authorPeter Amstutz <peter.amstutz@curoverse.com>
Tue, 22 Apr 2014 13:47:38 +0000 (09:47 -0400)
committerPeter Amstutz <peter.amstutz@curoverse.com>
Tue, 22 Apr 2014 13:47:38 +0000 (09:47 -0400)
services/api/app/middlewares/rack_socket.rb
services/api/config/initializers/eventbus.rb
services/api/lib/eventbus.rb [new file with mode: 0644]

index 779c67503046a0f49dac738685a5a5ba73e90fd0..f13316f416a190bca8e8bceb0c280e4be3af599e 100644 (file)
@@ -1,6 +1,5 @@
 require 'rack'
 require 'faye/websocket'
-require 'oj'
 require 'eventmachine'
 
 class RackSocket
@@ -38,8 +37,7 @@ class RackSocket
       }
     end
 
-    @channel = EventMachine::Channel.new
-    @bgthread = nil
+    @handler = @options[:handler].new
   end
 
   def call env
@@ -47,47 +45,7 @@ class RackSocket
     if request.path_info == @endpoint and Faye::WebSocket.websocket?(env)
       ws = Faye::WebSocket.new(env)
 
-      sub = @channel.subscribe do |msg|
-        puts "sending #{msg}"
-        ws.send({:message => "log"}.to_json)
-      end
-
-      ws.on :message do |event|
-        puts "got #{event.data}"
-        ws.send(event.data)
-      end
-
-      ws.on :close do |event|
-        p [:close, event.code, event.reason]
-        @channel.unsubscribe sub
-        ws = nil
-      end
-
-      unless @bgthread
-        @bgthread = true
-        Thread.new do
-          # from http://stackoverflow.com/questions/16405520/postgres-listen-notify-rails
-          ActiveRecord::Base.connection_pool.with_connection do |connection|
-            conn = connection.instance_variable_get(:@connection)
-            begin
-              conn.async_exec "LISTEN logs"
-              while true
-                conn.wait_for_notify do |channel, pid, payload|
-                  puts "Received a NOTIFY on channel #{channel}"
-                  puts "from PG backend #{pid}"
-                  puts "saying #{payload}"
-                  @channel.push true
-                end
-              end
-            ensure
-              # Don't want the connection to still be listening once we return
-              # it to the pool - could result in weird behavior for the next
-              # thread to check it out.
-              conn.async_exec "UNLISTEN *"
-            end
-          end
-        end
-      end
+      @handler.on_connect ws
 
       # Return async Rack response
       ws.rack_response
@@ -97,5 +55,3 @@ class RackSocket
   end
 
 end
-
-
index 2350b578c6e9aeae8b97749ad51e4251806947e4..a065632e39386c11e5c8bd4741354d73484e988e 100644 (file)
@@ -1,3 +1,5 @@
+require 'eventbus'
+
 Server::Application.configure do
-  config.middleware.insert_before ActionDispatch::Static, RackSocket
+  config.middleware.insert_before ActionDispatch::Static, RackSocket, {:handler => EventBus}
 end
diff --git a/services/api/lib/eventbus.rb b/services/api/lib/eventbus.rb
new file mode 100644 (file)
index 0000000..ff3cd8b
--- /dev/null
@@ -0,0 +1,55 @@
+require 'eventmachine'
+require 'oj'
+require 'faye/websocket'
+
+class EventBus
+  def initialize
+    @channel = EventMachine::Channel.new
+    @bgthread = nil
+  end
+
+  def on_connect ws
+      sub = @channel.subscribe do |msg|
+        puts "sending #{msg}"
+        ws.send({:message => "log"}.to_json)
+      end
+
+      ws.on :message do |event|
+        puts "got #{event.data}"
+        ws.send(event.data)
+      end
+
+      ws.on :close do |event|
+        p [:close, event.code, event.reason]
+        @channel.unsubscribe sub
+        ws = nil
+      end
+
+      unless @bgthread
+        @bgthread = true
+        Thread.new do
+          # from http://stackoverflow.com/questions/16405520/postgres-listen-notify-rails
+          ActiveRecord::Base.connection_pool.with_connection do |connection|
+            conn = connection.instance_variable_get(:@connection)
+            begin
+              conn.async_exec "LISTEN logs"
+              while true
+                conn.wait_for_notify do |channel, pid, payload|
+                  puts "Received a NOTIFY on channel #{channel}"
+                  puts "from PG backend #{pid}"
+                  puts "saying #{payload}"
+                  @channel.push true
+                end
+              end
+            ensure
+              # Don't want the connection to still be listening once we return
+              # it to the pool - could result in weird behavior for the next
+              # thread to check it out.
+              conn.async_exec "UNLISTEN *"
+            end
+          end
+        end
+      end
+
+  end
+end