Class: Eywa::Client
- Inherits:
-
Object
- Object
- Eywa::Client
- Defined in:
- lib/eywa.rb
Instance Method Summary collapse
- #close_task(status = SUCCESS) ⇒ Object
- #debug(message, data = nil) ⇒ Object
- #error(message, data = nil) ⇒ Object
- #exception(message, data = nil) ⇒ Object
- #get_task ⇒ Object
- #graphql(query, variables = nil) ⇒ Object
- #info(message, data = nil) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #log(event: 'INFO', message:, data: nil, duration: nil, coordinates: nil, time: Time.now) ⇒ Object
- #open_pipe ⇒ Object
- #register_handler(method, &handler) ⇒ Object
- #report(message, data = nil, image = nil) ⇒ Object
- #return_task ⇒ Object
- #send_notification(data) ⇒ Object
- #send_request(data) ⇒ Object
- #trace(message, data = nil) ⇒ Object
- #update_task(status = PROCESSING) ⇒ Object
- #warn(message, data = nil) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 |
# File 'lib/eywa.rb', line 16 def initialize @rpc_callbacks = {} @handlers = {} @mutex = Mutex.new end |
Instance Method Details
#close_task(status = SUCCESS) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/eywa.rb', line 129 def close_task(status = SUCCESS) send_notification( 'method' => 'task.close', 'params' => { 'status' => status } ) exit(status == SUCCESS ? 0 : 1) end |
#debug(message, data = nil) ⇒ Object
106 107 108 |
# File 'lib/eywa.rb', line 106 def debug(, data = nil) log(event: 'DEBUG', message: , data: data) end |
#error(message, data = nil) ⇒ Object
98 99 100 |
# File 'lib/eywa.rb', line 98 def error(, data = nil) log(event: 'ERROR', message: , data: data) end |
#exception(message, data = nil) ⇒ Object
114 115 116 |
# File 'lib/eywa.rb', line 114 def exception(, data = nil) log(event: 'EXCEPTION', message: , data: data) end |
#get_task ⇒ Object
149 150 151 |
# File 'lib/eywa.rb', line 149 def get_task send_request('method' => 'task.get') end |
#graphql(query, variables = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/eywa.rb', line 158 def graphql(query, variables = nil) send_request( 'method' => 'eywa.datasets.graphql', 'params' => { 'query' => query, 'variables' => variables } ) end |
#info(message, data = nil) ⇒ Object
94 95 96 |
# File 'lib/eywa.rb', line 94 def info(, data = nil) log(event: 'INFO', message: , data: data) end |
#log(event: 'INFO', message:, data: nil, duration: nil, coordinates: nil, time: Time.now) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/eywa.rb', line 80 def log(event: 'INFO', message:, data: nil, duration: nil, coordinates: nil, time: Time.now) send_notification( 'method' => 'task.log', 'params' => { 'time' => time.iso8601, 'event' => event, 'message' => , 'data' => data, 'coordinates' => coordinates, 'duration' => duration } ) end |
#open_pipe ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/eywa.rb', line 22 def open_pipe Thread.new do while (line = STDIN.gets) begin json = JSON.parse(line) handle_data(json) rescue JSON::ParserError => e STDERR.puts("Failed to parse JSON: #{e.message}") rescue => e STDERR.puts("Error handling data: #{e.message}") end end end end |
#register_handler(method, &handler) ⇒ Object
74 75 76 77 78 |
# File 'lib/eywa.rb', line 74 def register_handler(method, &handler) @mutex.synchronize do @handlers[method] = handler end end |
#report(message, data = nil, image = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/eywa.rb', line 118 def report(, data = nil, image = nil) send_notification( 'method' => 'task.report', 'params' => { 'message' => , 'data' => data, 'image' => image } ) end |
#return_task ⇒ Object
153 154 155 156 |
# File 'lib/eywa.rb', line 153 def return_task send_notification('method' => 'task.return') exit(0) end |
#send_notification(data) ⇒ Object
68 69 70 71 72 |
# File 'lib/eywa.rb', line 68 def send_notification(data) data['jsonrpc'] = '2.0' STDOUT.puts(data.to_json) STDOUT.flush end |
#send_request(data) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/eywa.rb', line 37 def send_request(data) id = SecureRandom.uuid data['jsonrpc'] = '2.0' data['id'] = id # Create a queue for this request queue = Queue.new @mutex.synchronize do @rpc_callbacks[id] = queue end # Send the request STDOUT.puts(data.to_json) STDOUT.flush # Wait for response in a thread Thread.new do response = queue.pop @mutex.synchronize do @rpc_callbacks.delete(id) end if response['error'] raise StandardError.new(response['error']['message'] || response['error'].to_s) else response['result'] end end end |
#trace(message, data = nil) ⇒ Object
110 111 112 |
# File 'lib/eywa.rb', line 110 def trace(, data = nil) log(event: 'TRACE', message: , data: data) end |
#update_task(status = PROCESSING) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/eywa.rb', line 140 def update_task(status = PROCESSING) send_notification( 'method' => 'task.update', 'params' => { 'status' => status } ) end |
#warn(message, data = nil) ⇒ Object
102 103 104 |
# File 'lib/eywa.rb', line 102 def warn(, data = nil) log(event: 'WARN', message: , data: data) end |