# encoding: utf-8 require 'mutx' require 'socket' require 'pty' require 'colorize' require 'fileutils' module Mutx module Workers class Executor include Sidekiq::Worker sidekiq_options :retry => false def perform(result_id) @output = "" Mutx::Support::Configuration.get Mutx::Database::MongoConnector.new Mutx::Support::Configuration.db_connection_data result = Mutx::Results::Result.get(result_id) puts "[#{result.id}]| Execution created" task = Mutx::Tasks::Task.get(result.task_id) result.mutx_report_file_name= "mutx_report_#{result_id}.html" Mutx::Support::Bundle.new.bundle_update(result) if Mutx::Support::ChangeInspector.is_there_a_change? efective_command = [] efective_command << Mutx::Support::Configuration.headless? if result.gui_task? efective_command << result.custom_params_values efective_command << result.command efective_command << "-f pretty -f html -o mutx/temp/#{result.mutx_report_file_name}" if result.is_cucumber? efective_command << "_id=#{result.id}" # to use inside execution the possibility to add information to the result puts "[#{result.id}]| Command: #{efective_command.join(" ")}" result.mutx_command = efective_command.join(" ") attach_folder = "#{Dir.pwd}/mutx/out/#{result.id}/attachment" result.running! puts "[#{result.id}]| setted as running" if !Mutx::Support::Configuration.proxys.empty? Mutx::Support::Configuration.proxys.detect{|proxy| Mutx::Support::Console.execute proxy} end Mutx::Support::TimeHelper.start # Sets timestamp before start process begin PTY.spawn( "#{result.mutx_command}" ) do |stdout, stdin, pid| begin stdout.each { |line| @output = line @output.slice! "fatal: Not a git repository (or any of the parent directories): .git" result.append_output! @output.gsub(/(\[\d{1,2}\m)/, "") if result.time_to_live_reached? result.finished_by_timeout! and break end } result.append_output! @output unless @output.empty? rescue Errno::EIO ensure Process.wait(pid) end end rescue PTY::ChildExited => e puts "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" puts "The child process exited!, #{e.message}" puts "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" rescue Errno::ENOENT => e cmd = result.mutx_command.match(/(\D*)\_/)[0].delete"_" exception_message = "EXCEPTION: #{e.message} for the command requested for you: #{cmd.upcase}" puts "[#{result.id}]| exception_message: #{exception_message}" @output = exception_message result.append_output! @output unless @output.empty? end puts "[#{result.id}]| Status before ensure finish => #{result.status}" result.ensure_finished! result.eval_regex! puts "[#{result.id}]| ensure finished" puts result.summary task = Mutx::Database::MongoConnector.task_data_for result.task[:id] subject = if ((task[:subject].empty?) || (task[:subject].nil?)) result.console_output.match(/\SUBJECT(.*)/)[1] if result.console_output.include? "SUBJECT" || task[:name] else task[:subject] end email = task[:mail] type = task[:type] name = task[:name] id = task[:_id] cucumber = task[:cucumber] notify_on = task[:notify_on] result.eval_regex! if ( (task["notifications"].eql? "on") && (!task["stop_bots"].eql? "on") && (!task["stop_bots"].eql? "off") && (!result.regex.empty?) && (result.console_output.to_s.include? "#{result.regex.to_s}") ) Mutx::Database::MongoConnector.mark_notified (result_id) Mutx::Workers::EmailSender.perform_async(result_id, subject, email, name, id, type, cucumber, notify_on, attach_folder) if ((task[:notifications].eql? "on") && (!email.empty?)) elsif ( (task["notifications"].eql? "on") && (!task["stop_bots"].eql? "on") && (!task["stop_bots"].eql? "off") && (result.regex.empty?) ) Mutx::Database::MongoConnector.mark_notified (result_id) Mutx::Workers::EmailSender.perform_async(result_id, subject, email, name, id, type, cucumber, notify_on, attach_folder) if ((task[:notifications].eql? "on") && (!email.empty?)) elsif ( (task["notifications"].eql? "on") && (task["stop_bots"].eql? "on") && (!result.regex.empty?) && (result.console_output.to_s.include? "#{result.regex.to_s}") ) Mutx::Database::MongoConnector.mark_notified (result_id) Mutx::Workers::EmailSender.perform_async(result_id, subject, email, name, id, type, cucumber, notify_on, attach_folder) if ((task[:notifications].eql? "on") && (!email.empty?)) Mutx::Database::MongoConnector.update_stop_bots_off (id) #silence notifications Mutx::Support::MailSender.new.sender(nil, "#{name} Comenzo a notificar, se le informara cuando vuelva a la normalidad", "#{email}", name, nil, nil, nil, nil, nil) elsif ( (task["notifications"].eql? "on") && (task["stop_bots"].eql? "off") && (!result.regex.empty?) && (!result.console_output.to_s.include? "#{result.regex.to_s}") ) Mutx::Database::MongoConnector.update_stop_bots_on (id) #on notifications Mutx::Support::MailSender.new.sender(nil, "#{name} Volvio a funcionar con normalidad", "#{email}", name, nil, nil, nil, nil, nil) elsif ( (task["notifications"].eql? "on") && (!result.regex.empty?) && (!result.console_output.to_s.include? "#{result.regex.to_s}") ) puts "****Result not to being notified, because regex #{result.regex.to_s} not included on output****" end puts "[#{result.id}]| command => #{result.mutx_command} | result as => #{result.status}" puts "[#{result.id}]| Removing out dir" FileUtils.remove_dir(Dir.pwd + "/mutx/out/#{result.id}") Mutx::Database::MongoConnector.force_close end end end end