Class: Cnvrg::Helpers::Agent
- Inherits:
-
Object
- Object
- Cnvrg::Helpers::Agent
- Defined in:
- lib/cnvrg/helpers/agent.rb
Defined Under Namespace
Instance Method Summary collapse
- #base_url ⇒ Object
- #exec! ⇒ Object
- #execute_command ⇒ Object
- #get_logs_to_send ⇒ Object
-
#initialize(executer: nil, slug: nil, command: nil, container_name: nil, send_log_interval: 60, timeout: -1,, logs_regex: [], async: false, send_logs: false, files_exist: [], retries: 0, sleep_before_retry: 30, single_quotes: false, docker_user: nil, use_bash: false, **kwargs) ⇒ Agent
constructor
This class represent a single command in the system.
- #periodic_thread ⇒ Object
- #retry_command ⇒ Object
- #should_run? ⇒ Boolean
Constructor Details
#initialize(executer: nil, slug: nil, command: nil, container_name: nil, send_log_interval: 60, timeout: -1,, logs_regex: [], async: false, send_logs: false, files_exist: [], retries: 0, sleep_before_retry: 30, single_quotes: false, docker_user: nil, use_bash: false, **kwargs) ⇒ Agent
This class represent a single command in the system. it runs under an executer (machine_activity) so it should have all the executer params
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cnvrg/helpers/agent.rb', line 19 def initialize(executer: nil, slug: nil, command: nil, container_name: nil, send_log_interval: 60, timeout: -1, logs_regex: [], async: false, send_logs: false, files_exist: [], retries: 0, sleep_before_retry: 30, single_quotes: false, docker_user: nil, use_bash: false, **kwargs) @executer = executer @slug = slug @files_exist = files_exist @container_name = container_name @run_in_slave = @container_name.downcase == "slave" @log_interval = send_log_interval # https://ruby-doc.org/stdlib-2.5.1/libdoc/timeout/rdoc/Timeout.html timeout should be 0 for running forever if timeout.blank? or timeout.negative? @timeout = 0 else @timeout = timeout end @logs_regex = logs_regex || [] @async = async @command = command @send_logs = send_logs @retries = retries.try(:to_i) ## How many times the user asked to try to execute the command again @sleep_before_retry = sleep_before_retry @real_execution_retries = 0 ## How many times the command really executed until success @single_quotes = single_quotes @docker_user = "" @shell_type = use_bash ? "bash -l" : "sh" if docker_user.present? @docker_user = " --user #{docker_user}" end if @run_in_slave if @single_quotes @command = "docker exec #{@docker_user} -it #{@executer.slave_id} #{@shell_type} -c '#{@command}'" else @command = "docker exec #{@docker_user} -it #{@executer.slave_id} #{@shell_type} -c \"#{@command}\"" end end @output = [] @errors = [] @exit_status = nil @is_running = true @pid = nil end |
Instance Method Details
#base_url ⇒ Object
59 60 61 |
# File 'lib/cnvrg/helpers/agent.rb', line 59 def base_url [@executer.activity_url, "commands", @slug].join("/") end |
#exec! ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cnvrg/helpers/agent.rb', line 76 def exec! log_internal("Command: #{@command} with slug: #{@slug} started!") if @command.blank? @exit_status = 0 elsif should_run? send_logs(status: Status::STARTED) periodic_thread_handle = periodic_thread execute_command else @exit_status = 127 end finish_log = "Command: #{@command} with slug: #{@slug} finished" finish_log += " after #{@real_execution_retries} retries" if @real_execution_retries > 0 log_internal(finish_log) send_logs(exit_status: @exit_status, status: Status::FINISHED) if periodic_thread_handle.present? periodic_thread_handle.join end end |
#execute_command ⇒ Object
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 |
# File 'lib/cnvrg/helpers/agent.rb', line 120 def execute_command Timeout.timeout(@timeout) do PTY.spawn(@command) do |stdout, stdin, pid, stderr| @pid = pid begin if stdout.present? stdout.each do |line| log_internal(line, level: LogLevel::PURE) line = line.strip.gsub(/\e\[([;\d]+)?m/, '') @output << {log: line, timestamp: Time.now} end end if stderr.present? stderr.each do |line| line = line.strip.gsub(/\e\[([;\d]+)?m/, '') log_internal(line, level: LogLevel::ERROR) @errors << {log: line, timestamp: Time.now} end end rescue Errno::EIO => e next rescue => e log_internal(e., level: LogLevel::ERROR) log_internal(e.backtrace.join("\n"), level: LogLevel::ERROR) @errors << {log: e., timestamp: Time.now} end ::Process.wait pid end end @exit_status = $?.exitstatus rescue Timeout::Error Process.kill(0, @pid) @errors << {log: "Command timed out!", timestamp: Time.now} log_internal("Command timed out!", level: LogLevel::ERROR) @exit_status = 124 ensure retry_command if @retries != 0 and @exit_status !=0 @exit_status end |
#get_logs_to_send ⇒ Object
96 97 98 99 100 |
# File 'lib/cnvrg/helpers/agent.rb', line 96 def get_logs_to_send new_logs = @output.pop(@output.length) new_errors = @errors.pop(@errors.length) [new_logs, new_errors] end |
#periodic_thread ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/cnvrg/helpers/agent.rb', line 103 def periodic_thread Thread.new do while @exit_status.blank? Thread.exit if @log_interval.blank? sleep(@log_interval) send_logs end end end |
#retry_command ⇒ Object
113 114 115 116 117 118 |
# File 'lib/cnvrg/helpers/agent.rb', line 113 def retry_command @retries -=1 sleep @sleep_before_retry @real_execution_retries +=1 execute_command end |
#should_run? ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cnvrg/helpers/agent.rb', line 63 def should_run? if @files_exist.present? file_doesnt_exists = @files_exist.find do |file| not File.exists? file end return true if file_doesnt_exists.blank? log_internal("Can't find file #{file_doesnt_exists}, stopping the job") return false end true end |