Class: RubyJob::Job

Inherits:
FibonacciHeap::Node
  • Object
show all
Defined in:
lib/ruby_job/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_class_name:, args:, start_at: Time.now, uuid: nil, jobstore: Job.send(:default_jobstore, worker_class_name)) ⇒ Job

Returns a new instance of Job.



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/ruby_job/job.rb', line 10

def args
  @args
end

#jobstoreObject (readonly)

Returns the value of attribute jobstore.



10
11
12
# File 'lib/ruby_job/job.rb', line 10

def jobstore
  @jobstore
end

#start_atObject (readonly)

Returns the value of attribute start_at.



10
11
12
# File 'lib/ruby_job/job.rb', line 10

def start_at
  @start_at
end

#uuidObject (readonly)

Returns the value of attribute uuid.



10
11
12
# File 'lib/ruby_job/job.rb', line 10

def uuid
  @uuid
end

#worker_class_nameObject (readonly)

Returns the value of attribute worker_class_name.



10
11
12
# File 'lib/ruby_job/job.rb', line 10

def worker_class_name
  @worker_class_name
end

Class Method Details

.json_create(hash) ⇒ Object



55
56
57
58
59
60
61
# 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

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
34
35
36
37
# 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

#dequeueObject



71
72
73
74
75
76
77
# File 'lib/ruby_job/job.rb', line 71

def dequeue
  raise 'job was not queued' unless @uuid

  @jobstore.dequeue(self)
  @uuid = nil
  self
end

#enqueueObject



63
64
65
66
67
68
69
# 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

#performObject



26
27
28
# File 'lib/ruby_job/job.rb', line 26

def perform
  worker_class.perform(*@args)
end

#to_hObject



39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



51
52
53
# File 'lib/ruby_job/job.rb', line 51

def to_json(*args)
  to_h.to_json(*args)
end