Class: QRPC::Server::Job

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/qrpc/server/job.rb

Overview

Queue RPC job.

Constant Summary collapse

DEFAULT_PRIORITY =
Deprecated.

(since 0.2.0)

Indicates default priority.

QRPC::DEFAULT_PRIORITY

Instance Method Summary collapse

Constructor Details

#initialize(api, synchronicity, job, protocol = QRPC::default_protocol) ⇒ Job

Constructor.

Parameters:

  • object (Object)

    which will serve as API

  • synchronicity (Symbol)

    API methods synchronicity

  • job (Object)

    beanstalk job

  • protocol (QRPC::Protocol::Abstract) (defaults to: QRPC::default_protocol)

    protocol handling instance



69
70
71
72
73
74
# File 'lib/qrpc/server/job.rb', line 69

def initialize(api, synchronicity, job, protocol = QRPC::default_protocol)
    @synchronicity = synchronicity
    @protocol = protocol
    @job = job
    @api = api
end

Instance Method Details

#clientString

Returns client identifier.

Returns:

  • (String)

    client identifier



166
167
168
# File 'lib/qrpc/server/job.rb', line 166

def client
    self.request.client
end

#priorityInteger

Returns job priority according to request.

Default priority is 50. You can scale up and down according to your needs in fact without limits.

Returns:

  • (Integer)

    priority level



150
151
152
153
154
155
156
157
158
159
# File 'lib/qrpc/server/job.rb', line 150

def priority
    priority = self.request.priority
    if priority.nil?
        priority = QRPC::DEFAULT_PRIORITY
    else
        priority = priority.to_i
    end
    
    return priority
end

#process!Object

Starts processing.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/qrpc/server/job.rb', line 80

def process!
    result = nil
    error = nil
    request = self.request
    
    if not request.notification?
        finalize = Proc::new do
            options = {
                :result => result,
                :error => error,
                :request => request
            }
            
            response = @protocol.response::new(options)
            self.set_deferred_status(:succeeded, response.serialize)
        end
    end

    
    if @synchronicity == :synchronous
        begin
            result = @api.send(request.method, *request.params)
        rescue ::Exception => e
            if not request.notification?
                error = self.generate_error(request, e)
            end
        end
        
        if not request.notification?
            finalize.call()
        end
    else                
        begin
            @api.send(request.method, *request.params) do |res|
                if not request.notification?
                    result = res
                    finalize.call()
                end
            end
        rescue ::Exception => e
            if not request.notification?
                error = self.generate_error(request, e)
                finalize.call()
            end
        end                    
    end
end

#requestQRPC::Protocol::Abstract::Request

Returns job in request form.

Returns:



133
134
135
136
137
138
139
# File 'lib/qrpc/server/job.rb', line 133

def request
    if @request.nil?
        @request = @protocol.request::parse(@job)
    else
        @request
    end
end