Class: Execache::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis_url) ⇒ Client

Returns a new instance of Client.



6
7
8
9
# File 'lib/execache/client.rb', line 6

def initialize(redis_url)
  @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/execache/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/execache/client.rb', line 4

def redis_2
  @redis_2
end

Instance Method Details

#exec(options) ⇒ Object



11
12
13
14
15
16
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/execache/client.rb', line 11

def exec(options)
  wait = options.delete(:wait)
  subscribe_to = options[:channel] = Digest::SHA1.hexdigest("#{rand}")
  options = Yajl::Encoder.encode(options)
  response = nil

  Timeout.timeout(60) do
    @redis_1.subscribe("execache:response:#{subscribe_to}") do |on|
      on.subscribe do |channel, subscriptions|
        @redis_2.rpush "execache:request", options
      end

      on.message do |channel, message|
        if message.include?('[PENDING]')
          if wait == false
            response = false
            @redis_1.unsubscribe
          else  
            @redis_2.rpush "execache:request", options
          end
        else
          response = Yajl::Parser.parse(message)
          @redis_1.unsubscribe
        end
      end
    end
  end

  response
end