class RubyJob::Job

Attributes

args[R]
jobstore[R]
start_at[R]
uuid[R]
worker_class_name[R]

Public Class Methods

json_create(hash) click to toggle source
# File lib/ruby_job/job.rb, line 55
def self.json_create(hash)
  worker_class_name = hash['data']['worker_class_name']
  args = JSON.parse(hash['data']['args_json'])
  start_at = Time.iso8601(hash['data']['start_at'])
  uuid = hash['data']['uuid']
  new(worker_class_name: worker_class_name, args: args, start_at: start_at, uuid: uuid)
end
new( worker_class_name:, args:, start_at: Time.now, uuid: nil, jobstore: Job.send(:default_jobstore, worker_class_name) ) click to toggle source
# File lib/ruby_job/job.rb, line 12
def initialize(
  worker_class_name:,
  args:,
  start_at: Time.now,
  uuid: nil,
  jobstore: Job.send(:default_jobstore, worker_class_name)
)
  @worker_class_name = worker_class_name
  @args = args
  @start_at = Time.at(start_at.to_f.round(3))
  @uuid_id = uuid
  @jobstore = jobstore
end

Private Class Methods

default_jobstore(worker_class_name) click to toggle source
# File lib/ruby_job/job.rb, line 88
def default_jobstore(worker_class_name)
  worker_class = worker_class_name.split('::').reduce(Module, :const_get)
  class_with_jobstore_method = worker_class.respond_to?(:jobstore) ? worker_class : Worker
  class_with_jobstore_method.jobstore
end

Public Instance Methods

==(other) click to toggle source
# File lib/ruby_job/job.rb, line 30
def ==(other)
  return false unless other.is_a? Job

  @start_at == other.start_at &&
    @uuid == other.uuid &&
    @args == other.args &&
    @worker_class_name == other.worker_class_name
end
dequeue() click to toggle source
# File lib/ruby_job/job.rb, line 71
def dequeue
  raise 'job was not queued' unless @uuid

  @jobstore.dequeue(self)
  @uuid = nil
  self
end
enqueue() click to toggle source
# File lib/ruby_job/job.rb, line 63
def enqueue
  raise 'job has already been enqueued' if @uuid

  @uuid = @jobstore.next_uuid
  @jobstore.enqueue(self)
  self
end
perform() click to toggle source
# File lib/ruby_job/job.rb, line 26
def perform
  worker_class.perform(*@args)
end
to_h() click to toggle source
# File lib/ruby_job/job.rb, line 39
def to_h
  {
    'json_class' => self.class.name,
    'data' => {
      'worker_class_name' => @worker_class_name,
      'args_json' => JSON.dump(@args),
      'start_at' => @start_at.iso8601(9),
      'uuid' => @uuid
    }
  }
end
to_json(*args) click to toggle source
# File lib/ruby_job/job.rb, line 51
def to_json(*args)
  to_h.to_json(*args)
end

Private Instance Methods

worker_class() click to toggle source
# File lib/ruby_job/job.rb, line 81
def worker_class
  @worker_class ||= @worker_class_name.split('::').reduce(Module, :const_get)
end