Method: OpenC3::PluginModel#undeploy

Defined in:
lib/openc3/models/plugin_model.rb

#undeployObject

Undeploy all models associated with this plugin



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/openc3/models/plugin_model.rb', line 326

def undeploy
  errors = []
  microservice_count = 0
  microservices = MicroserviceModel.find_all_by_plugin(plugin: @name, scope: @scope)
  microservices.each do |_name, model_instance|
    begin
      model_instance.destroy
    rescue Exception => e
      errors << e
    end
    microservice_count += 1
  end
  # Wait for the operator to wake up and remove the microservice processes
  sleep 15 if microservice_count > 0 # Cycle time 5s times 2 plus 5s wait for soft stop and then hard stop
  # Remove all the other models now that the processes have stopped
  # Save TargetModel for last as it has the most to cleanup
  [InterfaceModel, RouterModel, ToolModel, WidgetModel, TargetModel].each do |model|
    model.find_all_by_plugin(plugin: @name, scope: @scope).each do |_name, model_instance|
      begin
        model_instance.destroy
      rescue Exception => e
        errors << e
      end
    end
  end
  # Cleanup Redis stuff that might have been left by microservices
  microservices.each do |_name, model_instance|
    begin
      model_instance.cleanup
    rescue Exception => e
      errors << e
    end
  end
  # Raise all the errors at once
  if errors.length > 0
    message = ''
    errors.each do |error|
      message += "\n#{error.formatted}\n"
    end
    raise message
  end
rescue Exception => e
  Logger.error("Error undeploying plugin model #{@name} in scope #{@scope} due to: #{e.formatted}")
ensure
  # Double check everything is gone
  found = []
  [MicroserviceModel, InterfaceModel, RouterModel, ToolModel, WidgetModel, TargetModel].each do |model|
    model.find_all_by_plugin(plugin: @name, scope: @scope).each do |_name, model_instance|
      found << model_instance
    end
  end
  if found.length > 0
    # If undeploy failed we need to not move forward with anything else
    Logger.error("Error undeploying plugin model #{@name} in scope #{@scope} due to: Plugin submodels still exist after undeploy = #{found.length}")
    raise "Plugin #{@name} submodels still exist after undeploy = #{found.length}"
  end
end