Class: Workling::Clients::MemcacheQueueClient

Inherits:
BrokerBase show all
Defined in:
lib/workling/clients/memcache_queue_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BrokerBase

#dispatch, #subscribe

Methods inherited from Base

#dispatch, #logger, #subscribe

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)

delegates directly through to the memcache connection.



99
100
101
102
103
104
105
# File 'lib/workling/clients/memcache_queue_client.rb', line 99

def method_missing(method, *args)
  begin
    self.connection.send(method, *args)
  rescue MemCache::MemCacheError => e
    raise Workling::WorklingConnectionError.new("#{e.class.to_s} - #{e.message}")        
  end
end

Instance Attribute Details

#connectionObject

the memcache connection object



36
37
38
# File 'lib/workling/clients/memcache_queue_client.rb', line 36

def connection
  @connection
end

#queueserver_urlsObject

the url with which the memcache client expects to reach starling



33
34
35
# File 'lib/workling/clients/memcache_queue_client.rb', line 33

def queueserver_urls
  @queueserver_urls
end

Class Method Details

.installed?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/workling/clients/memcache_queue_client.rb', line 14

def self.installed?
  begin
    require 'starling' 
  rescue LoadError
  end

  Object.const_defined? "Starling"
end

.loadObject



23
24
25
26
27
28
29
30
# File 'lib/workling/clients/memcache_queue_client.rb', line 23

def self.load
  begin
    gem 'memcache-client'
    require 'memcache'
  rescue Gem::LoadError
    Workling::Base.logger.info "WORKLING: couldn't find memcache-client. Install: \"gem install memcache-client\". "
  end
end

Instance Method Details

#closeObject

closes the memcache connection



56
57
58
59
60
61
62
63
64
# File 'lib/workling/clients/memcache_queue_client.rb', line 56

def close
  begin
    self.connection.flush_all
  rescue MemCache::MemCacheError => err
    STDERR.puts "Memcache client doesn't respond to flush all"
  ensure
    self.connection.reset
  end
end

#connectObject

the client attempts to connect to queueserver using the configuration options found in

    Workling.config. this can be configured in config/workling.yml. 

the initialization code will raise an exception if memcache-client cannot connect 
to queueserver.


46
47
48
49
50
51
52
53
# File 'lib/workling/clients/memcache_queue_client.rb', line 46

def connect
  listens_on = Workling.config[:listens_on] || "localhost:22122"
  @queueserver_urls = listens_on.split(',').map { |url| url ? url.strip : url }
  options = [@queueserver_urls, Workling.config[:memcache_options]].compact
  self.connection = ::MemCache.new(*options)

  raise_unless_connected!
end

#request(key, value) ⇒ Object

implements the client job request and retrieval



67
68
69
# File 'lib/workling/clients/memcache_queue_client.rb', line 67

def request(key, value)
  set(key, value)
end

#retrieve(key) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/workling/clients/memcache_queue_client.rb', line 71

def retrieve(key)
  begin
    get(key)
  rescue MemCache::MemCacheError => e
    # failed to enqueue, raise a workling error so that it propagates upwards
    raise Workling::WorklingConnectionError.new("#{e.class.to_s} - #{e.message}")        
  end
end