Class: SidekickClient

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_override = {}) ⇒ SidekickClient

Returns a new instance of SidekickClient.



20
21
22
23
24
# File 'lib/sidekick/client/sidekick_client.rb', line 20

def initialize(config_override={})
  load_config(config_override)
  @logger.info "Initialized SidekickClient with config #{@config.inspect}"
  @qconn = SidekickQueuePublisher.new(@config, @logger)   
end

Class Attribute Details

.configObject

Returns the value of attribute config.



11
12
13
# File 'lib/sidekick/client/sidekick_client.rb', line 11

def config
  @config
end

.logObject

Returns the value of attribute log.



12
13
14
# File 'lib/sidekick/client/sidekick_client.rb', line 12

def log
  @log
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/sidekick/client/sidekick_client.rb', line 18

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/sidekick/client/sidekick_client.rb', line 17

def logger
  @logger
end

Class Method Details

.send_and_disconnect(cmd, args = {}, queue_name = nil) ⇒ Object



48
49
50
# File 'lib/sidekick/client/sidekick_client.rb', line 48

def self.send_and_disconnect(cmd, args={}, queue_name=nil)
  SidekickClient.new.send_and_disconnect(cmd,args,queue_name)
end

.send_and_receive(cmd, args = {}, queue_name = nil) ⇒ Object



52
53
54
# File 'lib/sidekick/client/sidekick_client.rb', line 52

def self.send_and_receive(cmd, args={}, queue_name=nil)
  SidekickClient.new.send_and_receive(cmd,args,queue_name)
end

.send_freeform_message(message, queue_name) ⇒ Object



56
57
58
# File 'lib/sidekick/client/sidekick_client.rb', line 56

def self.send_freeform_message(message, queue_name)
  SidekickClient.new.send_freeform_message(message, queue_name)
end

Instance Method Details

#load_config(config_override) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sidekick/client/sidekick_client.rb', line 26

def load_config(config_override)
  if SidekickClient.config && File.exists?(SidekickClient.config)
    @config ||= YAML.load_file(SidekickClient.config).with_indifferent_access.merge(config_override)
  elsif SidekickClient.config
    raise LoadError, "Missing configuration: #{SidekickClient.config}"
  else
    warn "Set the sidekick config path using SidekickClient.config='/path/to/sidekick.conf'"
  end
  @config ||= config_override.with_indifferent_access
  @config[:sidekick_log_file] ||= SidekickClient.log

  # If STDOUT is specified as a value use the actual stream to log to STDOUT
  if @config[:sidekick_log_file] == 'STDOUT'
    @config[:sidekick_log_file] = Kernel.const_get('STDOUT')
  end

  FileUtils.mkdir_p(File.dirname(@config[:sidekick_log_file])) if @config[:sidekick_log_file].is_a?(String)
  @logger = SidekickLogger.new(@config[:sidekick_log_file])

  @config[:exchange] ||= "amq.direct"
end

#send_and_disconnect(cmd, args = {}, queue_name = nil) ⇒ Object



66
67
68
69
70
71
# File 'lib/sidekick/client/sidekick_client.rb', line 66

def send_and_disconnect(cmd, args={}, queue_name=nil)
  message = { :command => cmd, :args => args}
  
  @qconn.publish(message.to_yaml, queue_name || @config[:queue_name])
  return true
end

#send_and_receive(cmd, args = {}, queue_name = nil) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/sidekick/client/sidekick_client.rb', line 73

def send_and_receive(cmd, args={}, queue_name=nil)
  temp_queue = "tmp-reply-#{UUIDTools::UUID.random_create}"
  message = { :command => cmd, :args => args, :reply_to => {:exchange => @config[:exchange], :routing_key => temp_queue}}
  
  response = @qconn.publish(message.to_yaml, queue_name || @config[:queue_name], temp_queue)        
  return response && YAML.load(response)
end

#send_freeform_message(message, queue_name = nil) ⇒ Object

Send an unformatted (freeform message)



61
62
63
64
# File 'lib/sidekick/client/sidekick_client.rb', line 61

def send_freeform_message(message, queue_name=nil)
  @qconn.publish(message, queue_name || @config[:queue_name])
  return true
end