Description:
added simple grader messaging service
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@385 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
r182:f58cdc625e87 - - 5 files changed: 127 inserted, 1 deleted
@@ -0,0 +1,85 | |||
|
1 | + class GraderMessage < ActiveRecord::Base | |
|
2 | + | |
|
3 | + GRADE_SUBMISSION = 1 | |
|
4 | + GRADE_TEST_REQUEST = 2 | |
|
5 | + STOP = 3 | |
|
6 | + | |
|
7 | + def self.create_message(recipient, command, options=nil, target_id=nil) | |
|
8 | + recipient_id = recipient | |
|
9 | + if recipient == :all | |
|
10 | + recipient_id = -1 | |
|
11 | + elsif recipient == :any | |
|
12 | + recipient_id = 0 | |
|
13 | + end | |
|
14 | + | |
|
15 | + GraderMessage.create(:grader_process_id => recipient_id, | |
|
16 | + :command => command, | |
|
17 | + :options => options, | |
|
18 | + :target_id => target_id, | |
|
19 | + :taken => false) | |
|
20 | + end | |
|
21 | + | |
|
22 | + def self.create_grade_submission(mode,submission) | |
|
23 | + GraderMessage.create_message(:any, | |
|
24 | + GraderMessage::GRADE_SUBMISSION, | |
|
25 | + mode, | |
|
26 | + submission.id) | |
|
27 | + end | |
|
28 | + | |
|
29 | + def self.create_grade_test_request(mode,test_request) | |
|
30 | + GraderMessage.create_message(:any, | |
|
31 | + GraderMessage::GRADE_TEST_REQUEST, | |
|
32 | + mode, | |
|
33 | + test_request.id) | |
|
34 | + end | |
|
35 | + | |
|
36 | + def self.create_stop(grader_process_id) | |
|
37 | + GraderMessage.create_message(grader_process_id, | |
|
38 | + GraderMessage::STOP) | |
|
39 | + end | |
|
40 | + | |
|
41 | + def self.get_message_for(recipient_id, accepting_commands=:all) | |
|
42 | + command_conditions = | |
|
43 | + GraderMessage.build_command_conditions(accepting_commands) | |
|
44 | + recp_conditions= "((`grader_process_id` = #{recipient_id.to_i})" + | |
|
45 | + " OR (`grader_process_id` = 0) OR (`grader_process_id` = -1))" | |
|
46 | + | |
|
47 | + message = nil # need this to bind message in do-block for transaction | |
|
48 | + begin | |
|
49 | + GraderMessage.transaction do | |
|
50 | + message = GraderMessage.find(:first, | |
|
51 | + :order => "created_at", | |
|
52 | + :conditions => | |
|
53 | + "(`taken`=0)" + | |
|
54 | + "AND (#{recp_conditions})" + | |
|
55 | + " AND (#{command_conditions})", | |
|
56 | + :lock => true) | |
|
57 | + if message!=nil | |
|
58 | + message.taken = true | |
|
59 | + message.save! | |
|
60 | + end | |
|
61 | + end | |
|
62 | + | |
|
63 | + rescue | |
|
64 | + message = nil | |
|
65 | + | |
|
66 | + end | |
|
67 | + | |
|
68 | + message | |
|
69 | + end | |
|
70 | + | |
|
71 | + protected | |
|
72 | + | |
|
73 | + def self.build_command_conditions(accepting_commands) | |
|
74 | + if accepting_commands==:all | |
|
75 | + return '(1=1)' | |
|
76 | + else | |
|
77 | + conds = [] | |
|
78 | + accepting_commands.each do |command| | |
|
79 | + conds << "(`command` = #{command.to_i})" | |
|
80 | + end | |
|
81 | + return "(" + conds.join(" OR ") + ")" | |
|
82 | + end | |
|
83 | + end | |
|
84 | + | |
|
85 | + end |
@@ -0,0 +1,16 | |||
|
1 | + class CreateGraderMessages < ActiveRecord::Migration | |
|
2 | + def self.up | |
|
3 | + create_table :grader_messages do |t| | |
|
4 | + t.integer :grader_process_id | |
|
5 | + t.integer :command | |
|
6 | + t.string :options | |
|
7 | + t.integer :target_id | |
|
8 | + t.boolean :taken | |
|
9 | + t.timestamps | |
|
10 | + end | |
|
11 | + end | |
|
12 | + | |
|
13 | + def self.down | |
|
14 | + drop_table :grader_messages | |
|
15 | + end | |
|
16 | + end |
@@ -0,0 +1,7 | |||
|
1 | + # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | |
|
2 | + | |
|
3 | + # one: | |
|
4 | + # column: value | |
|
5 | + # | |
|
6 | + # two: | |
|
7 | + # column: value |
@@ -0,0 +1,8 | |||
|
1 | + require 'test_helper' | |
|
2 | + | |
|
3 | + class GraderMessageTest < ActiveSupport::TestCase | |
|
4 | + # Replace this with your real tests. | |
|
5 | + test "the truth" do | |
|
6 | + assert true | |
|
7 | + end | |
|
8 | + end |
@@ -1,195 +1,205 | |||
|
1 | 1 | # This file is auto-generated from the current state of the database. Instead of editing this file, |
|
2 | 2 | # please use the migrations feature of Active Record to incrementally modify your database, and |
|
3 | 3 | # then regenerate this schema definition. |
|
4 | 4 | # |
|
5 | 5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need |
|
6 | 6 | # to create the application database on another system, you should be using db:schema:load, not running |
|
7 | 7 | # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations |
|
8 | 8 | # you'll amass, the slower it'll run and the greater likelihood for issues). |
|
9 | 9 | # |
|
10 | 10 | # It's strongly recommended to check this file into your version control system. |
|
11 | 11 | |
|
12 |
- ActiveRecord::Schema.define(:version => 2009042 |
|
|
12 | + ActiveRecord::Schema.define(:version => 20090429014554) do | |
|
13 | 13 | |
|
14 | 14 | create_table "announcements", :force => true do |t| |
|
15 | 15 | t.string "author" |
|
16 | 16 | t.text "body" |
|
17 | 17 | t.boolean "published" |
|
18 | 18 | t.datetime "created_at" |
|
19 | 19 | t.datetime "updated_at" |
|
20 | 20 | t.boolean "frontpage", :default => false |
|
21 | 21 | t.boolean "contest_only", :default => false |
|
22 | 22 | end |
|
23 | 23 | |
|
24 | 24 | create_table "configurations", :force => true do |t| |
|
25 | 25 | t.string "key" |
|
26 | 26 | t.string "value_type" |
|
27 | 27 | t.string "value" |
|
28 | 28 | t.datetime "created_at" |
|
29 | 29 | t.datetime "updated_at" |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | create_table "countries", :force => true do |t| |
|
33 | 33 | t.string "name" |
|
34 | 34 | t.datetime "created_at" |
|
35 | 35 | t.datetime "updated_at" |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | create_table "descriptions", :force => true do |t| |
|
39 | 39 | t.text "body" |
|
40 | 40 | t.boolean "markdowned" |
|
41 | 41 | t.datetime "created_at" |
|
42 | 42 | t.datetime "updated_at" |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | + create_table "grader_messages", :force => true do |t| | |
|
46 | + t.integer "grader_process_id" | |
|
47 | + t.integer "command" | |
|
48 | + t.string "options" | |
|
49 | + t.integer "target_id" | |
|
50 | + t.boolean "taken" | |
|
51 | + t.datetime "created_at" | |
|
52 | + t.datetime "updated_at" | |
|
53 | + end | |
|
54 | + | |
|
45 | 55 | create_table "grader_processes", :force => true do |t| |
|
46 | 56 | t.string "host", :limit => 20 |
|
47 | 57 | t.integer "pid" |
|
48 | 58 | t.string "mode" |
|
49 | 59 | t.boolean "active" |
|
50 | 60 | t.datetime "created_at" |
|
51 | 61 | t.datetime "updated_at" |
|
52 | 62 | t.integer "task_id" |
|
53 | 63 | t.string "task_type" |
|
54 | 64 | t.boolean "terminated" |
|
55 | 65 | end |
|
56 | 66 | |
|
57 | 67 | add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid" |
|
58 | 68 | |
|
59 | 69 | create_table "languages", :force => true do |t| |
|
60 | 70 | t.string "name", :limit => 10 |
|
61 | 71 | t.string "pretty_name" |
|
62 | 72 | t.string "ext", :limit => 10 |
|
63 | 73 | t.string "common_ext" |
|
64 | 74 | end |
|
65 | 75 | |
|
66 | 76 | create_table "messages", :force => true do |t| |
|
67 | 77 | t.integer "sender_id" |
|
68 | 78 | t.integer "receiver_id" |
|
69 | 79 | t.integer "replying_message_id" |
|
70 | 80 | t.text "body" |
|
71 | 81 | t.boolean "replied" |
|
72 | 82 | t.datetime "created_at" |
|
73 | 83 | t.datetime "updated_at" |
|
74 | 84 | end |
|
75 | 85 | |
|
76 | 86 | create_table "problems", :force => true do |t| |
|
77 | 87 | t.string "name", :limit => 30 |
|
78 | 88 | t.string "full_name" |
|
79 | 89 | t.integer "full_score" |
|
80 | 90 | t.date "date_added" |
|
81 | 91 | t.boolean "available" |
|
82 | 92 | t.string "url" |
|
83 | 93 | t.integer "description_id" |
|
84 | 94 | t.boolean "test_allowed" |
|
85 | 95 | t.boolean "output_only" |
|
86 | 96 | end |
|
87 | 97 | |
|
88 | 98 | create_table "rights", :force => true do |t| |
|
89 | 99 | t.string "name" |
|
90 | 100 | t.string "controller" |
|
91 | 101 | t.string "action" |
|
92 | 102 | end |
|
93 | 103 | |
|
94 | 104 | create_table "rights_roles", :id => false, :force => true do |t| |
|
95 | 105 | t.integer "right_id" |
|
96 | 106 | t.integer "role_id" |
|
97 | 107 | end |
|
98 | 108 | |
|
99 | 109 | add_index "rights_roles", ["role_id"], :name => "index_rights_roles_on_role_id" |
|
100 | 110 | |
|
101 | 111 | create_table "roles", :force => true do |t| |
|
102 | 112 | t.string "name" |
|
103 | 113 | end |
|
104 | 114 | |
|
105 | 115 | create_table "roles_users", :id => false, :force => true do |t| |
|
106 | 116 | t.integer "role_id" |
|
107 | 117 | t.integer "user_id" |
|
108 | 118 | end |
|
109 | 119 | |
|
110 | 120 | add_index "roles_users", ["user_id"], :name => "index_roles_users_on_user_id" |
|
111 | 121 | |
|
112 | 122 | create_table "sessions", :force => true do |t| |
|
113 | 123 | t.string "session_id" |
|
114 | 124 | t.text "data" |
|
115 | 125 | t.datetime "updated_at" |
|
116 | 126 | end |
|
117 | 127 | |
|
118 | 128 | add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" |
|
119 | 129 | add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" |
|
120 | 130 | |
|
121 | 131 | create_table "sites", :force => true do |t| |
|
122 | 132 | t.string "name" |
|
123 | 133 | t.boolean "started" |
|
124 | 134 | t.datetime "start_time" |
|
125 | 135 | t.datetime "created_at" |
|
126 | 136 | t.datetime "updated_at" |
|
127 | 137 | t.integer "country_id" |
|
128 | 138 | t.string "password" |
|
129 | 139 | end |
|
130 | 140 | |
|
131 | 141 | create_table "submissions", :force => true do |t| |
|
132 | 142 | t.integer "user_id" |
|
133 | 143 | t.integer "problem_id" |
|
134 | 144 | t.integer "language_id" |
|
135 | 145 | t.text "source" |
|
136 | 146 | t.binary "binary" |
|
137 | 147 | t.datetime "submitted_at" |
|
138 | 148 | t.datetime "compiled_at" |
|
139 | 149 | t.text "compiler_message" |
|
140 | 150 | t.datetime "graded_at" |
|
141 | 151 | t.integer "points" |
|
142 | 152 | t.text "grader_comment" |
|
143 | 153 | t.integer "number" |
|
144 | 154 | t.string "source_filename" |
|
145 | 155 | end |
|
146 | 156 | |
|
147 | 157 | add_index "submissions", ["user_id", "problem_id", "number"], :name => "index_submissions_on_user_id_and_problem_id_and_number", :unique => true |
|
148 | 158 | add_index "submissions", ["user_id", "problem_id"], :name => "index_submissions_on_user_id_and_problem_id" |
|
149 | 159 | |
|
150 | 160 | create_table "tasks", :force => true do |t| |
|
151 | 161 | t.integer "submission_id" |
|
152 | 162 | t.datetime "created_at" |
|
153 | 163 | t.integer "status" |
|
154 | 164 | t.datetime "updated_at" |
|
155 | 165 | end |
|
156 | 166 | |
|
157 | 167 | create_table "test_requests", :force => true do |t| |
|
158 | 168 | t.integer "user_id" |
|
159 | 169 | t.integer "problem_id" |
|
160 | 170 | t.integer "submission_id" |
|
161 | 171 | t.string "input_file_name" |
|
162 | 172 | t.string "output_file_name" |
|
163 | 173 | t.string "running_stat" |
|
164 | 174 | t.integer "status" |
|
165 | 175 | t.datetime "updated_at" |
|
166 | 176 | t.datetime "submitted_at" |
|
167 | 177 | t.datetime "compiled_at" |
|
168 | 178 | t.text "compiler_message" |
|
169 | 179 | t.datetime "graded_at" |
|
170 | 180 | t.string "grader_comment" |
|
171 | 181 | t.datetime "created_at" |
|
172 | 182 | t.float "running_time" |
|
173 | 183 | t.string "exit_status" |
|
174 | 184 | t.integer "memory_usage" |
|
175 | 185 | end |
|
176 | 186 | |
|
177 | 187 | add_index "test_requests", ["user_id", "problem_id"], :name => "index_test_requests_on_user_id_and_problem_id" |
|
178 | 188 | |
|
179 | 189 | create_table "users", :force => true do |t| |
|
180 | 190 | t.string "login", :limit => 50 |
|
181 | 191 | t.string "full_name" |
|
182 | 192 | t.string "hashed_password" |
|
183 | 193 | t.string "salt", :limit => 5 |
|
184 | 194 | t.string "alias" |
|
185 | 195 | t.string "email" |
|
186 | 196 | t.integer "site_id" |
|
187 | 197 | t.integer "country_id" |
|
188 | 198 | t.boolean "activated", :default => false |
|
189 | 199 | t.datetime "created_at" |
|
190 | 200 | t.datetime "updated_at" |
|
191 | 201 | end |
|
192 | 202 | |
|
193 | 203 | add_index "users", ["login"], :name => "index_users_on_login", :unique => true |
|
194 | 204 | |
|
195 | 205 | end |
You need to be logged in to leave comments.
Login now