class LogSender
Constants
- ROWS
Attributes
channel[R]
commands[R]
redis[R]
socket[R]
Public Class Methods
new(socket, redis, commands, channel)
click to toggle source
# File lib/gush/control/log_sender.rb, line 4 def initialize(socket, redis, commands, channel) @redis = redis @commands = commands @channel = channel @socket = socket end
Public Instance Methods
run()
click to toggle source
# File lib/gush/control/log_sender.rb, line 11 def run Thread.new do tail = [0, message_count - ROWS].max head = (tail - 1).downto(0).each_slice(ROWS) loop do if commands.empty? method = :append logs = fetch_logs_after(tail) tail += logs.size else case commands.pop when 'prepend' method = :prepend begin logs = fetch_range(head.next) rescue StopIteration logs = [] end end end send_lines(sanitize_logs(logs), method) sleep 1 end end end
Private Instance Methods
fetch_logs_after(idx)
click to toggle source
# File lib/gush/control/log_sender.rb, line 50 def fetch_logs_after(idx) redis.lrange(redis_key, idx, idx + ROWS) end
fetch_range(r)
click to toggle source
# File lib/gush/control/log_sender.rb, line 54 def fetch_range(r) redis.lrange(redis_key, r.last, r.first).reverse end
message_count()
click to toggle source
# File lib/gush/control/log_sender.rb, line 46 def message_count redis.llen(redis_key) end
redis_key()
click to toggle source
# File lib/gush/control/log_sender.rb, line 58 def redis_key "gush.logs.#{channel}" end
sanitize_logs(lines)
click to toggle source
# File lib/gush/control/log_sender.rb, line 62 def sanitize_logs(lines) lines.map {|l| Rack::Utils.escape_html(l) } end
send_lines(logs, method)
click to toggle source
# File lib/gush/control/log_sender.rb, line 41 def send_lines(logs, method) data = {lines: logs, method: method}.to_json EM.next_tick{ socket.send(data) } if logs.any? end