Class: Executer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/executer/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/executer/client.rb', line 6

def initialize(config)
  if File.exist?(config)
    options = YAML.load(File.read(config))
    redis_url = options['redis']
    @logger = Logger.new(options['log'] || '/var/log/executer.log')
  else
    redis_url = config
    @logger = Logger.new('/var/log/executer.log')
  end
  @redis_1 = Redis.connect(:url => "redis://#{redis_url}")
  @redis_2 = Redis.connect(:url => "redis://#{redis_url}")
end

Instance Attribute Details

#redis_1Object (readonly)

Returns the value of attribute redis_1.



4
5
6
# File 'lib/executer/client.rb', line 4

def redis_1
  @redis_1
end

#redis_2Object (readonly)

Returns the value of attribute redis_2.



4
5
6
# File 'lib/executer/client.rb', line 4

def redis_2
  @redis_2
end

Instance Method Details

#log(message) ⇒ Object



41
42
43
# File 'lib/executer/client.rb', line 41

def log(message)
  @logger.info("CLIENT: #{message}")
end

#run(options) ⇒ Object



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

def run(options)
  id = options[:id]
  options = Yajl::Encoder.encode(options)

  success = nil
  Timeout.timeout(60*60) do
    @redis_1.subscribe("executer:response:#{id}") do |on|
      on.subscribe do |channel, subscriptions|
        log("Queuing: #{options.inspect}")
        @redis_2.rpush "executer:request", options
      end

      on.message do |channel, message|
        success = message
        log("Finished: #{message.inspect}")
        @redis_1.unsubscribe
      end
    end
  end
  success
end