Class: OpenC3::ScriptStatusModel

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

Constant Summary collapse

RUNNING_PRIMARY_KEY =

Note: ScriptRunner only has permissions for keys that start with running-script

'running-script'
COMPLETED_PRIMARY_KEY =
'running-script-completed'

Instance Attribute Summary collapse

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

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

Constructor Details

#initialize(name:, state:, shard: 0, filename:, current_filename: nil, line_no: 0, start_line_no: 1, end_line_no: nil, username:, user_full_name:, start_time:, end_time: nil, disconnect: false, environment: nil, suite_runner: nil, errors: nil, pid: nil, log: nil, report: nil, updated_at: nil, scope:) ⇒ ScriptStatusModel

Returns a new instance of ScriptStatusModel.



130
131
132
133
134
135
136
137
138
139
140
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
173
174
175
176
# File 'lib/openc3/models/script_status_model.rb', line 130

def initialize(
  name:, # id
  state:, # spawning, init, running, paused, waiting, error, breakpoint, crashed, stopped, completed, completed_errors, killed
  shard: 0, # Future enhancement of script runner shards
  filename:, # The initial filename
  current_filename: nil, # The current filename
  line_no: 0, # The current line number
  start_line_no: 1, # The line number to start the script at
  end_line_no: nil, # The line number to end the script at
  username:, # The username of the person who started the script
  user_full_name:, # The full name of the person who started the script
  start_time:, # The time the script started ISO format
  end_time: nil, # The time the script ended ISO format
  disconnect: false,
  environment: nil,
  suite_runner: nil,
  errors: nil,
  pid: nil,
  log: nil,
  report: nil,
  updated_at: nil,
  scope:
)
  @state = state
  if is_complete?()
    super("#{COMPLETED_PRIMARY_KEY}__#{scope}", name: name, updated_at: updated_at, plugin: nil, scope: scope)
  else
    super("#{RUNNING_PRIMARY_KEY}__#{scope}", name: name, updated_at: updated_at, plugin: nil, scope: scope)
  end
  @shard = shard.to_i
  @filename = filename
  @current_filename = current_filename
  @line_no = line_no
  @start_line_no = start_line_no
  @end_line_no = end_line_no
  @username = username
  @user_full_name = user_full_name
  @start_time = start_time
  @end_time = end_time
  @disconnect = disconnect
  @environment = environment
  @suite_runner = suite_runner
  @errors = errors
  @pid = pid
  @log = log
  @report = report
end

Instance Attribute Details

#current_filenameObject

Returns the value of attribute current_filename.



33
34
35
# File 'lib/openc3/models/script_status_model.rb', line 33

def current_filename
  @current_filename
end

#disconnectObject

Returns the value of attribute disconnect.



41
42
43
# File 'lib/openc3/models/script_status_model.rb', line 41

def disconnect
  @disconnect
end

#end_line_noObject

Returns the value of attribute end_line_no.



36
37
38
# File 'lib/openc3/models/script_status_model.rb', line 36

def end_line_no
  @end_line_no
end

#end_timeObject

Returns the value of attribute end_time.



40
41
42
# File 'lib/openc3/models/script_status_model.rb', line 40

def end_time
  @end_time
end

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#errorsObject

Returns the value of attribute errors.



44
45
46
# File 'lib/openc3/models/script_status_model.rb', line 44

def errors
  @errors
end

#filenameObject

Returns the value of attribute filename.



32
33
34
# File 'lib/openc3/models/script_status_model.rb', line 32

def filename
  @filename
end

#line_noObject

Returns the value of attribute line_no.



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

def line_no
  @line_no
end

#logObject

Returns the value of attribute log.



46
47
48
# File 'lib/openc3/models/script_status_model.rb', line 46

def log
  @log
end

#pidObject

Returns the value of attribute pid.



45
46
47
# File 'lib/openc3/models/script_status_model.rb', line 45

def pid
  @pid
end

#reportObject

Returns the value of attribute report.



47
48
49
# File 'lib/openc3/models/script_status_model.rb', line 47

def report
  @report
end

#shardObject

Returns the value of attribute shard.



31
32
33
# File 'lib/openc3/models/script_status_model.rb', line 31

def shard
  @shard
end

#start_line_noObject

Returns the value of attribute start_line_no.



35
36
37
# File 'lib/openc3/models/script_status_model.rb', line 35

def start_line_no
  @start_line_no
end

#start_timeObject

Returns the value of attribute start_time.



39
40
41
# File 'lib/openc3/models/script_status_model.rb', line 39

def start_time
  @start_time
end

#stateObject

spawning, init, running, paused, waiting, breakpoint, error, crashed, stopped, completed, completed_errors, killed



30
31
32
# File 'lib/openc3/models/script_status_model.rb', line 30

def state
  @state
end

#suite_runnerObject

Returns the value of attribute suite_runner.



43
44
45
# File 'lib/openc3/models/script_status_model.rb', line 43

def suite_runner
  @suite_runner
end

#user_full_nameObject

Returns the value of attribute user_full_name.



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

def user_full_name
  @user_full_name
end

#usernameObject

Returns the value of attribute username.



37
38
39
# File 'lib/openc3/models/script_status_model.rb', line 37

def username
  @username
end

Class Method Details

.all(scope:, offset: 0, limit: 10, type: "running") ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/openc3/models/script_status_model.rb', line 68

def self.all(scope:, offset: 0, limit: 10, type: "running")
  if type == "running"
    keys = self.store.zrevrange("#{RUNNING_PRIMARY_KEY}__#{scope}__LIST", offset.to_i, offset.to_i + limit.to_i - 1)
    return [] if keys.empty?
    result = []
    if $openc3_redis_cluster
      # No pipelining for cluster mode
      # because it requires using the same shard for all keys
      keys.each do |key|
        result << self.store.hget("#{RUNNING_PRIMARY_KEY}__#{scope}", key)
      end
    else
      result = self.store.redis_pool.pipelined do
        keys.each do |key|
          self.store.hget("#{RUNNING_PRIMARY_KEY}__#{scope}", key)
        end
      end
    end
    result = result.map do |r|
      if r.nil?
        nil
      else
        JSON.parse(r, :allow_nan => true, :create_additions => true)
      end
    end
    return result
  else
    keys = self.store.zrevrange("#{COMPLETED_PRIMARY_KEY}__#{scope}__LIST", offset.to_i, offset.to_i + limit.to_i - 1)
    return [] if keys.empty?
    result = []
    if $openc3_redis_cluster
      # No pipelining for cluster mode
      # because it requires using the same shard for all keys
      keys.each do |key|
        result << self.store.hget("#{COMPLETED_PRIMARY_KEY}__#{scope}", key)
      end
    else
      result = self.store.redis_pool.pipelined do
        keys.each do |key|
          self.store.hget("#{COMPLETED_PRIMARY_KEY}__#{scope}", key)
        end
      end
    end
    result = result.map do |r|
      if r.nil?
        nil
      else
        JSON.parse(r, :allow_nan => true, :create_additions => true)
      end
    end
    return result
  end
end

.count(scope:, type: "running") ⇒ Object



122
123
124
125
126
127
128
# File 'lib/openc3/models/script_status_model.rb', line 122

def self.count(scope:, type: "running")
  if type == "running"
    return self.store.zcount("#{RUNNING_PRIMARY_KEY}__#{scope}__LIST", 0, Float::INFINITY)
  else
    return self.store.zcount("#{COMPLETED_PRIMARY_KEY}__#{scope}__LIST", 0, Float::INFINITY)
  end
end

.get(name:, scope:, type: "auto") ⇒ Object

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



51
52
53
54
55
56
57
58
# File 'lib/openc3/models/script_status_model.rb', line 51

def self.get(name:, scope:, type: "auto")
  if type == "auto" or type == "running"
    # Check for running first
    running = super("#{RUNNING_PRIMARY_KEY}__#{scope}", name: name)
    return running if running
  end
  return super("#{COMPLETED_PRIMARY_KEY}__#{scope}", name: name)
end

.names(scope:, type: "running") ⇒ Object



60
61
62
63
64
65
66
# File 'lib/openc3/models/script_status_model.rb', line 60

def self.names(scope:, type: "running")
  if type == "running"
    return super("#{RUNNING_PRIMARY_KEY}__#{scope}")
  else
    return super("#{COMPLETED_PRIMARY_KEY}__#{scope}")
  end
end

Instance Method Details

#as_json(*a) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/openc3/models/script_status_model.rb', line 239

def as_json(*a)
  {
    'name' => @name,
    'state' => @state,
    'shard' => @shard,
    'filename' => @filename,
    'current_filename' => @current_filename,
    'line_no' => @line_no,
    'start_line_no' => @start_line_no,
    'end_line_no' => @end_line_no,
    'username' => @username,
    'user_full_name' => @user_full_name,
    'start_time' => @start_time,
    'end_time' => @end_time,
    'disconnect' => @disconnect,
    'environment' => @environment,
    'suite_runner' => @suite_runner,
    'errors' => @errors,
    'pid' => @pid,
    'log' => @log,
    'report' => @report,
    'updated_at' => @updated_at,
    'scope' => @scope
  }
end

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

Update the Redis hash at primary_key and set the field “name” to the JSON generated via calling as_json



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/openc3/models/script_status_model.rb', line 196

def create(update: false, force: false, queued: false, isoformat: true)
  @updated_at = Time.now.utc.to_nsec_from_epoch

  if queued
    write_store = self.class.store_queued
  else
    write_store = self.class.store
  end
  write_store.hset(@primary_key, @name, JSON.generate(self.as_json(:allow_nan => true), :allow_nan => true))

  # Also add to ordered set on create
  write_store.zadd(@primary_key + "__LIST", @name.to_i, @name) if not update
end

#destroy(queued: false) ⇒ Object

Delete the model from the Store



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/openc3/models/script_status_model.rb', line 226

def destroy(queued: false)
  @destroyed = true
  undeploy()
  if queued
    write_store = self.class.store_queued
  else
    write_store = self.class.store
  end
  write_store.hdel(@primary_key, @name)
  # Also remove from ordered set
  write_store.zremrangebyscore(@primary_key + "__LIST", @name.to_i, @name.to_i)
end

#idObject



27
28
29
# File 'lib/openc3/models/script_status_model.rb', line 27

def id
  return @name
end

#is_complete?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/openc3/models/script_status_model.rb', line 178

def is_complete?
  return (@state == 'completed' or @state == 'completed_errors' or @state == 'stopped' or @state == 'crashed' or @state == 'killed')
end

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



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

def update(force: false, queued: false)
  # Magically handle the change from running to completed
  if is_complete?() and @primary_key == "#{RUNNING_PRIMARY_KEY}__#{@scope}"
    # Destroy the running key
    destroy(queued: queued)
    @destroyed = false

    # Move to completed
    @primary_key = "#{COMPLETED_PRIMARY_KEY}__#{@scope}"
    create(update: false, force: force, queued: queued, isoformat: true)
  else
    create(update: true, force: force, queued: queued, isoformat: true)
  end
end