Class: DistributedRails::Task

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/distributed_rails/task.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.distribute_taskObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/distributed_rails/task.rb', line 7

def self.distribute_task
  # 10% of the time, verify an already completed task
  if Random.rand(10) == 0
    task = self.next_verification_task
    # the other 90% of the time, take a task from the queue
  else
    task = self.next_ready_task
    if !task
      task = self.next_verification_task
    else
      task.distributed = true
      task.save
    end
  end
  return task
end

Instance Method Details

#add_new_result(result) ⇒ Object



36
37
38
39
40
41
# File 'app/models/distributed_rails/task.rb', line 36

def add_new_result(result)
  # Can't call result= because it will call this function'
  write_attribute(:result, result)
  self.finished = true
  save
end

#parameters=(parameters) ⇒ Object



24
25
26
# File 'app/models/distributed_rails/task.rb', line 24

def parameters=(parameters)
  write_attribute(:parameters, "[#{parameters}]")
end

#result=(result) ⇒ Object



28
29
30
31
32
33
34
# File 'app/models/distributed_rails/task.rb', line 28

def result=(result)
  if result and self.result
    verify_result(result)
  elsif result
    add_new_result(result)
  end
end

#verify_result(result) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/distributed_rails/task.rb', line 43

def verify_result(result)
  if result == self.result
    self.verified = true
    save
  else
    self.finished = false
    self.distributed = false
    self.verified = false
    # Can't call result= because it will call this function'
    write_attribute(:result, nil)
    save
  end
end