Class: OpenC3::QueueModel

Inherits:
Model show all
Defined in:
lib/openc3/models/queue_model.rb

Constant Summary collapse

PRIMARY_KEY =
'openc3__queue'.freeze
@@class_mutex =
Mutex.new

Instance Attribute Summary collapse

Attributes inherited from Model

#plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#check_disable_erb, #destroyed?, filter, find_all_by_plugin, from_json, get_all_models, get_model, handle_config, set, store, store_queued, #update

Constructor Details

#initialize(name:, scope:, state: 'HOLD', updated_at: nil) ⇒ QueueModel

Returns a new instance of QueueModel.



67
68
69
70
71
72
73
74
75
# File 'lib/openc3/models/queue_model.rb', line 67

def initialize(name:, scope:, state: 'HOLD', updated_at: nil)
  super("#{scope}__#{PRIMARY_KEY}", name: name, updated_at: updated_at, scope: scope)
  @microservice_name = "#{scope}__QUEUE__#{name}"
  if %w(HOLD RELEASE DISABLE).include?(state)
    @state = state
  else
    @state = 'HOLD'
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



65
66
67
# File 'lib/openc3/models/queue_model.rb', line 65

def name
  @name
end

#stateObject

Returns the value of attribute state.



65
66
67
# File 'lib/openc3/models/queue_model.rb', line 65

def state
  @state
end

Class Method Details

.all(scope:) ⇒ Object



42
43
44
# File 'lib/openc3/models/queue_model.rb', line 42

def self.all(scope:)
  super("#{scope}__#{PRIMARY_KEY}")
end

.get(name:, scope:) ⇒ Object

NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work



34
35
36
# File 'lib/openc3/models/queue_model.rb', line 34

def self.get(name:, scope:)
  super("#{scope}__#{PRIMARY_KEY}", name: name)
end

.names(scope:) ⇒ Object



38
39
40
# File 'lib/openc3/models/queue_model.rb', line 38

def self.names(scope:)
  super("#{scope}__#{PRIMARY_KEY}")
end

.queue_command(name, command:, username:, scope:) ⇒ Object

END NOTE

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openc3/models/queue_model.rb', line 47

def self.queue_command(name, command:, username:, scope:)
  model = get_model(name: name, scope: scope)
  raise QueueError, "Queue '#{name}' not found in scope '#{scope}'" unless model

  if model.state != 'DISABLE'
    result = Store.zrevrange("#{scope}:#{name}", 0, 0, with_scores: true)
    if result.empty?
      index = 1.0
    else
      index = result[0][1].to_f + 1
    end
    Store.zadd("#{scope}:#{name}", index, { username: username, value: command, timestamp: Time.now.to_nsec_from_epoch }.to_json)
    model.notify(kind: 'command')
  else
    raise QueueError, "Queue '#{name}' is disabled. Command '#{command}' not queued."
  end
end

Instance Method Details

#as_json(*a) ⇒ Hash

Returns generated from the QueueModel.

Returns:

  • (Hash)

    generated from the QueueModel



88
89
90
91
92
93
94
95
# File 'lib/openc3/models/queue_model.rb', line 88

def as_json(*a)
  return {
    'name' => @name,
    'scope' => @scope,
    'state' => @state,
    'updated_at' => @updated_at
  }
end

#create(update: false, force: false, queued: false) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/openc3/models/queue_model.rb', line 77

def create(update: false, force: false, queued: false)
  super(update: update, force: force, queued: queued)
  if update
    notify(kind: 'updated')
  else
    deploy()
    notify(kind: 'created')
  end
end

#create_microservice(topics:) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/openc3/models/queue_model.rb', line 182

def create_microservice(topics:)
  # queue Microservice
  microservice = MicroserviceModel.new(
    name: @microservice_name,
    folder_name: nil,
    cmd: ['ruby', 'queue_microservice.rb', @microservice_name],
    work_dir: '/openc3/lib/openc3/microservices',
    options: [
      ["QUEUE_STATE", @state],
    ],
    topics: topics,
    target_names: [],
    plugin: nil,
    scope: @scope
  )
  microservice.create
end

#deployObject



200
201
202
203
204
205
# File 'lib/openc3/models/queue_model.rb', line 200

def deploy
  topics = ["#{@scope}__#{QueueTopic::PRIMARY_KEY}"]
  if MicroserviceModel.get_model(name: @microservice_name, scope: @scope).nil?
    create_microservice(topics: topics)
  end
end

#destroyObject

Delete the model from the Store



226
227
228
229
230
# File 'lib/openc3/models/queue_model.rb', line 226

def destroy
  undeploy()
  Store.zremrangebyrank("#{@scope}:#{@name}", 0, -1)
  super()
end

#insert_command(index, command_data) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/openc3/models/queue_model.rb', line 106

def insert_command(index, command_data)
  if @state == 'DISABLE'
    raise QueueError, "Queue '#{@name}' is disabled. Command '#{command_data['value']}' not queued."
  end

  unless index
    result = Store.zrevrange("#{@scope}:#{@name}", 0, 0, with_scores: true)
    if result.empty?
      index = 1.0
    else
      index = result[0][1].to_f + 1
    end
  end
  Store.zadd("#{@scope}:#{@name}", index, command_data.to_json)
  notify(kind: 'command')
end

#listObject



174
175
176
177
178
179
180
# File 'lib/openc3/models/queue_model.rb', line 174

def list
  return Store.zrange("#{@scope}:#{@name}", 0, -1, with_scores: true).map do |item|
    result = JSON.parse(item[0])
    result['index'] = item[1].to_f
    result
  end
end

#notify(kind:) ⇒ Object

Returns [] update the redis stream / queue topic that something has changed.

Returns:

  • update the redis stream / queue topic that something has changed



98
99
100
101
102
103
104
# File 'lib/openc3/models/queue_model.rb', line 98

def notify(kind:)
  notification = {
    'kind' => kind,
    'data' => JSON.generate(as_json(:allow_nan => true)),
  }
  QueueTopic.write_notification(notification, scope: @scope)
end

#remove_command(index = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/openc3/models/queue_model.rb', line 141

def remove_command(index = nil)
  if @state == 'DISABLE'
    raise QueueError, "Queue '#{@name}' is disabled. Command not removed."
  end

  if index
    # Remove specific index
    result = Store.zrangebyscore("#{@scope}:#{@name}", index, index)
    if result.empty?
      return nil
    else
      Store.zremrangebyscore("#{@scope}:#{@name}", index, index)
      command_data = JSON.parse(result[0])
      command_data['index'] = index.to_f
      notify(kind: 'command')
      return command_data
    end
  else
    # Remove first element (lowest score)
    result = Store.zrange("#{@scope}:#{@name}", 0, 0, with_scores: true)
    if result.empty?
      return nil
    else
      score = result[0][1]
      Store.zremrangebyscore("#{@scope}:#{@name}", score, score)
      command_data = JSON.parse(result[0][0])
      command_data['index'] = score.to_f
      notify(kind: 'command')
      return command_data
    end
  end
end

#undeployObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/openc3/models/queue_model.rb', line 207

def undeploy
  model = MicroserviceModel.get_model(name: @microservice_name, scope: @scope)
  if model
    # Let the frontend know that the microservice is shutting down
    # Custom event which matches the 'deployed' event in QueueMicroservice
    notification = {
      'kind' => 'undeployed',
      # name and updated_at fields are required for Event formatting
      'data' => JSON.generate({
        'name' => @microservice_name,
        'updated_at' => Time.now.to_nsec_from_epoch,
      }),
    }
    QueueTopic.write_notification(notification, scope: @scope)
    model.destroy
  end
end

#update_command(index:, command:, username:) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/openc3/models/queue_model.rb', line 123

def update_command(index:, command:, username:)
  if @state == 'DISABLE'
    raise QueueError, "Queue '#{@name}' is disabled. Command at index #{index} not updated."
  end

  # Check if command exists at the given index
  existing = Store.zrangebyscore("#{@scope}:#{@name}", index, index)
  if existing.empty?
    raise QueueError, "No command found at index #{index} in queue '#{@name}'"
  end

  # Remove the existing command and add the new one at the same index
  Store.zremrangebyscore("#{@scope}:#{@name}", index, index)
  command_data = { username: username, value: command, timestamp: Time.now.to_nsec_from_epoch }
  Store.zadd("#{@scope}:#{@name}", index, command_data.to_json)
  notify(kind: 'command')
end