Class: Cnvrg::Helpers::Executer
- Inherits:
-
Object
- Object
- Cnvrg::Helpers::Executer
- Defined in:
- lib/cnvrg/helpers/executer.rb
Instance Attribute Summary collapse
-
#agent_id ⇒ Object
readonly
Returns the value of attribute agent_id.
-
#machine_activity ⇒ Object
readonly
Returns the value of attribute machine_activity.
-
#slave_id ⇒ Object
readonly
Returns the value of attribute slave_id.
Instance Method Summary collapse
- #activity_url ⇒ Object
- #containers ⇒ Object
- #create_file_cmd(path, content) ⇒ Object
- #current_homedir ⇒ Object
- #execute_cmds ⇒ Object
- #executer_stats ⇒ Object
- #get_node_and_pod_names ⇒ Object
- #get_node_events(node_name) ⇒ Object
- #get_pod_events(pod_name) ⇒ Object
- #handle_files(files) ⇒ Object
- #init ⇒ Object
-
#initialize(owner: nil, machine_activity: nil, poll_every: 30, job_id: nil) ⇒ Executer
constructor
this class represent a machine_activity.
- #main_thread ⇒ Object
- #merge_log_block(logs) ⇒ Object
- #poll ⇒ Object
- #polling_thread ⇒ Object
- #pre_pod_stop ⇒ Object
- #run_in_slave(command) ⇒ Object
- #slave_env ⇒ Object
- #slave_homedir ⇒ Object
- #spark_path ⇒ Object
Constructor Details
#initialize(owner: nil, machine_activity: nil, poll_every: 30, job_id: nil) ⇒ Executer
this class represent a machine_activity. it will poll the commands, communicate with the server (poll commands) and let the server know the status of this executer.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cnvrg/helpers/executer.rb', line 8 def initialize(owner: nil, machine_activity: nil, poll_every: 30, job_id: nil) @owner = owner @job_id = job_id @poll_every = poll_every @machine_activity = machine_activity @commands_q = Queue.new @files_q = Queue.new @agent_id = nil @slave_id = nil end |
Instance Attribute Details
#agent_id ⇒ Object (readonly)
Returns the value of attribute agent_id.
3 4 5 |
# File 'lib/cnvrg/helpers/executer.rb', line 3 def agent_id @agent_id end |
#machine_activity ⇒ Object (readonly)
Returns the value of attribute machine_activity.
3 4 5 |
# File 'lib/cnvrg/helpers/executer.rb', line 3 def machine_activity @machine_activity end |
#slave_id ⇒ Object (readonly)
Returns the value of attribute slave_id.
3 4 5 |
# File 'lib/cnvrg/helpers/executer.rb', line 3 def slave_id @slave_id end |
Instance Method Details
#activity_url ⇒ Object
33 34 35 |
# File 'lib/cnvrg/helpers/executer.rb', line 33 def activity_url ['users', @owner, 'machine_activities', @machine_activity].join("/") end |
#containers ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cnvrg/helpers/executer.rb', line 74 def containers agent_id = nil slave_id = nil while agent_id.blank? or slave_id.blank? grep_by = @job_id grep_by = "$(hostname)" if ENV['KUBERNETES_PORT'].present? cntrs = `docker ps --format "table {{.ID}},{{.Names}}" | grep -i #{grep_by}`.split("\n").map{|x| x.strip} agent_id = cntrs.find{|container_name| container_name.include? "agent"}.split(",").first rescue nil slave_id = cntrs.find{|container_name| container_name.include? "slave"}.split(",").first rescue nil sleep(5) end if slave_id.blank? raise "Can't find slave id" end [agent_id, slave_id] end |
#create_file_cmd(path, content) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/cnvrg/helpers/executer.rb', line 19 def create_file_cmd(path, content) if path.include? "~" path = File.(path) end FileUtils.mkdir_p(File.dirname(path)) File.open(path, "w+"){|f| f.write(content)} end |
#current_homedir ⇒ Object
91 92 93 |
# File 'lib/cnvrg/helpers/executer.rb', line 91 def current_homedir `env | grep -w HOME`.strip.split("=").try(:last) end |
#execute_cmds ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cnvrg/helpers/executer.rb', line 151 def execute_cmds pids = [] while true if @commands_q.empty? sleep(5) next end cmd = @commands_q.pop.symbolize_keys command_json = Cnvrg::API.request([activity_url, "commands", cmd[:slug]].join('/'), "GET") cmd_status = command_json["status"] rescue "" if cmd_status == Cnvrg::Helpers::Agent::Status::ABORTED Cnvrg::Logger.log_info("stopping job because command #{cmd[:slug]} with status #{cmd_status}") next end pid = Process.fork do Cnvrg::Helpers::Agent.new(executer: self, **cmd).exec! end if cmd[:async].blank? Process.waitpid(pid) else Process.detach(pid) end pids << pid ###### end pids end |
#executer_stats ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cnvrg/helpers/executer.rb', line 37 def executer_stats return @stats if @stats.present? Cnvrg::Logger.log_info("getting containers") @agent_id, @slave_id = containers Cnvrg::Logger.log_info("got containers") pod_name, node_name = get_node_and_pod_names @stats = { pod_name: pod_name, node_name: node_name, agent: { container_id: @agent_id, workdir: `pwd`.strip, homedir: current_homedir, user: `whoami`.strip, user_id: `id -u`.strip, group_id: `id -g`.strip, cnvrg: Cnvrg::VERSION }, slave: { container_id: @slave_id, workdir: run_in_slave('pwd'), homedir: slave_homedir, spark_path: spark_path, user: run_in_slave( 'whoami'), cnvrg: run_in_slave( 'which cnvrg'), has_bash: run_in_slave( 'which bash'), user_id: run_in_slave( 'id -u'), group_id: run_in_slave( 'id -g'), python_version: run_in_slave( 'python --version'), python3_version: run_in_slave( 'python3 --version'), pip_version: run_in_slave( 'pip --version'), pip3_version: run_in_slave( 'pip3 --version') }, } @stats end |
#get_node_and_pod_names ⇒ Object
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/cnvrg/helpers/executer.rb', line 186 def get_node_and_pod_names pod_name = `hostname`.strip rescue nil node_name = nil if pod_name.present? pod_describe = `kubectl -n cnvrg get pod #{pod_name} -o json` rescue nil pod_describe = JSON.parse(pod_describe) rescue {} node_name = pod_describe["spec"]["nodeName"] rescue nil end [pod_name, node_name] end |
#get_node_events(node_name) ⇒ Object
209 210 211 212 |
# File 'lib/cnvrg/helpers/executer.rb', line 209 def get_node_events(node_name) return if node_name.blank? `kubectl get event --all-namespaces --field-selector involvedObject.name=#{node_name} -o json` end |
#get_pod_events(pod_name) ⇒ Object
204 205 206 207 |
# File 'lib/cnvrg/helpers/executer.rb', line 204 def get_pod_events(pod_name) return if pod_name.blank? `kubectl get event --namespace cnvrg --field-selector involvedObject.name=#{pod_name} -o json` end |
#handle_files(files) ⇒ Object
27 28 29 30 31 |
# File 'lib/cnvrg/helpers/executer.rb', line 27 def handle_files(files) (files || {}).each do |path, content| create_file_cmd(path, content) end end |
#init ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cnvrg/helpers/executer.rb', line 122 def init resp = Cnvrg::API.request(activity_url, "PUT", {stats: executer_stats}) machine_activity = resp["machine_activity"] Cnvrg::Logger.log_info("Got back machine activity #{machine_activity}") if machine_activity.present? and @machine_activity != machine_activity Cnvrg::Logger.log_info("Changing to machine activity #{machine_activity}") machine_activity_yml = {slug: machine_activity} File.open("/conf/.machine_activity.yml", "w+") {|f| f.write machine_activity_yml.to_yaml} @machine_activity = machine_activity end rescue => e Cnvrg::Logger.log_error(e) end |
#main_thread ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/cnvrg/helpers/executer.rb', line 143 def main_thread init Thread.new do polling_thread end execute_cmds end |
#merge_log_block(logs) ⇒ Object
181 182 183 184 |
# File 'lib/cnvrg/helpers/executer.rb', line 181 def merge_log_block(logs) logs.group_by {|log| log[:timestamp].to_s} .map {|ts, logz| {timestamp: ts, logs: logz.map {|l| l[:log]}.join("\n")}} end |
#poll ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/cnvrg/helpers/executer.rb', line 112 def poll resp = Cnvrg::API.request([activity_url, "commands"].join('/'), "POST") commands = resp["commands"] files = resp["files"] handle_files(files) commands.each{|cmd| @commands_q.push(cmd)} rescue => e Cnvrg::Logger.log_error(e) end |
#polling_thread ⇒ Object
136 137 138 139 140 141 |
# File 'lib/cnvrg/helpers/executer.rb', line 136 def polling_thread while true poll sleep(@poll_every) end end |
#pre_pod_stop ⇒ Object
197 198 199 200 201 202 |
# File 'lib/cnvrg/helpers/executer.rb', line 197 def pre_pod_stop pod_name, node_name = get_node_and_pod_names pod_events = get_pod_events(pod_name) node_events = get_node_events(node_name) Cnvrg::API.request([activity_url, "job_events"].join('/'), "POST", {pod_events: pod_events, node_events: node_events}) end |
#run_in_slave(command) ⇒ Object
107 108 109 |
# File 'lib/cnvrg/helpers/executer.rb', line 107 def run_in_slave(command) `docker exec -i #{@slave_id} sh -c '#{command}'`.strip end |
#slave_env ⇒ Object
103 104 105 |
# File 'lib/cnvrg/helpers/executer.rb', line 103 def slave_env run_in_slave("env").split("\n").map{|x| x.split("=")} end |
#slave_homedir ⇒ Object
99 100 101 |
# File 'lib/cnvrg/helpers/executer.rb', line 99 def slave_homedir() run_in_slave("env | grep -w HOME").split("=").try(:last) end |
#spark_path ⇒ Object
95 96 97 |
# File 'lib/cnvrg/helpers/executer.rb', line 95 def spark_path run_in_slave("env | grep SPARK_HOME").strip.split("=").try(:last) end |