Class: Nanite::JobWarden

Inherits:
Object show all
Defined in:
lib/nanite/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer) ⇒ JobWarden

Returns a new instance of JobWarden.



5
6
7
8
# File 'lib/nanite/job.rb', line 5

def initialize(serializer)
  @serializer = serializer
  @jobs = {}
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



3
4
5
# File 'lib/nanite/job.rb', line 3

def jobs
  @jobs
end

#serializerObject (readonly)

Returns the value of attribute serializer.



3
4
5
# File 'lib/nanite/job.rb', line 3

def serializer
  @serializer
end

Instance Method Details

#new_job(request, targets, inthandler = nil, blk = nil) ⇒ Object



10
11
12
13
14
# File 'lib/nanite/job.rb', line 10

def new_job(request, targets, inthandler = nil, blk = nil)
  job = Job.new(request, targets, inthandler, blk)
  jobs[job.token] = job
  job
end

#process(msg) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nanite/job.rb', line 16

def process(msg)
  if job = jobs[msg.token]
    job.process(msg)

    if job.intermediate_handler && (job.pending_keys.size > 0)

      unless job.pending_keys.size == 1
        raise "IntermediateMessages are currently dispatched as they arrive, shouldn't have more than one key in pending_keys: #{job.pending_keys.inspect}"
      end

      key = job.pending_keys.first
      handler = job.intermediate_handler_for_key(key)
      if handler
        case handler.arity
        when 2
          handler.call(job.intermediate_state[msg.from][key].last, job)
        when 3
          handler.call(key, msg.from, job.intermediate_state[msg.from][key].last)
        when 4
          handler.call(key, msg.from, job.intermediate_state[msg.from][key].last, job)
        end
      end

      job.reset_pending_intermediate_state_keys
    end

    if job.completed?
      jobs.delete(job.token)
      if job.completed
        case job.completed.arity
        when 1
          job.completed.call(job.results)
        when 2
          job.completed.call(job.results, job)
        end
      end
    end
  end
end