Sha256: d193022abbaab75be30f4f5c429bfea0eee9967cf7fa4c7aa2760d6fccfe0092

Contents?: true

Size: 1.34 KB

Versions: 27

Compression:

Stored size: 1.34 KB

Contents

require "socket"
require "yajl"

require "gorgon_bunny/lib/gorgon_bunny"
require 'open4'

class GemCommandHandler
  def initialize bunny
    @bunny = bunny
  end

  def handle payload, configuration
    reply_exchange_name = payload[:reply_exchange_name]
    publish_to reply_exchange_name, :type => :running_command

    gem = configuration[:bin_gem_path] || "gem"

    cmd = "#{gem} #{payload[:body][:gem_command]} gorgon"
    pid, stdin, stdout, stderr = Open4::popen4 cmd
    stdin.close

    ignore, status = Process.waitpid2 pid
    exitstatus = status.exitstatus

    output, errors = [stdout, stderr].map { |p| begin p.read ensure p.close end }

    if exitstatus == 0
      reply = {:type => :command_completed, :command => cmd, :stdout => output,
        :stderr => errors}
      publish_to reply_exchange_name, reply
      @bunny.stop
      exit     # TODO: for now exit until we implement a command to exit listeners
    else
      reply = {:type => :command_failed, :command => cmd, :stdout => output, :stderr => errors}
      publish_to reply_exchange_name, reply
   end
  end

  private

  # TODO: factors this out to a class
  def publish_to reply_exchange_name, message
    reply_exchange = @bunny.exchange(reply_exchange_name, :auto_delete => true
)
    reply_exchange.publish(Yajl::Encoder.encode(message.merge(:hostname => Socket.gethostname)))
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
gorgon-0.11.0 lib/gorgon/gem_command_handler.rb
gorgon-0.10.5 lib/gorgon/gem_command_handler.rb
gorgon-0.10.4 lib/gorgon/gem_command_handler.rb
gorgon-0.10.3 lib/gorgon/gem_command_handler.rb
gorgon-0.10.2 lib/gorgon/gem_command_handler.rb
gorgon-0.10.1 lib/gorgon/gem_command_handler.rb
gorgon-0.10.0 lib/gorgon/gem_command_handler.rb
gorgon-0.9.0 lib/gorgon/gem_command_handler.rb
gorgon-0.8.4 lib/gorgon/gem_command_handler.rb
gorgon-0.8.3 lib/gorgon/gem_command_handler.rb
gorgon-0.8.2 lib/gorgon/gem_command_handler.rb
gorgon-0.8.1 lib/gorgon/gem_command_handler.rb
gorgon-0.8.0 lib/gorgon/gem_command_handler.rb
gorgon-0.7.1 lib/gorgon/gem_command_handler.rb
gorgon-0.7.0 lib/gorgon/gem_command_handler.rb
gorgon-0.7.0.rc4 lib/gorgon/gem_command_handler.rb
gorgon-0.7.0.rc3 lib/gorgon/gem_command_handler.rb
gorgon-0.7.0.rc2 lib/gorgon/gem_command_handler.rb
gorgon-0.7.0.rc1 lib/gorgon/gem_command_handler.rb
gorgon-0.6.5 lib/gorgon/gem_command_handler.rb