Class: OpenC3::TimelineWorker
- Defined in:
- lib/openc3/microservices/timeline_microservice.rb
Overview
The Timeline worker is a very simple thread pool worker. Once the timeline manager has pushed a job to the schedule one of these workers will run the CMD (command) or SCRIPT (script) or anything that could be expanded in the future.
Instance Method Summary collapse
- #clear_expired(activity) ⇒ Object
- #get_exec_setting ⇒ Object
- #get_token(username) ⇒ Object
-
#initialize(name:, logger:, scope:, queue:) ⇒ TimelineWorker
constructor
A new instance of TimelineWorker.
- #run ⇒ Object
- #run_activity(activity) ⇒ Object
- #run_command(activity) ⇒ Object
- #run_script(activity) ⇒ Object
Constructor Details
#initialize(name:, logger:, scope:, queue:) ⇒ TimelineWorker
Returns a new instance of TimelineWorker.
38 39 40 41 42 43 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 38 def initialize(name:, logger:, scope:, queue:) @timeline_name = name @logger = logger @scope = scope @queue = queue end |
Instance Method Details
#clear_expired(activity) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 149 def clear_expired(activity) begin num = ActivityModel.range_destroy(name: @timeline_name, scope: @scope, min: activity.start, max: activity.stop) @logger.info "#{@timeline_name} clear_expired removed #{num} items from #{activity.start} to #{activity.stop}" activity.add_event(status: 'completed') rescue StandardError => e @logger.error "#{@timeline_name} clear_expired failed > #{activity.as_json(:allow_nan => true)} #{e.}" end end |
#get_exec_setting ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 86 def get_exec_setting() json = ToolConfigModel.load_config('calendar-settings', 'default', scope: @scope) if json settings = JSON.parse(json) return settings['execEnabled'] else # Default is execute return true end end |
#get_token(username) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 45 def get_token(username) if ENV['OPENC3_API_CLIENT'].nil? ENV['OPENC3_API_PASSWORD'] ||= ENV['OPENC3_SERVICE_PASSWORD'] return OpenC3Authentication.new().token else # Check for offline access token model = nil model = OpenC3::OfflineAccessModel.get_model(name: username, scope: @scope) if username and username != '' if model and model.offline_access_token auth = OpenC3KeycloakAuthentication.new(ENV['OPENC3_KEYCLOAK_URL']) return auth.get_token_from_refresh_token(model.offline_access_token) else return nil end end end |
#run ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 62 def run @logger.info "#{@timeline_name} timeline worker running" loop do activity = @queue.pop break if activity.nil? run_activity(activity) end @logger.info "#{@timeline_name} timeline worker exiting" end |
#run_activity(activity) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 73 def run_activity(activity) case activity.kind.downcase when 'command' run_command(activity) when 'script' run_script(activity) when 'expire' clear_expired(activity) else @logger.error "Unknown kind passed to microservice #{@timeline_name}: #{activity.as_json(:allow_nan => true)}" end end |
#run_command(activity) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 97 def run_command(activity) @logger.info "#{@timeline_name} run_command > #{activity.as_json(:allow_nan => true)}" begin if get_exec_setting() username = activity.data['username'] token = get_token(username) raise "No token available for username: #{username}" unless token cmd_no_hazardous_check(activity.data['command'], scope: @scope, token: token) activity.commit(status: 'completed', fulfillment: true) else activity.commit(status: 'disabled', message: 'Execution is disabled') @logger.warn "#{@timeline_name} run_command disabled > #{activity.as_json(:allow_nan => true)}" end rescue StandardError => e activity.commit(status: 'failed', message: e.) @logger.error "#{@timeline_name} run_command failed > #{activity.as_json(:allow_nan => true)}, #{e.formatted}" end end |
#run_script(activity) ⇒ Object
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 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 116 def run_script(activity) @logger.info "#{@timeline_name} run_script > #{activity.as_json(:allow_nan => true)}" begin if get_exec_setting() username = activity.data['username'] token = get_token(username) raise "No token available for username: #{username}" unless token request = Net::HTTP::Post.new( "/script-api/scripts/#{activity.data['script']}/run?scope=#{@scope}", 'Content-Type' => 'application/json', 'Authorization' => token ) request.body = JSON.generate({ 'scope' => @scope, 'environment' => activity.data['environment'], 'timeline' => @timeline_name, 'id' => activity.start }) hostname = ENV['OPENC3_SCRIPT_HOSTNAME'] || 'openc3-cosmos-script-runner-api' response = Net::HTTP.new(hostname, 2902).request(request) raise "failed to call #{hostname}, for script: #{activity.data['script']}, response code: #{response.code}" if response.code != '200' activity.commit(status: 'completed', message: "#{activity.data['script']} => #{response.body}", fulfillment: true) else activity.commit(status: 'disabled', message: 'Execution is disabled') @logger.warn "#{@timeline_name} run_script disabled > #{activity.as_json(:allow_nan => true)}" end rescue StandardError => e activity.commit(status: 'failed', message: e.) @logger.error "#{@timeline_name} run_script failed > #{activity.as_json(:allow_nan => true)}, #{e.}" end end |