Method: OpenC3::Microservice#initialize
- Defined in:
- lib/openc3/microservices/microservice.rb
#initialize(name, is_plugin: false) ⇒ Microservice
Returns a new instance of Microservice.
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 121 122 123 124 125 126 127 128 129 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/openc3/microservices/microservice.rb', line 81 def initialize(name, is_plugin: false) Logger.info("Microservice running from: ruby #{$0} #{ARGV.join(" ")}") raise "Microservice must be named" unless name @name = name split_name = name.split("__") raise "Name #{name} doesn't match convention of SCOPE__TYPE__NAME" if split_name.length != 3 @scope = split_name[0] $openc3_scope = @scope @cancel_thread = false @metric = Metric.new(microservice: @name, scope: @scope) Logger.scope = @scope Logger.microservice_name = @name @logger = Logger.new @logger.scope = @scope @logger.microservice_name = @name @secrets = Secrets.getClient OpenC3.setup_open_telemetry(@name, false) # Create temp folder for this microservice @temp_dir = Dir.mktmpdir # Get microservice configuration from Redis @config = MicroserviceModel.get(name: @name, scope: @scope) if @config @topics = @config['topics'] @plugin = @config['plugin'] if @config['secrets'] @secrets.setup(@config['secrets']) end else @config = {} @plugin = nil end @logger.info("Microservice initialized with config:\n#{@config}") @topics ||= [] # Get configuration for any targets @target_names = @config["target_names"] @target_names ||= [] System.setup_targets(@target_names, @temp_dir, scope: @scope) unless is_plugin # Use at_exit to shutdown cleanly no matter how we die at_exit do shutdown() end @count = 0 @error = nil @custom = nil @state = 'INITIALIZED' metric_name = "metric_output_duration_seconds" @work_dir = @config["work_dir"] if is_plugin cmd_array = @config["cmd"] # Get Microservice files from bucket storage temp_dir = Dir.mktmpdir bucket = ENV['OPENC3_CONFIG_BUCKET'] client = Bucket.getClient() prefix = "#{@scope}/microservices/#{@name}/" file_count = 0 client.list_objects(bucket: bucket, prefix: prefix).each do |object| response_target = File.join(temp_dir, object.key.split(prefix)[-1]) FileUtils.mkdir_p(File.dirname(response_target)) client.get_object(bucket: bucket, key: object.key, path: response_target) file_count += 1 end # Adjust @work_dir to microservice files downloaded if files and a relative path if file_count > 0 and @work_dir[0] != '/' @work_dir = File.join(temp_dir, @work_dir) end # Check Syntax on any ruby files ruby_filename = nil cmd_array.each do |part| if /\.rb$/.match?(part) ruby_filename = part break end end if ruby_filename OpenC3.set_working_dir(@work_dir) do if File.exist?(ruby_filename) # Run ruby syntax so we can log those syntax_check, _ = Open3.capture2e("ruby -c #{ruby_filename}") if /Syntax OK/.match?(syntax_check) @logger.info("Ruby microservice #{@name} file #{ruby_filename} passed syntax check\n", scope: @scope) else @logger.error("Ruby microservice #{@name} file #{ruby_filename} failed syntax check\n#{syntax_check}", scope: @scope) end else @logger.error("Ruby microservice #{@name} file #{ruby_filename} does not exist", scope: @scope) end end end else @microservice_sleeper = Sleeper.new @microservice_status_period_seconds = 5 @microservice_status_thread = Thread.new do until @cancel_thread start = Process.clock_gettime(Process::CLOCK_MONOTONIC) @metric.output diff = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start # seconds as a float @metric.add_sample(name: metric_name, value: diff, labels: {}) MicroserviceStatusModel.set(as_json(:allow_nan => true), scope: @scope) unless @cancel_thread break if @microservice_sleeper.sleep(@microservice_status_period_seconds) end rescue Exception => err @logger.error "#{@name} status thread died: #{err.formatted}" raise err end end end |