Class: OpenC3::MicroserviceOperator
- Defined in:
- lib/openc3/operators/microservice_operator.rb
Overview
Creates new OperatorProcess objects based on querying the Redis key value store. Any keys under 'openc3_microservices' will be created into microservices.
Constant Summary
Constants inherited from Operator
Operator::CYCLE_TIME, Operator::PROCESS_SHUTDOWN_SECONDS
Instance Attribute Summary
Attributes inherited from Operator
Instance Method Summary collapse
- #convert_microservice_to_process_definition(microservice_name, microservice_config) ⇒ Object
-
#handle_changed_microservice(microservice_name, microservice_config) ⇒ Object
Handle a change detected in a microservice model.
-
#handle_new_microservice(microservice_name, microservice_config) ⇒ Object
Handle the detection of a new microservice model.
-
#handle_removed_microservice(microservice_name, microservice_config) ⇒ Object
Handle the detection of a removed microservice model.
-
#initialize ⇒ MicroserviceOperator
constructor
A new instance of MicroserviceOperator.
- #update ⇒ Object
Methods inherited from Operator
instance, processes, #remove_old, #respawn_changed, #respawn_dead, #run, run, #shutdown, #shutdown_processes, #start_new, #stop
Constructor Details
#initialize ⇒ MicroserviceOperator
Returns a new instance of MicroserviceOperator.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/openc3/operators/microservice_operator.rb', line 30 def initialize Logger.microservice_name = "MicroserviceOperator" super @secrets = Secrets.getClient @microservices = {} @previous_microservices = {} @new_microservices = {} @changed_microservices = {} @removed_microservices = {} @shard = ENV['OPENC3_SHARD'] || 0 @shard = @shard.to_i end |
Instance Method Details
#convert_microservice_to_process_definition(microservice_name, microservice_config) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 |
# File 'lib/openc3/operators/microservice_operator.rb', line 44 def convert_microservice_to_process_definition(microservice_name, microservice_config) process_definition = ["ruby", "plugin_microservice.rb"] work_dir = "/openc3/lib/openc3/microservices" env = microservice_config["env"].dup if microservice_config["needs_dependencies"] env['GEM_HOME'] = '/gems' # Resolve the Python virtual environment for this microservice. # If the microservice belongs to a plugin that has a per-plugin UV venv # (created by uvinstall during plugin install), use that isolated venv. # Otherwise fall back to the shared PYTHONUSERBASE for legacy plugins. plugin_name = microservice_config["plugin"] plugin_venv_dir = nil if plugin_name scope = microservice_name.split("__")[0] sanitized_name = "#{scope}__#{plugin_name}".tr('^a-zA-Z0-9_-', '_') candidate = "/gems/plugin_venvs/#{sanitized_name}/.venv" plugin_venv_dir = candidate if File.directory?(candidate) end if plugin_venv_dir env['VIRTUAL_ENV'] = plugin_venv_dir env['PATH'] = "#{plugin_venv_dir}/bin:#{ENV.fetch('PATH', '')}" env['PYTHONUSERBASE'] = plugin_venv_dir # Add the plugin venv's site-packages to PYTHONPATH so the base venv's # Python binary can find plugin-specific packages. We keep the base binary # because it has openc3 core installed; PYTHONPATH is always respected by # CPython regardless of venv activation state. site_packages = Dir.glob("#{plugin_venv_dir}/lib/python*/site-packages").first existing_pythonpath = ENV.fetch('PYTHONPATH', '') if site_packages env['PYTHONPATH'] = existing_pythonpath.empty? ? site_packages : "#{site_packages}:#{existing_pythonpath}" else env['PYTHONPATH'] = existing_pythonpath.empty? ? nil : existing_pythonpath end else env['PYTHONUSERBASE'] = '/gems/python_packages' env['PYTHONPATH'] = ENV.fetch('PYTHONPATH', nil) end else env['GEM_HOME'] = nil env['PYTHONUSERBASE'] = nil env['PYTHONPATH'] = nil end env['OPENC3_MICROSERVICE_NAME'] = microservice_name container = microservice_config["container"] scope = microservice_name.split("__")[0] # Setup secrets for microservice secrets = microservice_config["secrets"] if secrets secrets.each do |type, secret_name, env_name_or_path, secret_store| secret_value = @secrets.get(secret_name, secret_store: secret_store, scope: scope) if secret_value if type == 'ENV' env[env_name_or_path] = secret_value elsif type == 'FILE' FileUtils.mkdir_p(File.dirname(env_name_or_path)) File.open(env_name_or_path, 'wb') do |file| file.write(secret_value) end end else Logger.error("Microservice #{microservice_name} references unknown secret: #{secret_name}") end end end return process_definition, work_dir, env, scope, container end |
#handle_changed_microservice(microservice_name, microservice_config) ⇒ Object
Handle a change detected in a microservice model
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 177 178 179 180 181 182 183 |
# File 'lib/openc3/operators/microservice_operator.rb', line 137 def handle_changed_microservice(microservice_name, microservice_config) parent = microservice_config['parent'] enabled = microservice_config['enabled'] enabled = true if enabled.nil? scope = microservice_name.split("__")[0] previous_parent = @previous_microservices[microservice_name]['parent'] previous_enabled = @previous_microservices[microservice_name]["enabled"] previous_enabled = true if previous_enabled.nil? Logger.info("Changed microservice detected: #{microservice_name}\nWas: #{@previous_microservices[microservice_name]}\nIs: #{microservice_config}", scope: scope) if parent or previous_parent if parent == previous_parent # Same Parent - Respawn parent @changed_microservices[parent] = @microservices[parent] if @microservices[parent] and @previous_microservices[parent] elsif parent and previous_parent # Parent changed - Respawn both parents @changed_microservices[parent] = @microservices[parent] if @microservices[parent] and @previous_microservices[parent] @changed_microservices[previous_parent] = @microservices[previous_parent] if @microservices[previous_parent] and @previous_microservices[previous_parent] elsif parent # Moved under a parent - Respawn parent and kill standalone (if previously enabled) @changed_microservices[parent] = @microservices[parent] if @microservices[parent] and @previous_microservices[parent] if previous_enabled @removed_microservices[microservice_name] = microservice_config end else # previous_parent # Moved to standalone - Respawn previous parent and make new (if enabled) @changed_microservices[previous_parent] = @microservices[previous_parent] if @microservices[previous_parent] and @previous_microservices[previous_parent] if enabled @new_microservices[microservice_name] = microservice_config end end else if previous_enabled if enabled # Respawn regular microservice @changed_microservices[microservice_name] = microservice_config else # Remove regular microservice @removed_microservices[microservice_name] = microservice_config end else # Newly enabled microservice @new_microservices[microservice_name] = microservice_config end end end |
#handle_new_microservice(microservice_name, microservice_config) ⇒ Object
Handle the detection of a new microservice model
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/openc3/operators/microservice_operator.rb', line 116 def handle_new_microservice(microservice_name, microservice_config) parent = microservice_config['parent'] enabled = microservice_config['enabled'] enabled = true if enabled.nil? scope = microservice_name.split("__")[0] if enabled Logger.info("New microservice detected: #{microservice_name}", scope: scope) if parent # Respawn parent if it exists and isn't new if @microservices[parent] and @previous_microservices[parent] @changed_microservices[parent] = @microservices[parent] end else # New process be spawned @new_microservices[microservice_name] = microservice_config end end end |
#handle_removed_microservice(microservice_name, microservice_config) ⇒ Object
Handle the detection of a removed microservice model
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/openc3/operators/microservice_operator.rb', line 186 def handle_removed_microservice(microservice_name, microservice_config) previous_parent = @previous_microservices[microservice_name]['parent'] scope = microservice_name.split("__")[0] Logger.info("Removed microservice detected: #{microservice_name}", scope: scope) if previous_parent # Respawn previous parent @changed_microservices[previous_parent] = @microservices[previous_parent] if @microservices[previous_parent] and @previous_microservices[previous_parent] else previous_enabled = @previous_microservices[microservice_name]["enabled"] previous_enabled = true if previous_enabled.nil? if previous_enabled # Regular process to be removed @removed_microservices[microservice_name] = microservice_config end end end |
#update ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/openc3/operators/microservice_operator.rb', line 205 def update @previous_microservices = @microservices.dup # Get all the microservice configuration @microservices = MicroserviceModel.all # Filter to just this shard @microservices = @microservices.select do |microservice_name, microservice_config| microservice_shard = microservice_config['shard'] || 0 microservice_shard == @shard end # Detect new and changed microservices @new_microservices = {} @changed_microservices = {} @removed_microservices = {} @microservices.each do |microservice_name, microservice_config| if @previous_microservices[microservice_name] if @previous_microservices[microservice_name] != microservice_config if not microservice_config['ignore_changes'] handle_changed_microservice(microservice_name, microservice_config) end end else handle_new_microservice(microservice_name, microservice_config) end end # Detect removed microservices @previous_microservices.each do |microservice_name, microservice_config| unless @microservices[microservice_name] handle_removed_microservice(microservice_name, microservice_config) end end # Convert to processes @mutex.synchronize do @new_microservices.each do |microservice_name, microservice_config| cmd_array, work_dir, env, scope, container = convert_microservice_to_process_definition(microservice_name, microservice_config) if cmd_array process = OperatorProcess.new(cmd_array, work_dir: work_dir, env: env, scope: scope, container: container, config: microservice_config) @new_processes[microservice_name] = process @processes[microservice_name] = process end end @changed_microservices.each do |microservice_name, microservice_config| cmd_array, work_dir, env, scope, container = convert_microservice_to_process_definition(microservice_name, microservice_config) if cmd_array process = @processes[microservice_name] if process process.process_definition = cmd_array process.work_dir = work_dir process.new_temp_dir = nil process.env = env @changed_processes[microservice_name] = process else # This shouldn't be possible, but still needs to be handled Logger.error("Changed microservice #{microservice_name} does not exist. Creating new...", scope: scope) process = OperatorProcess.new(cmd_array, work_dir: work_dir, env: env, scope: scope, container: container, config: microservice_config) @new_processes[microservice_name] = process @processes[microservice_name] = process end end end @removed_microservices.each do |microservice_name, _microservice_config| process = @processes[microservice_name] @processes.delete(microservice_name) @removed_processes[microservice_name] = process end end end |