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_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.
37 38 39 40 41 42 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 37 def initialize(name:, logger:, scope:, queue:) @timeline_name = name @logger = logger @scope = scope @queue = queue end |
Instance Method Details
#clear_expired(activity) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 127 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_token(username) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 44 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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 61 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} timeine worker exiting" end |
#run_activity(activity) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 72 def run_activity(activity) case activity.kind.upcase 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
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 85 def run_command(activity) @logger.info "#{@timeline_name} run_command > #{activity.as_json(:allow_nan => true)}" begin 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) rescue StandardError => e activity.commit(status: 'failed', message: e.) @logger.error "#{@timeline_name} run_cmd failed > #{activity.as_json(:allow_nan => true)}, #{e.formatted}" end end |
#run_script(activity) ⇒ Object
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 |
# File 'lib/openc3/microservices/timeline_microservice.rb', line 99 def run_script(activity) @logger.info "#{@timeline_name} run_script > #{activity.as_json(:allow_nan => true)}" begin 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) rescue StandardError => e activity.commit(status: 'failed', message: e.) @logger.error "#{@timeline_name} run_script failed > #{activity.as_json(:allow_nan => true).to_s}, #{e.}" end end |