Class: Rjob::Job

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

Constant Summary collapse

DeserializationError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Job

Returns a new instance of Job.



11
12
13
# File 'lib/rjob/job.rb', line 11

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/rjob/job.rb', line 9

def context
  @context
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/rjob/job.rb', line 8

def payload
  @payload
end

#retry_numObject

Returns the value of attribute retry_num.



7
8
9
# File 'lib/rjob/job.rb', line 7

def retry_num
  @retry_num
end

Class Method Details

.deserialize(context, job_str) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rjob/job.rb', line 36

def self.deserialize(context, job_str)
  first = job_str.index('!')
  second = job_str.index('!', first + 1)

  if first == nil || second == nil
    raise DeserializationError.new("Malformed job string: '#{job_str}'")
  end

  begin
    new(context).tap do |job|
      job.id = job_str[0...first]
      job.retry_num = job_str[(first + 1)...second].to_i
      job.payload = job_str[(second + 1)..-1]
    end
  rescue MessagePack::MalformedFormatError => e
    raise DeserializationError.new("Malformed job msgpack payload: #{e.message}")
  end
end

Instance Method Details

#serializeObject



32
33
34
# File 'lib/rjob/job.rb', line 32

def serialize
  "#{@id}!#{@retry_num}!#{@payload}".b
end

#worker_argsObject



23
24
25
# File 'lib/rjob/job.rb', line 23

def worker_args
  @deserialized_payload[1]
end

#worker_classObject



19
20
21
# File 'lib/rjob/job.rb', line 19

def worker_class
  @context.fetch_worker_class(class_name: worker_class_name)
end

#worker_class_nameObject



15
16
17
# File 'lib/rjob/job.rb', line 15

def worker_class_name
  @deserialized_payload[0]
end