Class: QRPC::Server::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/qrpc/server/dispatcher.rb

Overview

Queue RPC job.

Instance Method Summary collapse

Constructor Details

#initialize(max_jobs = 20) ⇒ Dispatcher

Constructor.



40
41
42
43
44
45
46
47
48
# File 'lib/qrpc/server/dispatcher.rb', line 40

def initialize(max_jobs = 20)
    @count = 0
    @queue = Depq::new
    @max_jobs = max_jobs
    
    if @max_jobs.nil?
        @max_jobs = 20
    end
end

Instance Method Details

#process_next!Object

Sets up next job for processing.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/qrpc/server/dispatcher.rb', line 72

def process_next!
    job = @queue.pop
    job.callback do
        if (@count < @max_jobs) and not @queue.empty?
            self.process_next!
        else
            @count -= 1
        end
    end

    job.process!
end

#put(job) ⇒ Object

Puts job to dispatcher.

Parameters:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/qrpc/server/dispatcher.rb', line 55

def put(job)
    begin
        @queue.put(job, job.priority)
    rescue ::Exception => e
        return
    end
    
    if @count < @max_jobs
        self.process_next!
        @count += 1
    end
end