Class: Cosmos::Microservice
- Defined in:
- lib/cosmos/microservices/microservice.rb
Direct Known Subclasses
CleanupMicroservice, DecomMicroservice, InterfaceMicroservice, LogMicroservice, PluginMicroservice, ReactionMicroservice, ReducerMicroservice, TextLogMicroservice, TimelineMicroservice, TriggerGroupMicroservice
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#error ⇒ Object
Returns the value of attribute error.
-
#microservice_status_thread ⇒ Object
Returns the value of attribute microservice_status_thread.
-
#name ⇒ Object
Returns the value of attribute name.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#state ⇒ Object
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(name, is_plugin: false) ⇒ Microservice
constructor
A new instance of Microservice.
-
#run ⇒ Object
Must be implemented by a subclass.
- #shutdown ⇒ Object
Constructor Details
#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 |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
37 38 39 |
# File 'lib/cosmos/microservices/microservice.rb', line 37 def count @count end |
#custom ⇒ Object
Returns the value of attribute custom.
39 40 41 |
# File 'lib/cosmos/microservices/microservice.rb', line 39 def custom @custom end |
#error ⇒ Object
Returns the value of attribute error.
38 39 40 |
# File 'lib/cosmos/microservices/microservice.rb', line 38 def error @error end |
#microservice_status_thread ⇒ Object
Returns the value of attribute microservice_status_thread.
34 35 36 |
# File 'lib/cosmos/microservices/microservice.rb', line 34 def microservice_status_thread @microservice_status_thread end |
#name ⇒ Object
Returns the value of attribute name.
35 36 37 |
# File 'lib/cosmos/microservices/microservice.rb', line 35 def name @name end |
#scope ⇒ Object
Returns the value of attribute scope.
40 41 42 |
# File 'lib/cosmos/microservices/microservice.rb', line 40 def scope @scope end |
#state ⇒ Object
Returns the value of attribute state.
36 37 38 |
# File 'lib/cosmos/microservices/microservice.rb', line 36 def state @state end |
Class Method Details
.run ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cosmos/microservices/microservice.rb', line 42 def self.run microservice = self.new(ARGV[0]) begin MicroserviceStatusModel.set(microservice.as_json, scope: microservice.scope) microservice.state = 'RUNNING' microservice.run microservice.state = 'FINISHED' rescue Exception => err if err.class == SystemExit or err.class == Interrupt microservice.state = 'KILLED' else microservice.error = err microservice.state = 'DIED_ERROR' Logger.fatal("Microservice #{ARGV[0]} dying from exception\n#{err.formatted}") end ensure MicroserviceStatusModel.set(microservice.as_json, scope: microservice.scope) end end |
Instance Method Details
#as_json ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cosmos/microservices/microservice.rb', line 62 def as_json { 'name' => @name, 'state' => @state, 'count' => @count, 'error' => @error.as_json, 'custom' => @custom.as_json, 'plugin' => @plugin, } end |
#run ⇒ Object
Must be implemented by a subclass
192 193 194 |
# File 'lib/cosmos/microservices/microservice.rb', line 192 def run shutdown() end |
#shutdown ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/cosmos/microservices/microservice.rb', line 196 def shutdown @cancel_thread = true @microservice_sleeper.cancel if @microservice_sleeper MicroserviceStatusModel.set(as_json(), scope: @scope) FileUtils.remove_entry(@temp_dir) if File.exist?(@temp_dir) @metric.destroy end |