Class: Cosmos::TimelineWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/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

Constructor Details

#initialize(name:, scope:, queue:) ⇒ TimelineWorker

Returns a new instance of TimelineWorker.



35
36
37
38
39
40
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 35

def initialize(name:, scope:, queue:)
  @timeline_name = name
  @scope = scope
  @queue = queue
  @authentication = generate_auth()
end

Instance Method Details

#clear_expired(activity) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 111

def clear_expired(activity)
  begin
    ActivityModel.range_destroy(name: @timeline_name, scope: @scope, min: activity.start, max: activity.stop)
    activity.add_event(status: 'completed')
  rescue StandardError => e
    Logger.error "#{@timeline_name} clear_expired failed > #{activity.as_json} #{e.message}"
  end
end

#generate_authObject

generate the auth object



43
44
45
46
47
48
49
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 43

def generate_auth
  if ENV['COSMOS_API_USER'].nil? || ENV['COSMOS_API_CLIENT'].nil?
    return CosmosAuthentication.new()
  else
    return CosmosKeycloakAuthentication.new(ENV['COSMOS_KEYCLOAK_URL'])
  end
end

#runObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 51

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 62

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}"
  end
end

#run_command(activity) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 75

def run_command(activity)
  Logger.info "#{@timeline_name} run_command > #{activity.as_json}"
  begin
    cmd_no_hazardous_check(activity.data['command'], scope: @scope)
    activity.commit(status: 'completed', fulfillment: true)
  rescue StandardError => e
    activity.commit(status: 'failed', message: e.message)
    Logger.error "#{@timeline_name} run_cmd failed > #{activity.as_json}, #{e.message}"
  end
end

#run_script(activity) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cosmos/microservices/timeline_microservice.rb', line 86

def run_script(activity)
  Logger.info "#{@timeline_name} run_script > #{activity.as_json}"
  begin
    request = Net::HTTP::Post.new(
      "/script-api/scripts/#{activity.data['script']}/run?scope=#{@scope}",
      'Content-Type' => 'application/json',
      'Authorization' => @authentication.token()
    )
    request.body = JSON.generate({
      'scope' => @scope,
      'environment' => activity.data['environment'],
      'timeline' => @timeline_name,
      'id' => activity.start
    })
    hostname = ENV['COSMOS_SCRIPT_HOSTNAME'] || '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.message)
    Logger.error "#{@timeline_name} run_script failed > #{activity.as_json.to_s}, #{e.message}"
  end
end