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:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

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
@@ -9,7 +9,7
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 => 20090426131044) do
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"
@@ -42,6 +42,16
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"
You need to be logged in to leave comments. Login now