Class: React::Runner
- Inherits:
-
Object
- Object
- React::Runner
- Defined in:
- lib/react/runner.rb
Defined Under Namespace
Classes: NoQueueError
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(commands_file, options) ⇒ Runner
constructor
A new instance of Runner.
-
#join ⇒ Object
It joins all alive threads, and it’s waiting till they will finish.
-
#redis ⇒ Object
Redis client instance.
-
#start ⇒ Object
It starts the consumer loop.
-
#threads ⇒ Object
Returns group of executor’s threads.
Constructor Details
#initialize(commands_file, options) ⇒ Runner
Returns a new instance of Runner.
9 10 11 12 13 14 |
# File 'lib/react/runner.rb', line 9 def initialize(commands_file, ) @options = @commands = YAML.load_file(commands_file) raise NoQueueError, "Queue have to be specified!" unless [:queue] end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
7 8 9 |
# File 'lib/react/runner.rb', line 7 def commands @commands end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/react/runner.rb', line 6 def @options end |
Instance Method Details
#join ⇒ Object
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 |
#redis ⇒ Object
Redis client instance.
53 54 55 |
# File 'lib/react/runner.rb', line 53 def redis @redis ||= Redis.new([:redis].merge(:thread_safe => true)) end |
#start ⇒ Object
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 `#{[:queue]}`" if [:daemon] puts "== Daemonizing..." Daemons.daemonize end loop do begin cid = redis.blpop([: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 |
#threads ⇒ Object
Returns group of executor’s threads.
43 44 45 |
# File 'lib/react/runner.rb', line 43 def threads @threads ||= ThreadGroup.new end |