Class: AllQ::Connection
- Inherits:
-
Object
- Object
- AllQ::Connection
- 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
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #call_socat(data, timeout = 3.0) ⇒ Object
-
#close ⇒ Object
Close connection with allq server.
-
#initialize(address = '') ⇒ Connection
constructor
Initializes new connection.
- #socat(command, options = {}) {|block.call(res)| ... } ⇒ Object
-
#to_s ⇒ Object
(also: #inspect)
Returns string representation of job.
-
#transmit(command, options = {}, &block) ⇒ Array<Hash{String => String, Number}>
Send commands to allq server via connection.
Constructor Details
#initialize(address = '') ⇒ Connection
Initializes new connection.
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
#address ⇒ Object (readonly)
Returns the value of attribute address.
17 18 19 |
# File 'lib/allq/connection.rb', line 17 def address @address end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
17 18 19 |
# File 'lib/allq/connection.rb', line 17 def connection @connection end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
17 18 19 |
# File 'lib/allq/connection.rb', line 17 def host @host end |
#port ⇒ Object (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 |
#close ⇒ Object
Close connection with allq server.
75 76 |
# File 'lib/allq/connection.rb', line 75 def close end |
#socat(command, options = {}) {|block.call(res)| ... } ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/allq/connection.rb', line 37 def socat(command, ={}, &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_s ⇒ Object Also known as: inspect
Returns string representation of job.
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.
62 63 64 65 66 67 68 |
# File 'lib/allq/connection.rb', line 62 def transmit(command, ={}, &block) _with_retry([:retry_interval], [: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 |