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 class GraderMessage < ActiveRecord::Base
1 class GraderMessage < ActiveRecord::Base
2
2
3 belongs_to :taken_grader_process, :class_name => :grader_process
3 belongs_to :taken_grader_process, :class_name => :grader_process
4
4
5 GRADE_SUBMISSION = 1
5 GRADE_SUBMISSION = 1
6 GRADE_TEST_REQUEST = 2
6 GRADE_TEST_REQUEST = 2
7 STOP = 3
7 STOP = 3
8
8
9 RECIPIENT_ANY = -1
9 RECIPIENT_ANY = -1
10
10
11 def self.create_message(recipient, command, options=nil, target_id=nil)
11 def self.create_message(recipient, command, options=nil, target_id=nil)
12 recipient_id = recipient
12 recipient_id = recipient
13 if recipient == :any
13 if recipient == :any
14 recipient_id = GraderMessage::RECIPIENT_ANY
14 recipient_id = GraderMessage::RECIPIENT_ANY
15 end
15 end
16
16
17 GraderMessage.create(:grader_process_id => recipient_id,
17 GraderMessage.create(:grader_process_id => recipient_id,
18 :command => command,
18 :command => command,
19 :options => options,
19 :options => options,
20 :target_id => target_id,
20 :target_id => target_id,
21 :taken => false)
21 :taken => false)
22 end
22 end
23
23
24 - def self.create_grade_submission(mode,submission)
24 + def self.create_grade_submission(submission,
25 + grading_environment="grading")
25 GraderMessage.create_message(:any,
26 GraderMessage.create_message(:any,
26 GraderMessage::GRADE_SUBMISSION,
27 GraderMessage::GRADE_SUBMISSION,
27 - mode,
28 + grading_environment,
28 submission.id)
29 submission.id)
29 end
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 GraderMessage.create_message(:any,
34 GraderMessage.create_message(:any,
33 GraderMessage::GRADE_TEST_REQUEST,
35 GraderMessage::GRADE_TEST_REQUEST,
34 - mode,
36 + grading_environment,
35 test_request.id)
37 test_request.id)
36 end
38 end
37
39
38 def self.create_stop(grader_process_id)
40 def self.create_stop(grader_process_id)
39 GraderMessage.create_message(grader_process_id,
41 GraderMessage.create_message(grader_process_id,
40 GraderMessage::STOP)
42 GraderMessage::STOP)
41 end
43 end
42
44
43 def self.get_message_for(recipient_id, accepting_commands=:all)
45 def self.get_message_for(recipient_id, accepting_commands=:all)
44 command_conditions =
46 command_conditions =
45 GraderMessage.build_command_conditions(accepting_commands)
47 GraderMessage.build_command_conditions(accepting_commands)
46 recp_conditions= "((`grader_process_id` = #{recipient_id.to_i})" +
48 recp_conditions= "((`grader_process_id` = #{recipient_id.to_i})" +
47 " OR (`grader_process_id` = #{GraderMessage::RECIPIENT_ANY}))"
49 " OR (`grader_process_id` = #{GraderMessage::RECIPIENT_ANY}))"
48
50
49 message = nil # need this to bind message in do-block for transaction
51 message = nil # need this to bind message in do-block for transaction
50 begin
52 begin
51 GraderMessage.transaction do
53 GraderMessage.transaction do
52 message = GraderMessage.find(:first,
54 message = GraderMessage.find(:first,
53 :order => "created_at",
55 :order => "created_at",
54 :conditions =>
56 :conditions =>
55 "(`taken` = 0)" +
57 "(`taken` = 0)" +
56 " AND (#{recp_conditions})" +
58 " AND (#{recp_conditions})" +
57 " AND (#{command_conditions})",
59 " AND (#{command_conditions})",
58 :lock => true)
60 :lock => true)
59 if message!=nil
61 if message!=nil
60 message.taken = true
62 message.taken = true
61 message.taken_grader_process_id = recipient_id
63 message.taken_grader_process_id = recipient_id
62 message.save!
64 message.save!
63 end
65 end
64 end
66 end
65
67
66 rescue
68 rescue
67 message = nil
69 message = nil
68
70
69 end
71 end
70
72
71 message
73 message
72 end
74 end
73
75
74 protected
76 protected
75
77
76 def self.build_command_conditions(accepting_commands)
78 def self.build_command_conditions(accepting_commands)
77 if accepting_commands==:all
79 if accepting_commands==:all
78 return '(1=1)'
80 return '(1=1)'
79 else
81 else
80 conds = []
82 conds = []
81 accepting_commands.each do |command|
83 accepting_commands.each do |command|
82 conds << "(`command` = #{command.to_i})"
84 conds << "(`command` = #{command.to_i})"
83 end
85 end
84 return "(" + conds.join(" OR ") + ")"
86 return "(" + conds.join(" OR ") + ")"
85 end
87 end
86 end
88 end
87
89
88 end
90 end
@@ -1,127 +1,127
1 #!/usr/bin/ruby
1 #!/usr/bin/ruby
2
2
3 def stop_grader(id)
3 def stop_grader(id)
4 if id==:all
4 if id==:all
5 File.open(File.dirname(__FILE__) + "/stop.all",'w').close
5 File.open(File.dirname(__FILE__) + "/stop.all",'w').close
6 else
6 else
7 File.open(File.dirname(__FILE__) + "/stop.#{id}",'w').close
7 File.open(File.dirname(__FILE__) + "/stop.#{id}",'w').close
8 end
8 end
9 end
9 end
10
10
11 def check_stopfile
11 def check_stopfile
12 FileTest.exist?(File.dirname(__FILE__) + "/stop.all") or
12 FileTest.exist?(File.dirname(__FILE__) + "/stop.all") or
13 FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}")
13 FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}")
14 end
14 end
15
15
16 def clear_stopfile
16 def clear_stopfile
17 if FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}")
17 if FileTest.exist?(File.dirname(__FILE__) + "/stop.#{Process.pid}")
18 system("rm " + File.dirname(__FILE__) + "/stop.#{Process.pid}")
18 system("rm " + File.dirname(__FILE__) + "/stop.#{Process.pid}")
19 end
19 end
20 end
20 end
21
21
22 def config
22 def config
23 Grader::Configuration.get_instance
23 Grader::Configuration.get_instance
24 end
24 end
25
25
26 def log_file_name
26 def log_file_name
27 if !File.exists?(config.log_dir)
27 if !File.exists?(config.log_dir)
28 raise "Log directory does not exist: #{config.log_dir}"
28 raise "Log directory does not exist: #{config.log_dir}"
29 end
29 end
30 config.log_dir +
30 config.log_dir +
31 - "/#{GRADER_ENV}_#{config.grader_mode}.#{Process.pid}"
31 + "/#{GRADER_ENV}.#{Process.pid}"
32 end
32 end
33
33
34 def log(str)
34 def log(str)
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
You need to be logged in to leave comments. Login now