Sha256: 1314b5b0b4d3070bd8c4a2358da59063ea9b4319567048b35a4582349e877266

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module Buster
  class Worker
    def initialize(context)
      @context = context
    end

    def run
      worker = @context.socket(ZMQ::DEALER)
      worker.connect("inproc://workers")
      while true
        worker.recv_strings(msgs = [])
        return if msgs.length < 2

        body = MessagePack.unpack(msgs.pop)
        message_name = msgs.pop
        reply_id = msgs.pop

        handler = find_handler message_name
        if handler.nil?
          puts "No handler found for #{message_name}"
          return
        end

        handler.reply_action = lambda do |name,props|
          worker.send_strings([reply_id, name.to_s, MessagePack.pack(props)])
        end

        handler.execute body
      end
      worker.close
    end


    def self.start_worker(context)
      worker = Worker.new(context)
      t = Thread.new { worker.run }
      t.run
    end

    private

    def find_handler(name)
     camelized = name.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
     const_name = "#{camelized}Handler"
     Object.const_get(const_name).new if Object.const_defined?(const_name)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
buster-0.1.3 lib/buster/worker.rb
buster-0.1.2 lib/buster/worker.rb
buster-0.1.1 lib/buster/worker.rb