Class: React::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/react/runner.rb

Defined Under Namespace

Classes: NoQueueError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands_file, options) ⇒ Runner

Returns a new instance of Runner.

Raises:



9
10
11
12
13
14
# File 'lib/react/runner.rb', line 9

def initialize(commands_file, options)
  @options  = options
  @commands = YAML.load_file(commands_file)
  
  raise NoQueueError, "Queue have to be specified!" unless options[:queue]
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



7
8
9
# File 'lib/react/runner.rb', line 7

def commands
  @commands
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/react/runner.rb', line 6

def options
  @options
end

Instance Method Details

#joinObject

It joins all alive threads, and it’s waiting till they will finish.



48
49
50
# File 'lib/react/runner.rb', line 48

def join
  threads.list.each {|t| t.join if t.alive? }
end

#redisObject

Redis client instance.



53
54
55
# File 'lib/react/runner.rb', line 53

def redis
  @redis ||= Redis.new(options[:redis].merge(:thread_safe => true))
end

#startObject

It starts the consumer loop.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/react/runner.rb', line 17

def start
  puts "== Connected to #{redis.client.id}"
  puts "== Waiting for commands from `#{options[:queue]}`"
  
  if options[:daemon]
    puts "== Daemonizing..."
    Daemons.daemonize
  end
  
  loop do
    begin 
      cid = redis.blpop(options[:queue], 10)[1]
      if cmd = commands[cid.to_s]
        puts "\e[33m[#{Time.now}]\e[0m Reacting for `#{cid}` command"
        threads.add(Thread.new { system(cmd) })
      end
    rescue Interrupt, SystemExit
      puts "\nCleaning up..."
      return 0
    rescue => ex
      puts "ERROR: #{ex}"
    end
  end
end

#threadsObject

Returns group of executor’s threads.



43
44
45
# File 'lib/react/runner.rb', line 43

def threads
  @threads ||= ThreadGroup.new
end