Description:
added termination reporting to grader (using at_exit) git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@377 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r70:347e4eeef742 - - 1 file changed: 8 inserted, 3 deleted

@@ -35,178 +35,183
35 if config.talkative
35 if config.talkative
36 puts str
36 puts str
37 end
37 end
38 if config.logging
38 if config.logging
39 fp = File.open(log_file_name,"a")
39 fp = File.open(log_file_name,"a")
40 fp.puts("GRADER: #{Time.new.strftime("%H:%M")} #{str}")
40 fp.puts("GRADER: #{Time.new.strftime("%H:%M")} #{str}")
41 fp.close
41 fp.close
42 end
42 end
43 end
43 end
44
44
45 def display_manual
45 def display_manual
46 puts <<USAGE
46 puts <<USAGE
47 Grader.
47 Grader.
48 using: (1) grader
48 using: (1) grader
49 (2) grader environment [mode]
49 (2) grader environment [mode]
50 (3) grader stop [all|pids-list]
50 (3) grader stop [all|pids-list]
51 (4) grader --help
51 (4) grader --help
52 (1) call grader with environment = 'exam', mode = 'queue'
52 (1) call grader with environment = 'exam', mode = 'queue'
53 (2) possible modes are: 'queue', 'prob', 'test_request'
53 (2) possible modes are: 'queue', 'prob', 'test_request'
54 (3) create stop-file to stop running grader in queue mode
54 (3) create stop-file to stop running grader in queue mode
55 (4) You are here.
55 (4) You are here.
56 USAGE
56 USAGE
57 end
57 end
58
58
59 #########################################
59 #########################################
60 # main program
60 # main program
61 #########################################
61 #########################################
62
62
63 # with --help
63 # with --help
64 if (ARGV.length==1) and (/help/.match(ARGV[0]))
64 if (ARGV.length==1) and (/help/.match(ARGV[0]))
65 display_manual
65 display_manual
66 exit(0)
66 exit(0)
67 end
67 end
68
68
69 # reading environment and options
69 # reading environment and options
70 if (ARGV.length >= 1) and (ARGV[0]=='stop')
70 if (ARGV.length >= 1) and (ARGV[0]=='stop')
71 if ARGV.length==1
71 if ARGV.length==1
72 puts "you should specify pid-list or 'all'"
72 puts "you should specify pid-list or 'all'"
73 display_manual
73 display_manual
74 elsif (ARGV.length==2) and (ARGV[1]=='all')
74 elsif (ARGV.length==2) and (ARGV[1]=='all')
75 stop_grader(:all)
75 stop_grader(:all)
76 puts "A global stop file ('stop.all') created."
76 puts "A global stop file ('stop.all') created."
77 puts "You should remove it manually later."
77 puts "You should remove it manually later."
78 else
78 else
79 (1..ARGV.length-1).each do |i|
79 (1..ARGV.length-1).each do |i|
80 stop_grader(ARGV[i])
80 stop_grader(ARGV[i])
81 end
81 end
82 puts "stop file(s) created"
82 puts "stop file(s) created"
83 end
83 end
84 exit(0)
84 exit(0)
85 end
85 end
86
86
87 if check_stopfile
87 if check_stopfile
88 puts "Stop file exists. Terminated."
88 puts "Stop file exists. Terminated."
89 clear_stopfile
89 clear_stopfile
90 exit(0)
90 exit(0)
91 end
91 end
92
92
93 grader_mode = 'queue'
93 grader_mode = 'queue'
94 if ARGV.length >= 1
94 if ARGV.length >= 1
95 GRADER_ENV = ARGV[0]
95 GRADER_ENV = ARGV[0]
96 if ARGV.length >=2
96 if ARGV.length >=2
97 grader_mode = ARGV[1]
97 grader_mode = ARGV[1]
98 end
98 end
99 else
99 else
100 GRADER_ENV = 'exam'
100 GRADER_ENV = 'exam'
101 end
101 end
102
102
103 puts "environment: #{GRADER_ENV}"
103 puts "environment: #{GRADER_ENV}"
104 require File.join(File.dirname(__FILE__),'config/environment')
104 require File.join(File.dirname(__FILE__),'config/environment')
105
105
106 # add grader_mode to config
106 # add grader_mode to config
107 # this is needed because method log needs it. TODO: clean this up
107 # this is needed because method log needs it. TODO: clean this up
108 class << config
108 class << config
109 attr_accessor :grader_mode
109 attr_accessor :grader_mode
110 end
110 end
111 config.grader_mode = grader_mode
111 config.grader_mode = grader_mode
112
112
113 # reading rails environment
113 # reading rails environment
114 log 'Reading rails environment'
114 log 'Reading rails environment'
115
115
116 RAILS_ENV = config.rails_env
116 RAILS_ENV = config.rails_env
117 require RAILS_ROOT + '/config/environment'
117 require RAILS_ROOT + '/config/environment'
118
118
119 # register grader process
119 # register grader process
120 if config.report_grader
120 if config.report_grader
121 grader_proc = GraderProcess.register(config.grader_hostname,
121 grader_proc = GraderProcess.register(config.grader_hostname,
122 Process.pid,
122 Process.pid,
123 grader_mode)
123 grader_mode)
124 else
124 else
125 grader_proc = nil
125 grader_proc = nil
126 end
126 end
127
127
128 #set loggin environment
128 #set loggin environment
129 ENV['GRADER_LOGGING'] = log_file_name
129 ENV['GRADER_LOGGING'] = log_file_name
130
130
131 + # register exit handler to report inactive, and terminated
132 + at_exit do
133 + if grader_proc!=nil
134 + grader_proc.report_inactive
135 + grader_proc.terminate
136 + end
137 + end
138 +
131 #
139 #
132 # MAIN LOOP
140 # MAIN LOOP
133 #
141 #
134
142
135 case grader_mode
143 case grader_mode
136 when "queue", "test_request"
144 when "queue", "test_request"
137 log "Grader: #{grader_mode}"
145 log "Grader: #{grader_mode}"
138 if grader_mode=="queue"
146 if grader_mode=="queue"
139 engine = Grader::Engine.new
147 engine = Grader::Engine.new
140 else
148 else
141 engine = Grader::Engine.new(Grader::TestRequestRoomMaker.new,
149 engine = Grader::Engine.new(Grader::TestRequestRoomMaker.new,
142 Grader::TestRequestReporter.new)
150 Grader::TestRequestReporter.new)
143 end
151 end
144
152
145 runner = Grader::Runner.new(engine, grader_proc)
153 runner = Grader::Runner.new(engine, grader_proc)
146 while true
154 while true
147
155
148 if check_stopfile # created by calling grader stop
156 if check_stopfile # created by calling grader stop
149 clear_stopfile
157 clear_stopfile
150 log "stopped (with stop file)"
158 log "stopped (with stop file)"
151 break
159 break
152 end
160 end
153
161
154 if grader_mode=="queue"
162 if grader_mode=="queue"
155 task = runner.grade_oldest_task
163 task = runner.grade_oldest_task
156 else
164 else
157 task = runner.grade_oldest_test_request
165 task = runner.grade_oldest_test_request
158 end
166 end
159 if task==nil
167 if task==nil
160 sleep(1)
168 sleep(1)
161 end
169 end
162 end
170 end
163
171
164 when "prob"
172 when "prob"
165 engine = Grader::Engine.new
173 engine = Grader::Engine.new
166 runner = Grader::Runner.new(engine, grader_proc)
174 runner = Grader::Runner.new(engine, grader_proc)
167
175
168 grader_proc.report_active if grader_proc!=nil
176 grader_proc.report_active if grader_proc!=nil
169
177
170 ARGV.shift
178 ARGV.shift
171 ARGV.shift
179 ARGV.shift
172
180
173 ARGV.each do |prob_name|
181 ARGV.each do |prob_name|
174 prob = Problem.find_by_name(prob_name)
182 prob = Problem.find_by_name(prob_name)
175 if prob==nil
183 if prob==nil
176 puts "cannot find problem: #{prob_name}"
184 puts "cannot find problem: #{prob_name}"
177 else
185 else
178 runner.grade_problem(prob)
186 runner.grade_problem(prob)
179 end
187 end
180 end
188 end
181
189
182 when "sub"
190 when "sub"
183 engine = Grader::Engine.new
191 engine = Grader::Engine.new
184 runner = Grader::Runner.new(engine, grader_proc)
192 runner = Grader::Runner.new(engine, grader_proc)
185
193
186 grader_proc.report_active if grader_proc!=nil
194 grader_proc.report_active if grader_proc!=nil
187
195
188 ARGV.shift
196 ARGV.shift
189 ARGV.shift
197 ARGV.shift
190
198
191 ARGV.each do |sub_id|
199 ARGV.each do |sub_id|
192 puts "Grading #{sub_id}"
200 puts "Grading #{sub_id}"
193 begin
201 begin
194 submission = Submission.find(sub_id.to_i)
202 submission = Submission.find(sub_id.to_i)
195 rescue ActiveRecord::RecordNotFound
203 rescue ActiveRecord::RecordNotFound
196 puts "Record not found"
204 puts "Record not found"
197 submission = nil
205 submission = nil
198 end
206 end
199
207
200 if submission!=nil
208 if submission!=nil
201 runner.grade_submission(submission)
209 runner.grade_submission(submission)
202 end
210 end
203 end
211 end
204
212
205 else
213 else
206 display_manual
214 display_manual
207 exit(0)
215 exit(0)
208 end
216 end
209
217
210 - # report inactive
211 - grader_proc.report_inactive if grader_proc!=nil
212 -
You need to be logged in to leave comments. Login now