Method: Cosmos::Microservice#initialize
- Defined in:
- lib/cosmos/microservices/microservice.rb
#initialize(name, is_plugin: false) ⇒ Microservice
Returns a new instance of Microservice.
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 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 |
# File 'lib/cosmos/microservices/microservice.rb', line 73 def initialize(name, is_plugin: false) 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] $cosmos_scope = @scope Logger.scope = @scope @cancel_thread = false @metric = Metric.new(microservice: @name, scope: @scope) Logger.microservice_name = @name Logger.tag = @name + "__cosmos.log" # 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'] else @config = {} @plugin = nil end Logger.info("Microservice initialized with config:\n#{@config}") @topics ||= [] # Get configuration for any targets from Minio/S3 @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" if is_plugin @work_dir = @config["work_dir"] cmd_array = @config["cmd"] # Get Microservice files from S3 temp_dir = Dir.mktmpdir rubys3_client = Aws::S3::Client.new bucket = "config" # Ensure config bucket exists begin rubys3_client.head_bucket(bucket: bucket) rescue Aws::S3::Errors::NotFound rubys3_client.create_bucket(bucket: bucket) end prefix = "#{@scope}/microservices/#{@name}/" file_count = 0 rubys3_client.list_objects(bucket: bucket, prefix: prefix).contents.each do |object| response_target = File.join(temp_dir, object.key.split(prefix)[-1]) FileUtils.mkdir_p(File.dirname(response_target)) rubys3_client.get_object(bucket: bucket, key: object.key, response_target: 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 Cosmos.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(), 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 |