-
-
Save jackbit/1936886 to your computer and use it in GitHub Desktop.
Simple stream tail -f over http socket using Ruby EventMachine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# README: Start server with em-stream-tail.rb tail-filename | |
# Connect to server with curl http://localhost:8083 | |
# Observe log events on curl. | |
# | |
require 'rubygems' | |
require 'eventmachine' | |
if ARGV.empty? | |
puts "usage: em-stream-tail.rb filename" | |
exit 0 | |
end | |
class TailListener < EM::Connection | |
def self.spawn | |
EventMachine.popen("tail -f #{ARGV[0]}", TailListener) | |
end | |
def receive_data data | |
TailServer.announce data | |
end | |
end | |
class TailServer < EM::Connection | |
@@connected_clients = [] | |
def self.announce data | |
@@connected_clients.each { |cc| cc.send_data data } | |
end | |
def initialize | |
@@connected_clients << self | |
end | |
def receive_data data; end | |
def unbind | |
@@connected_clients.delete self | |
end | |
end | |
EventMachine.run do | |
TailListener.spawn | |
EventMachine.start_server("0.0.0.0", 8083, TailServer) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment