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, generator = QRPC::default_generator, protocol = QRPC::default_protocol, &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

  • ID (QRPC::Generator)

    generator

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

    protocol instance

  • callback (Proc)

    result callback

Since:

  • 0.2.0



99
100
101
102
103
104
105
106
107
# File 'lib/qrpc/client/job.rb', line 99

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

Instance Method Details

#assign_result(result) ⇒ Object

Assigns job result and subsequently calls callback.

Parameters:

Since:

  • 0.2.0



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/qrpc/client/job.rb', line 170

def assign_result(result)
    if not result.error?
        @result = result.result
    else
        exception = result.error
        raise exception
    end
    
    if not @callback.nil?
        @callback.call(@result)
    end
end

#idSymbol

Returns job ID.

Returns:

  • (Symbol)

    job ID

Since:

  • 0.2.0



114
115
116
117
118
119
120
# File 'lib/qrpc/client/job.rb', line 114

def id
    if @id.nil?
        @id = @generator.generate(self)
    else
        @id
    end
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



161
162
163
# File 'lib/qrpc/client/job.rb', line 161

def notification?
    @callback.nil?
end

#serializeObject

Serializes job using serializer.

Returns:

  • (Object)

    serialized object

Since:

  • 0.4.0



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/qrpc/client/job.rb', line 141

def serialize
    options = {
        :client_id => @client_id,
        :id => @id,
        :method => @method,
        :arguments => @arguments,
        :priority => @priority,
        :notification => self.notification?
    }
    
    @protocol.request::new(options).serialize
end

#to_jsonString

Deprecated.

(since 0.4.0) Use #serialize.

Converts job to JSON.

Returns:

  • (String)

    job in JSON form (JSON-RPC)

See Also:

Since:

  • 0.2.0



130
131
132
# File 'lib/qrpc/client/job.rb', line 130

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