Description:
[grader] raise when log directory does not exist
git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@108 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
r25:8541b6e657f1 - - 1 file changed: 3 inserted, 0 deleted
@@ -1,181 +1,184 | |||
|
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 | + if !File.exists?(config.log_dir) | |
|
28 | + raise "Log directory does not exist: #{config.log_dir}" | |
|
29 | + end | |
|
27 | 30 | config.log_dir + |
|
28 | 31 | "/#{GRADER_ENV}_#{config.grader_mode}.#{Process.pid}" |
|
29 | 32 | end |
|
30 | 33 | |
|
31 | 34 | def log(str) |
|
32 | 35 | if config.talkative |
|
33 | 36 | puts str |
|
34 | 37 | end |
|
35 | 38 | if config.logging |
|
36 | 39 | fp = File.open(log_file_name,"a") |
|
37 | 40 | fp.puts("GRADER: #{Time.new.strftime("%H:%M")} #{str}") |
|
38 | 41 | fp.close |
|
39 | 42 | end |
|
40 | 43 | end |
|
41 | 44 | |
|
42 | 45 | def display_manual |
|
43 | 46 | puts <<USAGE |
|
44 | 47 | Grader. |
|
45 | 48 | using: (1) grader |
|
46 | 49 | (2) grader environment [mode] |
|
47 | 50 | (3) grader stop [all|pids-list] |
|
48 | 51 | (4) grader --help |
|
49 | 52 | (1) call grader with environment = 'exam', mode = 'queue' |
|
50 | 53 | (2) possible modes are: 'queue', 'prob', 'test_request' |
|
51 | 54 | (3) create stop-file to stop running grader in queue mode |
|
52 | 55 | (4) You are here. |
|
53 | 56 | USAGE |
|
54 | 57 | end |
|
55 | 58 | |
|
56 | 59 | ######################################### |
|
57 | 60 | # main program |
|
58 | 61 | ######################################### |
|
59 | 62 | |
|
60 | 63 | # with --help |
|
61 | 64 | if (ARGV.length==1) and (/help/.match(ARGV[0])) |
|
62 | 65 | display_manual |
|
63 | 66 | exit(0) |
|
64 | 67 | end |
|
65 | 68 | |
|
66 | 69 | # reading environment and options |
|
67 | 70 | if (ARGV.length >= 1) and (ARGV[0]=='stop') |
|
68 | 71 | if ARGV.length==1 |
|
69 | 72 | puts "you should specify pid-list or 'all'" |
|
70 | 73 | display_manual |
|
71 | 74 | elsif (ARGV.length==2) and (ARGV[1]=='all') |
|
72 | 75 | stop_grader(:all) |
|
73 | 76 | puts "A global stop file ('stop.all') created." |
|
74 | 77 | puts "You should remove it manually later." |
|
75 | 78 | else |
|
76 | 79 | (1..ARGV.length-1).each do |i| |
|
77 | 80 | stop_grader(ARGV[i]) |
|
78 | 81 | end |
|
79 | 82 | puts "stop file(s) created" |
|
80 | 83 | end |
|
81 | 84 | exit(0) |
|
82 | 85 | end |
|
83 | 86 | |
|
84 | 87 | if check_stopfile |
|
85 | 88 | puts "Stop file exists. Terminated." |
|
86 | 89 | clear_stopfile |
|
87 | 90 | exit(0) |
|
88 | 91 | end |
|
89 | 92 | |
|
90 | 93 | grader_mode = 'queue' |
|
91 | 94 | if ARGV.length >= 1 |
|
92 | 95 | GRADER_ENV = ARGV[0] |
|
93 | 96 | if ARGV.length >=2 |
|
94 | 97 | grader_mode = ARGV[1] |
|
95 | 98 | end |
|
96 | 99 | else |
|
97 | 100 | GRADER_ENV = 'exam' |
|
98 | 101 | end |
|
99 | 102 | |
|
100 | 103 | puts "environment: #{GRADER_ENV}" |
|
101 | 104 | require File.join(File.dirname(__FILE__),'config/environment') |
|
102 | 105 | |
|
103 | 106 | # add grader_mode to config |
|
104 | 107 | # this is needed because method log needs it. TODO: clean this up |
|
105 | 108 | class << config |
|
106 | 109 | attr_accessor :grader_mode |
|
107 | 110 | end |
|
108 | 111 | config.grader_mode = grader_mode |
|
109 | 112 | |
|
110 | 113 | # reading rails environment |
|
111 | 114 | log 'Reading rails environment' |
|
112 | 115 | |
|
113 | 116 | RAILS_ENV = config.rails_env |
|
114 | 117 | require RAILS_ROOT + '/config/environment' |
|
115 | 118 | |
|
116 | 119 | # register grader process |
|
117 | 120 | if config.report_grader |
|
118 | 121 | grader_proc = GraderProcess.register(config.grader_hostname, |
|
119 | 122 | Process.pid, |
|
120 | 123 | grader_mode) |
|
121 | 124 | else |
|
122 | 125 | grader_proc = nil |
|
123 | 126 | end |
|
124 | 127 | |
|
125 | 128 | #set loggin environment |
|
126 | 129 | ENV['GRADER_LOGGING'] = log_file_name |
|
127 | 130 | |
|
128 | 131 | # |
|
129 | 132 | # MAIN LOOP |
|
130 | 133 | # |
|
131 | 134 | |
|
132 | 135 | case grader_mode |
|
133 | 136 | when "queue", "test_request" |
|
134 | 137 | log "Grader: #{grader_mode}" |
|
135 | 138 | if grader_mode=="queue" |
|
136 | 139 | engine = Grader::Engine.new |
|
137 | 140 | else |
|
138 | 141 | engine = Grader::Engine.new(Grader::TestRequestRoomMaker.new, |
|
139 | 142 | Grader::TestRequestReporter.new) |
|
140 | 143 | end |
|
141 | 144 | |
|
142 | 145 | runner = Grader::Runner.new(engine, grader_proc) |
|
143 | 146 | while true |
|
144 | 147 | |
|
145 | 148 | if check_stopfile # created by calling grader stop |
|
146 | 149 | clear_stopfile |
|
147 | 150 | log "stopped (with stop file)" |
|
148 | 151 | break |
|
149 | 152 | end |
|
150 | 153 | |
|
151 | 154 | if grader_mode=="queue" |
|
152 | 155 | task = runner.grade_oldest_task |
|
153 | 156 | else |
|
154 | 157 | task = runner.grade_oldest_test_request |
|
155 | 158 | end |
|
156 | 159 | if task==nil |
|
157 | 160 | sleep(1) |
|
158 | 161 | end |
|
159 | 162 | end |
|
160 | 163 | |
|
161 | 164 | when "prob" |
|
162 | 165 | engine = Grader::Engine.new |
|
163 | 166 | runner = Grader::Runner.new(engine, grader_proc) |
|
164 | 167 | |
|
165 | 168 | grader_proc.report_active if grader_proc!=nil |
|
166 | 169 | |
|
167 | 170 | prob = Problem.find_by_name(ARGV[2]) |
|
168 | 171 | if prob==nil |
|
169 | 172 | puts "cannot find problem: #{ARGV[2]}" |
|
170 | 173 | else |
|
171 | 174 | runner.grade_problem(prob) |
|
172 | 175 | end |
|
173 | 176 | |
|
174 | 177 | else |
|
175 | 178 | display_manual |
|
176 | 179 | exit(0) |
|
177 | 180 | end |
|
178 | 181 | |
|
179 | 182 | # report inactive |
|
180 | 183 | grader_proc.report_inactive if grader_proc!=nil |
|
181 | 184 |
You need to be logged in to leave comments.
Login now