16
17
18
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
|
# File 'lib/mutx/background_jobs/workers/executor.rb', line 16
def perform(result_id)
@output = ""
Mutx::Support::Configuration.get
Mutx::Database::MongoConnector.new Mutx::Support::Configuration.db_connection_data
Mutx::Support::ChangeInspector.is_there_a_change?
result = Mutx::Results::Result.get(result_id)
task = Mutx::Tasks::Task.get(result.task_id)
result.mutx_report_file_name= "mutx_report_#{result_id}.html"
if result.is_ruby_platform?
if Mutx::Platforms::Ruby.using_bundler?
bundle_output = Mutx::Support::Console.execute "bundle install"
bundle_output.slice! "fatal: Not a git repository (or any of the parent directories): .git"
end
if bundle_output
result.append_output bundle_output if bundle_output.include? "Installing"
if bundle_output.include? "Could not find"
result.finish!
raise "An error ocurred installing gem while executing bundler"
else
result.append_output "All GEMS are installed and running!\nEXECUTION OUTPUT:\n\n--"
end
end
end
Dir.mkdir "#{Dir.pwd}/mutx/temp" unless Dir.exist? "#{Dir.pwd}/mutx/temp"
efective_command = []
efective_command << Mutx::Support::Configuration.headless? if result.gui_task?
efective_command << result.command
efective_command << "-f pretty -f html -o mutx/temp/#{result.mutx_report_file_name}" if result.is_cucumber?
efective_command << result.custom_params_values
efective_command << "_id=#{result.id}"
result.mutx_command= efective_command.join(" ")
attach_folder = "#{Dir.pwd}/mutx/out/#{result.id}/attachment"
Mutx::Support::Log.debug "[result:#{result.id}] #{efective_command.join(' ')}" if Mutx::Support::Log
result.running!
Mutx::Support::Log.debug "[result:#{result.id}] setted as running" if Mutx::Support::Log
@output = ""
@count = 0
if !Mutx::Support::Configuration.proxys.empty?
Mutx::Support::Configuration.proxys.detect{|proxy| Mutx::Support::Console.execute proxy}
end
Mutx::Support::TimeHelper.start
Mutx::Support::Log.debug "[result:#{result.id}] Creating process" if Mutx::Support::Log
@start_time = Time.now
begin
PTY.spawn("#{result.mutx_command}") do |stdout, stdin, pid|
result.pid ="#{`ps -fea | grep #{Process.pid} | grep -v grep | awk '$2!=#{Process.pid} && $8!~/awk/ && $3==#{Process.pid}{print $2}'`}"
result.save!
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)/, "")
@output = ""
if result.seconds_without_changes > Mutx::Support::Configuration.execution_time_to_live
result.finished_by_timeout! and break
end
}
result.append_output @output unless @output.empty?
result.append_output "=========================\n"
rescue Errno::EIO, Errno::ENOENT => e
end
end
rescue PTY::ChildExited, Errno::ENOENT => e
cmd = result.mutx_command.match(/(\D*)\_/)[0].delete"_"
@output = "EXCEPTION: #{e.message} for the command requested for you: #{cmd.upcase}"
result.append_output @output unless @output.empty?
result.append_output "\n\n=========================\n"
end
result.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]
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)
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)
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
Mutx::Support::Log.debug "[result:#{result.id}]| command => #{result.mutx_command} | result as => #{result.status}" if Mutx::Support::Log
Mutx::Database::MongoConnector.force_close
end
|