Sha256: b740a43fea2487c6788789df69493db66adfa0421991f9b391de4c5b18263f2e

Contents?: true

Size: 1.82 KB

Versions: 101

Compression:

Stored size: 1.82 KB

Contents

#!/usr/bin/env rackup -s thin
# 
#  async_tailer.ru
#  raggi/thin
#
#  Tested with 150 spawned tails on OS X
#  
#  Created by James Tucker on 2008-06-18.
#  Copyright 2008 James Tucker <raggi@rubyforge.org>.

# Uncomment if appropriate for you..
# EM.epoll
# EM.kqueue

class DeferrableBody
  include EventMachine::Deferrable
  
  def initialize
    @queue = []
    # make sure to flush out the queue before closing the connection
    callback{
      until @queue.empty?
        @queue.shift.each{|chunk| @body_callback.call(chunk) }
      end
    }
  end
  
  def schedule_dequeue
    return unless @body_callback
    EventMachine::next_tick do
      next unless body = @queue.shift
      body.each do |chunk|
        @body_callback.call(chunk)
      end
      schedule_dequeue unless @queue.empty?
    end
  end 

  def call(body)
    @queue << body
    schedule_dequeue
  end

  def each &blk
    @body_callback = blk
    schedule_dequeue
  end

end

module TailRenderer
  attr_accessor :callback

  def receive_data(data)
    @callback.call([data])
  end

  def unbind
    @callback.succeed
  end
end

class AsyncTailer
  
  AsyncResponse = [-1, {}, []].freeze
    
  def call(env)
    
    body = DeferrableBody.new
    
    EventMachine::next_tick do
      
      env['async.callback'].call [200, {'Content-Type' => 'text/html'}, body]
      
      body.call ["<h1>Async Tailer</h1><pre>"]
      
    end
    
    EventMachine::popen('tail -f /var/log/system.log', TailRenderer) do |t|
      
      t.callback = body
      
      # If for some reason we 'complete' body, close the tail.
      body.callback do
        t.close_connection
      end
      
      # If for some reason the client disconnects, close the tail.
      body.errback do
        t.close_connection
      end
      
    end
    
    AsyncResponse
  end
  
end

run AsyncTailer.new

Version data entries

101 entries across 100 versions & 10 rubygems

Version Path
thin-1.8.2 example/async_tailer.ru
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/thin-1.8.1/example/async_tailer.ru
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/thin-1.8.1/example/async_tailer.ru
thin-1.8.1 example/async_tailer.ru
thin-1.8.0 example/async_tailer.ru
gross-1.7.2 example/async_tailer.ru
thin-1.7.2 example/async_tailer.ru
thin-1.7.1 example/async_tailer.ru
arcabouco-0.2.13 vendor/bundle/gems/thin-1.7.0/example/async_tailer.ru
thin-1.7.0 example/async_tailer.ru
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/thin-1.6.4/example/async_tailer.ru
classiccms-0.7.5 vendor/bundle/gems/thin-1.3.1/example/async_tailer.ru
classiccms-0.7.4 vendor/bundle/gems/thin-1.3.1/example/async_tailer.ru
classiccms-0.7.3 vendor/bundle/gems/thin-1.3.1/example/async_tailer.ru
thin-1.6.4 example/async_tailer.ru
thin-1.6.3 example/async_tailer.ru
michaelyta-thin-1.2.2 example/async_tailer.ru
thin-1.6.2 example/async_tailer.ru
thin-1.6.1 example/async_tailer.ru
thin-1.6.0 example/async_tailer.ru