Class: AllQ::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/allq/connection.rb

Overview

Represents a connection to a allq instance.

Constant Summary collapse

MAX_RETRIES =

Default number of retries to send a command to a connection

3
DEFAULT_RETRY_INTERVAL =

Default retry interval

1
DEFAULT_PORT =

Default port value for beanstalk connection

11300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address = '') ⇒ Connection

Initializes new connection.

Examples:

AllQ::Connection.new('127.0.0.1')
AllQ::Connection.new('127.0.0.1:11300')

ENV['ALLQ_CLIENT_URL'] = '127.0.0.1:11300'
@b = AllQ.new
@b.connection.host # => '127.0.0.1'
@b.connection.port # => '11300'

Parameters:

  • address (String) (defaults to: '')

    allq instance address.



31
32
33
34
35
# File 'lib/allq/connection.rb', line 31

def initialize(address = '')
  @address = address || _host_from_env
rescue
  _raise_not_connected!
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



17
18
19
# File 'lib/allq/connection.rb', line 17

def address
  @address
end

#connectionObject (readonly)

Returns the value of attribute connection.



17
18
19
# File 'lib/allq/connection.rb', line 17

def connection
  @connection
end

#hostObject (readonly)

Returns the value of attribute host.



17
18
19
# File 'lib/allq/connection.rb', line 17

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



17
18
19
# File 'lib/allq/connection.rb', line 17

def port
  @port
end

Instance Method Details

#call_socat(data, timeout = 3.0) ⇒ Object



47
48
49
50
51
# File 'lib/allq/connection.rb', line 47

def call_socat(data, timeout = 3.0)
  cmd_string = "echo '#{data}' | socat -t #{timeout} - tcp4-connect:#{@address}"
  output = `#{cmd_string}`
  return output
end

#closeObject

Close connection with allq server.

Examples:

@conn.close


75
76
# File 'lib/allq/connection.rb', line 75

def close
end

#socat(command, options = {}) {|block.call(res)| ... } ⇒ Object

Yields:

  • (block.call(res))


37
38
39
40
41
42
43
44
45
# File 'lib/allq/connection.rb', line 37

def socat(command, options={}, &block)
  send_string = command.to_s
  if send_string.include?("'")
    puts "Single quotes not allow in JSON. This will probably error."
  end
  res = call_socat(send_string)
  _raise_not_connected if res.include?("Connection refused")
  yield block.call(res)
end

#to_sObject Also known as: inspect

Returns string representation of job.

Examples:

@conn.inspect


83
84
85
# File 'lib/allq/connection.rb', line 83

def to_s
  "#<AllQ::Connection host=#{host.inspect} port=#{port.inspect}>"
end

#transmit(command, options = {}, &block) ⇒ Array<Hash{String => String, Number}>

Send commands to allq server via connection.

Examples:

@conn = AllQ::Connection.new
@conn.transmit('bury 123')
@conn.transmit('stats')

Parameters:

  • command (String)

    AllQ command

Returns:

  • (Array<Hash{String => String, Number}>)

    AllQ command response



62
63
64
65
66
67
68
# File 'lib/allq/connection.rb', line 62

def transmit(command, options={}, &block)
  _with_retry(options[:retry_interval], options[:init]) do
    res = call_socat(command.to_s, 20.0)
    raise "Socat failed after 20 seconds" if res.to_s == ""
    yield block.call(res)
  end
end