Class: HotCell::Request

Inherits:
Object
  • Object
show all
Extended by:
Fields
Defined in:
lib/hot_cell/request.rb

Overview

One line of UTF-8 JSON terminated by a newline, sent with a single sendmsg carrying one SCM_RIGHTS message.

{"v":1,"op":"active_storage.transform_image","inputs":1,"outputs":1,"payload":{"format":"png"}}

Inputs are the leading descriptors and outputs the trailing ones, so two counts are the whole framing and there is no naming layer to keep in step with an operation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op:, inputs: 0, outputs: 0, payload: {}, version: PROTOCOL_VERSION) ⇒ Request

Returns a new instance of Request.



14
15
16
17
18
19
20
21
22
# File 'lib/hot_cell/request.rb', line 14

def initialize(op:, inputs: 0, outputs: 0, payload: {}, version: PROTOCOL_VERSION)
  @version = version
  @op = op
  @inputs = inputs
  @outputs = outputs
  @payload = payload

  verify_descriptor_count!
end

Instance Attribute Details

#inputsObject (readonly)

Returns the value of attribute inputs.



12
13
14
# File 'lib/hot_cell/request.rb', line 12

def inputs
  @inputs
end

#opObject (readonly)

Returns the value of attribute op.



12
13
14
# File 'lib/hot_cell/request.rb', line 12

def op
  @op
end

#outputsObject (readonly)

Returns the value of attribute outputs.



12
13
14
# File 'lib/hot_cell/request.rb', line 12

def outputs
  @outputs
end

#payloadObject (readonly)

Returns the value of attribute payload.



12
13
14
# File 'lib/hot_cell/request.rb', line 12

def payload
  @payload
end

#versionObject (readonly)

Returns the value of attribute version.



12
13
14
# File 'lib/hot_cell/request.rb', line 12

def version
  @version
end

Class Method Details

.nounObject



52
53
54
# File 'lib/hot_cell/request.rb', line 52

def noun
  "request"
end

.parse(line) ⇒ Object



56
57
58
59
60
61
# File 'lib/hot_cell/request.rb', line 56

def parse(line)
  parse_message(line) do |parsed|
    new version: integer(parsed, :v), op: name(parsed, :op), inputs: count(parsed, :inputs),
        outputs: count(parsed, :outputs), payload: object(parsed, :payload)
  end
end

Instance Method Details

#current_version?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/hot_cell/request.rb', line 28

def current_version?
  version == PROTOCOL_VERSION
end

#descriptor_countObject



24
25
26
# File 'lib/hot_cell/request.rb', line 24

def descriptor_count
  inputs + outputs
end

#to_lineObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/hot_cell/request.rb', line 38

def to_line
  Payload.validate! payload, "payload"

  line = JSON.generate({ v: version, op: op, inputs: inputs, outputs: outputs, payload: payload }) << "\n"
  if line.bytesize > MAX_REQUEST_BYTES
    raise MessageError, "request is #{line.bytesize} bytes, over the #{MAX_REQUEST_BYTES} byte limit"
  end

  line
end

#version_mismatchObject

Next to the predicate it explains, because both sockets answer with it and a caller may well be grepping for it — the one failure that arrives at a hundred percent during a rolling deploy.



34
35
36
# File 'lib/hot_cell/request.rb', line 34

def version_mismatch
  "this cell speaks v#{PROTOCOL_VERSION} and the request is v#{version}"
end