Class: Cotcube::Helpers::OrderClient
- Inherits:
-
Object
- Object
- Cotcube::Helpers::OrderClient
- Defined in:
- lib/cotcube-helpers/order_client.rb
Constant Summary collapse
- SECRETS_DEFAULT =
{ 'orderproxy_mq_proto' => 'http', 'orderproxy_mq_user' => 'guest', 'orderproxy_mq_password' => 'guest', 'orderproxy_mq_host' => 'localhost', 'orderproxy_mq_port' => '15672', 'orderproxy_mq_vhost' => '%2F' }.freeze
- SECRETS =
SECRETS_DEFAULT.merge( lambda { begin YAML.safe_load(File.read(Cotcube::Helpers.init[:secrets_file])) rescue StandardError {} end }.call )
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#lock ⇒ Object
readonly
Returns the value of attribute lock.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
-
#command(command, timeout: 10) ⇒ Object
(also: #send_command)
command acts a synchronizer: it sends the command and waits for the response otherwise times out — the counterpart here is the subscription within setup_reply_queue.
- #get_contracts(symbol:) ⇒ Object
-
#initialize ⇒ OrderClient
constructor
A new instance of OrderClient.
- #stop ⇒ Object
Constructor Details
#initialize ⇒ OrderClient
Returns a new instance of OrderClient.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cotcube-helpers/order_client.rb', line 29 def initialize @connection = Bunny.new(user: SECRETS['orderproxy_mq_user'], password: SECRETS['orderproxy_mq_password'], vhost: SECRETS['orderproxy_mq_vhost']) @connection.start @commands = connection.create_channel @exchange = commands.direct('orderproxy_commands') @requests = {} @persistent = { depth: {}, realtimebars: {}, ticks: {} } @debug = false setup_reply_queue end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
87 88 89 |
# File 'lib/cotcube-helpers/order_client.rb', line 87 def condition @condition end |
#lock ⇒ Object (readonly)
Returns the value of attribute lock.
87 88 89 |
# File 'lib/cotcube-helpers/order_client.rb', line 87 def lock @lock end |
#response ⇒ Object
Returns the value of attribute response.
86 87 88 |
# File 'lib/cotcube-helpers/order_client.rb', line 86 def response @response end |
Instance Method Details
#command(command, timeout: 10) ⇒ Object Also known as: send_command
command acts a synchronizer: it sends the command and waits for the response
otherwise times out --- the counterpart here is the subscription within
setup_reply_queue
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cotcube-helpers/order_client.rb', line 47 def command(command, timeout: 10) command = { command: command.to_s } unless command.is_a? Hash command[:timestamp] ||= (Time.now.to_f * 1000).to_i request_id = Digest::SHA256.hexdigest(command.to_json)[..6] requests[request_id] = { request: command, id: request_id, lock: Mutex.new, condition: ConditionVariable.new } exchange.publish(command.to_json, routing_key: 'orderproxy_commands', correlation_id: request_id, reply_to: reply_queue.name) # wait for the signal to continue the execution # requests[request_id][:lock].synchronize do requests[request_id][:condition].wait(requests[request_id][:lock], timeout) end # if we reached timeout, we will return nil, just for explicity response = requests[request_id][:response].dup requests.delete(request_id) response end |
#get_contracts(symbol:) ⇒ Object
82 83 84 |
# File 'lib/cotcube-helpers/order_client.rb', line 82 def get_contracts(symbol:) send_command({ command: :get_contracts, symbol: symbol }) end |
#stop ⇒ Object
77 78 79 80 |
# File 'lib/cotcube-helpers/order_client.rb', line 77 def stop commands.close connection.close end |