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,140 +1,150 | |||||
|
1 | # This file is auto-generated from the current state of the database. Instead of editing this file, |
|
1 | # This file is auto-generated from the current state of the database. Instead of editing this file, |
|
2 | # please use the migrations feature of Active Record to incrementally modify your database, and |
|
2 | # please use the migrations feature of Active Record to incrementally modify your database, and |
|
3 | # then regenerate this schema definition. |
|
3 | # then regenerate this schema definition. |
|
4 | # |
|
4 | # |
|
5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need |
|
5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need |
|
6 | # to create the application database on another system, you should be using db:schema:load, not running |
|
6 | # to create the application database on another system, you should be using db:schema:load, not running |
|
7 | # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations |
|
7 | # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations |
|
8 | # you'll amass, the slower it'll run and the greater likelihood for issues). |
|
8 | # you'll amass, the slower it'll run and the greater likelihood for issues). |
|
9 | # |
|
9 | # |
|
10 | # It's strongly recommended to check this file into your version control system. |
|
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 | create_table "announcements", :force => true do |t| |
|
14 | create_table "announcements", :force => true do |t| |
|
15 | t.string "author" |
|
15 | t.string "author" |
|
16 | t.text "body" |
|
16 | t.text "body" |
|
17 | t.boolean "published" |
|
17 | t.boolean "published" |
|
18 | t.datetime "created_at" |
|
18 | t.datetime "created_at" |
|
19 | t.datetime "updated_at" |
|
19 | t.datetime "updated_at" |
|
20 | t.boolean "frontpage", :default => false |
|
20 | t.boolean "frontpage", :default => false |
|
21 | t.boolean "contest_only", :default => false |
|
21 | t.boolean "contest_only", :default => false |
|
22 | end |
|
22 | end |
|
23 |
|
23 | ||
|
24 | create_table "configurations", :force => true do |t| |
|
24 | create_table "configurations", :force => true do |t| |
|
25 | t.string "key" |
|
25 | t.string "key" |
|
26 | t.string "value_type" |
|
26 | t.string "value_type" |
|
27 | t.string "value" |
|
27 | t.string "value" |
|
28 | t.datetime "created_at" |
|
28 | t.datetime "created_at" |
|
29 | t.datetime "updated_at" |
|
29 | t.datetime "updated_at" |
|
30 | end |
|
30 | end |
|
31 |
|
31 | ||
|
32 | create_table "countries", :force => true do |t| |
|
32 | create_table "countries", :force => true do |t| |
|
33 | t.string "name" |
|
33 | t.string "name" |
|
34 | t.datetime "created_at" |
|
34 | t.datetime "created_at" |
|
35 | t.datetime "updated_at" |
|
35 | t.datetime "updated_at" |
|
36 | end |
|
36 | end |
|
37 |
|
37 | ||
|
38 | create_table "descriptions", :force => true do |t| |
|
38 | create_table "descriptions", :force => true do |t| |
|
39 | t.text "body" |
|
39 | t.text "body" |
|
40 | t.boolean "markdowned" |
|
40 | t.boolean "markdowned" |
|
41 | t.datetime "created_at" |
|
41 | t.datetime "created_at" |
|
42 | t.datetime "updated_at" |
|
42 | t.datetime "updated_at" |
|
43 | end |
|
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 | create_table "grader_processes", :force => true do |t| |
|
55 | create_table "grader_processes", :force => true do |t| |
|
46 | t.string "host", :limit => 20 |
|
56 | t.string "host", :limit => 20 |
|
47 | t.integer "pid" |
|
57 | t.integer "pid" |
|
48 | t.string "mode" |
|
58 | t.string "mode" |
|
49 | t.boolean "active" |
|
59 | t.boolean "active" |
|
50 | t.datetime "created_at" |
|
60 | t.datetime "created_at" |
|
51 | t.datetime "updated_at" |
|
61 | t.datetime "updated_at" |
|
52 | t.integer "task_id" |
|
62 | t.integer "task_id" |
|
53 | t.string "task_type" |
|
63 | t.string "task_type" |
|
54 | t.boolean "terminated" |
|
64 | t.boolean "terminated" |
|
55 | end |
|
65 | end |
|
56 |
|
66 | ||
|
57 | add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid" |
|
67 | add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid" |
|
58 |
|
68 | ||
|
59 | create_table "languages", :force => true do |t| |
|
69 | create_table "languages", :force => true do |t| |
|
60 | t.string "name", :limit => 10 |
|
70 | t.string "name", :limit => 10 |
|
61 | t.string "pretty_name" |
|
71 | t.string "pretty_name" |
|
62 | t.string "ext", :limit => 10 |
|
72 | t.string "ext", :limit => 10 |
|
63 | t.string "common_ext" |
|
73 | t.string "common_ext" |
|
64 | end |
|
74 | end |
|
65 |
|
75 | ||
|
66 | create_table "messages", :force => true do |t| |
|
76 | create_table "messages", :force => true do |t| |
|
67 | t.integer "sender_id" |
|
77 | t.integer "sender_id" |
|
68 | t.integer "receiver_id" |
|
78 | t.integer "receiver_id" |
|
69 | t.integer "replying_message_id" |
|
79 | t.integer "replying_message_id" |
|
70 | t.text "body" |
|
80 | t.text "body" |
|
71 | t.boolean "replied" |
|
81 | t.boolean "replied" |
|
72 | t.datetime "created_at" |
|
82 | t.datetime "created_at" |
|
73 | t.datetime "updated_at" |
|
83 | t.datetime "updated_at" |
|
74 | end |
|
84 | end |
|
75 |
|
85 | ||
|
76 | create_table "problems", :force => true do |t| |
|
86 | create_table "problems", :force => true do |t| |
|
77 | t.string "name", :limit => 30 |
|
87 | t.string "name", :limit => 30 |
|
78 | t.string "full_name" |
|
88 | t.string "full_name" |
|
79 | t.integer "full_score" |
|
89 | t.integer "full_score" |
|
80 | t.date "date_added" |
|
90 | t.date "date_added" |
|
81 | t.boolean "available" |
|
91 | t.boolean "available" |
|
82 | t.string "url" |
|
92 | t.string "url" |
|
83 | t.integer "description_id" |
|
93 | t.integer "description_id" |
|
84 | t.boolean "test_allowed" |
|
94 | t.boolean "test_allowed" |
|
85 | t.boolean "output_only" |
|
95 | t.boolean "output_only" |
|
86 | end |
|
96 | end |
|
87 |
|
97 | ||
|
88 | create_table "rights", :force => true do |t| |
|
98 | create_table "rights", :force => true do |t| |
|
89 | t.string "name" |
|
99 | t.string "name" |
|
90 | t.string "controller" |
|
100 | t.string "controller" |
|
91 | t.string "action" |
|
101 | t.string "action" |
|
92 | end |
|
102 | end |
|
93 |
|
103 | ||
|
94 | create_table "rights_roles", :id => false, :force => true do |t| |
|
104 | create_table "rights_roles", :id => false, :force => true do |t| |
|
95 | t.integer "right_id" |
|
105 | t.integer "right_id" |
|
96 | t.integer "role_id" |
|
106 | t.integer "role_id" |
|
97 | end |
|
107 | end |
|
98 |
|
108 | ||
|
99 | add_index "rights_roles", ["role_id"], :name => "index_rights_roles_on_role_id" |
|
109 | add_index "rights_roles", ["role_id"], :name => "index_rights_roles_on_role_id" |
|
100 |
|
110 | ||
|
101 | create_table "roles", :force => true do |t| |
|
111 | create_table "roles", :force => true do |t| |
|
102 | t.string "name" |
|
112 | t.string "name" |
|
103 | end |
|
113 | end |
|
104 |
|
114 | ||
|
105 | create_table "roles_users", :id => false, :force => true do |t| |
|
115 | create_table "roles_users", :id => false, :force => true do |t| |
|
106 | t.integer "role_id" |
|
116 | t.integer "role_id" |
|
107 | t.integer "user_id" |
|
117 | t.integer "user_id" |
|
108 | end |
|
118 | end |
|
109 |
|
119 | ||
|
110 | add_index "roles_users", ["user_id"], :name => "index_roles_users_on_user_id" |
|
120 | add_index "roles_users", ["user_id"], :name => "index_roles_users_on_user_id" |
|
111 |
|
121 | ||
|
112 | create_table "sessions", :force => true do |t| |
|
122 | create_table "sessions", :force => true do |t| |
|
113 | t.string "session_id" |
|
123 | t.string "session_id" |
|
114 | t.text "data" |
|
124 | t.text "data" |
|
115 | t.datetime "updated_at" |
|
125 | t.datetime "updated_at" |
|
116 | end |
|
126 | end |
|
117 |
|
127 | ||
|
118 | add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" |
|
128 | add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" |
|
119 | add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" |
|
129 | add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" |
|
120 |
|
130 | ||
|
121 | create_table "sites", :force => true do |t| |
|
131 | create_table "sites", :force => true do |t| |
|
122 | t.string "name" |
|
132 | t.string "name" |
|
123 | t.boolean "started" |
|
133 | t.boolean "started" |
|
124 | t.datetime "start_time" |
|
134 | t.datetime "start_time" |
|
125 | t.datetime "created_at" |
|
135 | t.datetime "created_at" |
|
126 | t.datetime "updated_at" |
|
136 | t.datetime "updated_at" |
|
127 | t.integer "country_id" |
|
137 | t.integer "country_id" |
|
128 | t.string "password" |
|
138 | t.string "password" |
|
129 | end |
|
139 | end |
|
130 |
|
140 | ||
|
131 | create_table "submissions", :force => true do |t| |
|
141 | create_table "submissions", :force => true do |t| |
|
132 | t.integer "user_id" |
|
142 | t.integer "user_id" |
|
133 | t.integer "problem_id" |
|
143 | t.integer "problem_id" |
|
134 | t.integer "language_id" |
|
144 | t.integer "language_id" |
|
135 | t.text "source" |
|
145 | t.text "source" |
|
136 | t.binary "binary" |
|
146 | t.binary "binary" |
|
137 | t.datetime "submitted_at" |
|
147 | t.datetime "submitted_at" |
|
138 | t.datetime "compiled_at" |
|
148 | t.datetime "compiled_at" |
|
139 | t.text "compiler_message" |
|
149 | t.text "compiler_message" |
|
140 | t.datetime "graded_at" |
|
150 | t.datetime "graded_at" |
You need to be logged in to leave comments.
Login now