Description:
renamed judge/script/grader to grader-batch
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@388 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r185:9fe786e3b2a3 - - 2 files changed: 217 inserted, 217 deleted
@@ -0,0 +1,217 | |||||
|
|
1 | + #!/usr/bin/ruby | ||
|
|
2 | + | ||
|
|
3 | + def stop_grader(id) | ||
|
|
4 | + if id==:all | ||
|
|
5 | + File.open(File.dirname(__FILE__) + "/stop.all",'w').close | ||
|
|
6 | + else | ||
|
|
7 | + File.open(File.dirname(__FILE__) + "/stop.#{id}",'w').close | ||
|
|
8 | + end | ||
|
|
9 | + end | ||
|
|
10 | + | ||
|
|
11 | + def check_stopfile | ||
|
|
12 | + FileTest.exist?(File.dirname(__FILE__) + "/stop.all") or | ||
|
|
13 | + FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}") | ||
|
|
14 | + end | ||
|
|
15 | + | ||
|
|
16 | + def clear_stopfile | ||
|
|
17 | + if FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}") | ||
|
|
18 | + system("rm " + File.dirname(__FILE__) + "/stop.#{Process.pid}") | ||
|
|
19 | + end | ||
|
|
20 | + end | ||
|
|
21 | + | ||
|
|
22 | + def config | ||
|
|
23 | + Grader::Configuration.get_instance | ||
|
|
24 | + end | ||
|
|
25 | + | ||
|
|
26 | + def log_file_name | ||
|
|
27 | + if !File.exists?(config.log_dir) | ||
|
|
28 | + raise "Log directory does not exist: #{config.log_dir}" | ||
|
|
29 | + end | ||
|
|
30 | + config.log_dir + | ||
|
|
31 | + "/#{GRADER_ENV}_#{config.grader_mode}.#{Process.pid}" | ||
|
|
32 | + end | ||
|
|
33 | + | ||
|
|
34 | + def log(str) | ||
|
|
35 | + if config.talkative | ||
|
|
36 | + puts str | ||
|
|
37 | + end | ||
|
|
38 | + if config.logging | ||
|
|
39 | + fp = File.open(log_file_name,"a") | ||
|
|
40 | + fp.puts("GRADER: #{Time.new.strftime("%H:%M")} #{str}") | ||
|
|
41 | + fp.close | ||
|
|
42 | + end | ||
|
|
43 | + end | ||
|
|
44 | + | ||
|
|
45 | + def display_manual | ||
|
|
46 | + puts <<USAGE | ||
|
|
47 | + Grader. | ||
|
|
48 | + using: (1) grader | ||
|
|
49 | + (2) grader environment [mode] | ||
|
|
50 | + (3) grader stop [all|pids-list] | ||
|
|
51 | + (4) grader --help | ||
|
|
52 | + (1) call grader with environment = 'exam', mode = 'queue' | ||
|
|
53 | + (2) possible modes are: 'queue', 'prob', 'test_request' | ||
|
|
54 | + (3) create stop-file to stop running grader in queue mode | ||
|
|
55 | + (4) You are here. | ||
|
|
56 | + USAGE | ||
|
|
57 | + end | ||
|
|
58 | + | ||
|
|
59 | + ######################################### | ||
|
|
60 | + # main program | ||
|
|
61 | + ######################################### | ||
|
|
62 | + | ||
|
|
63 | + # with --help | ||
|
|
64 | + if (ARGV.length==1) and (/help/.match(ARGV[0])) | ||
|
|
65 | + display_manual | ||
|
|
66 | + exit(0) | ||
|
|
67 | + end | ||
|
|
68 | + | ||
|
|
69 | + # reading environment and options | ||
|
|
70 | + if (ARGV.length >= 1) and (ARGV[0]=='stop') | ||
|
|
71 | + if ARGV.length==1 | ||
|
|
72 | + puts "you should specify pid-list or 'all'" | ||
|
|
73 | + display_manual | ||
|
|
74 | + elsif (ARGV.length==2) and (ARGV[1]=='all') | ||
|
|
75 | + stop_grader(:all) | ||
|
|
76 | + puts "A global stop file ('stop.all') created." | ||
|
|
77 | + puts "You should remove it manually later." | ||
|
|
78 | + else | ||
|
|
79 | + (1..ARGV.length-1).each do |i| | ||
|
|
80 | + stop_grader(ARGV[i]) | ||
|
|
81 | + end | ||
|
|
82 | + puts "stop file(s) created" | ||
|
|
83 | + end | ||
|
|
84 | + exit(0) | ||
|
|
85 | + end | ||
|
|
86 | + | ||
|
|
87 | + if check_stopfile | ||
|
|
88 | + puts "Stop file exists. Terminated." | ||
|
|
89 | + clear_stopfile | ||
|
|
90 | + exit(0) | ||
|
|
91 | + end | ||
|
|
92 | + | ||
|
|
93 | + grader_mode = 'queue' | ||
|
|
94 | + if ARGV.length >= 1 | ||
|
|
95 | + GRADER_ENV = ARGV[0] | ||
|
|
96 | + if ARGV.length >=2 | ||
|
|
97 | + grader_mode = ARGV[1] | ||
|
|
98 | + end | ||
|
|
99 | + else | ||
|
|
100 | + GRADER_ENV = 'exam' | ||
|
|
101 | + end | ||
|
|
102 | + | ||
|
|
103 | + puts "environment: #{GRADER_ENV}" | ||
|
|
104 | + require File.join(File.dirname(__FILE__),'config/environment') | ||
|
|
105 | + | ||
|
|
106 | + # add grader_mode to config | ||
|
|
107 | + # this is needed because method log needs it. TODO: clean this up | ||
|
|
108 | + class << config | ||
|
|
109 | + attr_accessor :grader_mode | ||
|
|
110 | + end | ||
|
|
111 | + config.grader_mode = grader_mode | ||
|
|
112 | + | ||
|
|
113 | + # reading rails environment | ||
|
|
114 | + log 'Reading rails environment' | ||
|
|
115 | + | ||
|
|
116 | + RAILS_ENV = config.rails_env | ||
|
|
117 | + require RAILS_ROOT + '/config/environment' | ||
|
|
118 | + | ||
|
|
119 | + # register grader process | ||
|
|
120 | + if config.report_grader | ||
|
|
121 | + grader_proc = GraderProcess.register(config.grader_hostname, | ||
|
|
122 | + Process.pid, | ||
|
|
123 | + grader_mode) | ||
|
|
124 | + else | ||
|
|
125 | + grader_proc = nil | ||
|
|
126 | + end | ||
|
|
127 | + | ||
|
|
128 | + #set loggin environment | ||
|
|
129 | + ENV['GRADER_LOGGING'] = log_file_name | ||
|
|
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 | + | ||
|
|
139 | + # | ||
|
|
140 | + # MAIN LOOP | ||
|
|
141 | + # | ||
|
|
142 | + | ||
|
|
143 | + case grader_mode | ||
|
|
144 | + when "queue", "test_request" | ||
|
|
145 | + log "Grader: #{grader_mode}" | ||
|
|
146 | + if grader_mode=="queue" | ||
|
|
147 | + engine = Grader::Engine.new | ||
|
|
148 | + else | ||
|
|
149 | + engine = Grader::Engine.new(Grader::TestRequestRoomMaker.new, | ||
|
|
150 | + Grader::TestRequestReporter.new) | ||
|
|
151 | + end | ||
|
|
152 | + | ||
|
|
153 | + runner = Grader::Runner.new(engine, grader_proc) | ||
|
|
154 | + while true | ||
|
|
155 | + | ||
|
|
156 | + if check_stopfile # created by calling grader stop | ||
|
|
157 | + clear_stopfile | ||
|
|
158 | + log "stopped (with stop file)" | ||
|
|
159 | + break | ||
|
|
160 | + end | ||
|
|
161 | + | ||
|
|
162 | + if grader_mode=="queue" | ||
|
|
163 | + task = runner.grade_oldest_task | ||
|
|
164 | + else | ||
|
|
165 | + task = runner.grade_oldest_test_request | ||
|
|
166 | + end | ||
|
|
167 | + if task==nil | ||
|
|
168 | + sleep(1) | ||
|
|
169 | + end | ||
|
|
170 | + end | ||
|
|
171 | + | ||
|
|
172 | + when "prob" | ||
|
|
173 | + engine = Grader::Engine.new | ||
|
|
174 | + runner = Grader::Runner.new(engine, grader_proc) | ||
|
|
175 | + | ||
|
|
176 | + grader_proc.report_active if grader_proc!=nil | ||
|
|
177 | + | ||
|
|
178 | + ARGV.shift | ||
|
|
179 | + ARGV.shift | ||
|
|
180 | + | ||
|
|
181 | + ARGV.each do |prob_name| | ||
|
|
182 | + prob = Problem.find_by_name(prob_name) | ||
|
|
183 | + if prob==nil | ||
|
|
184 | + puts "cannot find problem: #{prob_name}" | ||
|
|
185 | + else | ||
|
|
186 | + runner.grade_problem(prob) | ||
|
|
187 | + end | ||
|
|
188 | + end | ||
|
|
189 | + | ||
|
|
190 | + when "sub" | ||
|
|
191 | + engine = Grader::Engine.new | ||
|
|
192 | + runner = Grader::Runner.new(engine, grader_proc) | ||
|
|
193 | + | ||
|
|
194 | + grader_proc.report_active if grader_proc!=nil | ||
|
|
195 | + | ||
|
|
196 | + ARGV.shift | ||
|
|
197 | + ARGV.shift | ||
|
|
198 | + | ||
|
|
199 | + ARGV.each do |sub_id| | ||
|
|
200 | + puts "Grading #{sub_id}" | ||
|
|
201 | + begin | ||
|
|
202 | + submission = Submission.find(sub_id.to_i) | ||
|
|
203 | + rescue ActiveRecord::RecordNotFound | ||
|
|
204 | + puts "Record not found" | ||
|
|
205 | + submission = nil | ||
|
|
206 | + end | ||
|
|
207 | + | ||
|
|
208 | + if submission!=nil | ||
|
|
209 | + runner.grade_submission(submission) | ||
|
|
210 | + end | ||
|
|
211 | + end | ||
|
|
212 | + | ||
|
|
213 | + else | ||
|
|
214 | + display_manual | ||
|
|
215 | + exit(0) | ||
|
|
216 | + end | ||
|
|
217 | + |
deleted file |
You need to be logged in to leave comments.
Login now