4233: "helper" method for replaying a job log from a specified file, with utility...
[arvados.git] / services / api / lib / simulate_job_log.rb
1 module SimulateJobLog
2         def replay(filename, simulated_job_uuid = nil, multiplier = 1)
3                 raise "Environment must be development or test" unless [ 'test', 'development' ].include? ENV['RAILS_ENV']
4
5             multiplier = multiplier.to_f
6             multiplier = 1.0 if multiplier <= 0
7
8             actual_start_time = Time.now
9             log_start_time = nil
10
11                 act_as_system_user do
12                         File.open(filename).each.with_index do |line, index|
13                                 cols = {}
14                         cols[:timestamp], cols[:job_uuid], cols[:pid], cols[:task], cols[:event_type], cols[:message] = line.split(' ', 6)
15                         cols[:timestamp] = Time.strptime( cols[:timestamp], "%Y-%m-%d_%H:%M:%S" )
16                         # Override job uuid with a simulated one if specified
17                         cols[:job_uuid] = simulated_job_uuid || cols[:job_uuid]
18                         # determine when we want to simulate this log being created, based on the time multiplier
19                         log_start_time = cols[:timestamp] if log_start_time.nil?
20                         log_time = cols[:timestamp]
21                         actual_elapsed_time = Time.now - actual_start_time
22                         log_elapsed_time = log_time - log_start_time
23                     modified_elapsed_time = log_elapsed_time / multiplier
24                         pause_time = modified_elapsed_time - actual_elapsed_time
25                         if pause_time > 0
26                                 sleep pause_time
27                             end
28                             # output log entry for debugging and create it in the current environment's database
29                         puts "#{index} #{cols.to_yaml}\n"
30                         Log.new({
31                                 event_at:    Time.zone.local_to_utc(cols[:timestamp]),
32                                 object_uuid: cols[:job_uuid],
33                                 event_type:  cols[:event_type],
34                                 properties:  { 'text' => cols[:message] }
35                         }).save!
36                         end
37                 end
38
39         end
40 end