Class: Profit::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/profit/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



7
8
9
# File 'lib/profit/server.rb', line 7

def initialize
  @ctx = ZMQ::Context.new
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



5
6
7
# File 'lib/profit/server.rb', line 5

def ctx
  @ctx
end

Instance Method Details

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/profit/server.rb', line 22

def run
  @run = true
  EM.run do

    @redis_pool = EM::Pool.new
    spawn = lambda { @redis_pool.add Redis.new(host: "127.0.0.1", port: 6379) }
    @redis_pool.on_error { |conn| spawn[] }
    10.times { spawn[] }

    @puller = @ctx.bind(:PULL, "tcp://127.0.0.1:5556")

    # gives us a graceful exit
    setup_trap_int

    if @shutdown
      EM.next_tick do # change to add_timer(1) for more delay
        @run = false
      end
    end

    # allows us to break out of the loop
    EM.add_periodic_timer do
      unless @run
        EM.stop unless @run
      end
    end

    # ensures we can continue to run other specs
    EM.add_shutdown_hook { puts("destroy"); @ctx.destroy }

    # this is the entry to message handling
    EM.add_periodic_timer do
      @redis_pool.perform do |conn|
        message_handler = MessageHandler.new @puller.recv, conn

        message_handler.callback do |response|
          puts response.inspect
        end
        message_handler.run
        message_handler
      end
    end
  end
end

#setup_trap_intObject



15
16
17
18
19
20
# File 'lib/profit/server.rb', line 15

def setup_trap_int
  trap :INT do
    puts "\nSIGINT received, quitting!"
    EM.stop
  end
end

#shutdown!Object



11
12
13
# File 'lib/profit/server.rb', line 11

def shutdown!
  @shutdown = true
end