Description:
added default grading mode, changed log message in grader git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@393 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

r187:a677937ad928 - - 2 files changed: 7 inserted, 5 deleted

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