Class: QRPC::Client::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/qrpc/client/job.rb

Overview

Queue RPC client job.

Since:

  • 0.2.0

Instance Method Summary collapse

Constructor Details

#initialize(client_id, method, arguments = [ ], priority = QRPC::DEFAULT_PRIORITY, &callback) ⇒ Job

Constructor.

Parameters:

  • client_id (Symbol)

    client (session) ID

  • method (Symbol)

    job method name

  • arguments (Array) (defaults to: [ ])

    array of arguments for job

  • priority (Integer) (defaults to: QRPC::DEFAULT_PRIORITY)

    job priority

  • callback (Proc)

    result callback

Since:

  • 0.2.0



83
84
85
86
87
88
89
# File 'lib/qrpc/client/job.rb', line 83

def initialize(client_id, method, arguments = [ ], priority = QRPC::DEFAULT_PRIORITY, &callback)
    @client_id = client_id
    @method = method
    @arguments = arguments
    @callback = callback
    @priority = priority
end

Instance Method Details

#assign_result(result) ⇒ Object

Assigns job result and subsequently calls callback.

Parameters:

  • result (JsonRpcObjects::Generic::Response)

    of the call

Since:

  • 0.2.0



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/qrpc/client/job.rb', line 129

def assign_result(result)
    if not result.error?
        @result = result.result
    else
=begin
        STDERR.write(">>> Exception while call ID: " << self.id.to_s << "\n")
=end                    
        exception = QRPC::Client::Exception::get(result.error)
=begin
        STDERR.write exception.class.name.dup << ": " << exception.message << "\n"
        
        exception.backtrace.each do |i|
            STDERR.write "	from " << i << "\n"
        end
=end                    
        raise exception
    end
    
    if not @callback.nil?
        @callback.call(@result)
    end
end

#idSymbol

Returns job ID.

Returns:

  • (Symbol)

    job ID

Since:

  • 0.2.0



96
97
98
99
100
101
102
# File 'lib/qrpc/client/job.rb', line 96

def id
    if @id.nil?
        @id = UUID.generate(:compact).to_sym
    end
    
    return @id
end

#notification?Boolean

Indicates message is notification. So callback isn’t set and it doesn’t expect any result.

Returns:

  • (Boolean)

    true if it’s, false in otherwise

Since:

  • 0.2.0



120
121
122
# File 'lib/qrpc/client/job.rb', line 120

def notification?
    @callback.nil?
end

#to_jsonString

Converts job to JSON.

Returns:

  • (String)

    job in JSON form (JSON-RPC)

Since:

  • 0.2.0



109
110
111
# File 'lib/qrpc/client/job.rb', line 109

def to_json
    QRPC::Protocol::Request::create(@client_id, @id, @method, @arguments, @priority).to_json
end