Class: Worker::Job

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Job

Returns a new instance of Job.


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

def initialize(data={})
  @data = data
end

Class Method Details

.deserialize(string) ⇒ Object


30
31
32
# File 'lib/worker/job.rb', line 30

def self.deserialize(string)
  self.new(Hash[*JSON.parse(string).map { |k, v| [k.to_sym, v] }.flatten])
end

.priority(num = nil) ⇒ Object


17
18
19
20
# File 'lib/worker/job.rb', line 17

def self.priority(num=nil)
  @priority = num if num
  @priority || 0
end

.queue(name = nil) ⇒ Object


12
13
14
15
# File 'lib/worker/job.rb', line 12

def self.queue(name=nil)
  @queue = name if name
  @queue || 'default'
end

Instance Method Details

#beforeObject


34
35
# File 'lib/worker/job.rb', line 34

def before
end

#enqueueObject


56
57
58
# File 'lib/worker/job.rb', line 56

def enqueue
  Worker.queue.enqueue(self)
end

#on_failureObject


40
41
# File 'lib/worker/job.rb', line 40

def on_failure
end

#on_successObject


37
38
# File 'lib/worker/job.rb', line 37

def on_success
end

#performObject


43
44
45
46
# File 'lib/worker/job.rb', line 43

def perform
  sleep 5
  puts "performing"
end

#priorityObject


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

def priority
  self.class.priority
end

#queueObject


22
23
24
# File 'lib/worker/job.rb', line 22

def queue
  self.class.queue
end

#run_atObject


48
49
50
# File 'lib/worker/job.rb', line 48

def run_at
  Time.now
end

#serializeObject


52
53
54
# File 'lib/worker/job.rb', line 52

def serialize
  JSON.generate(Hash[*instance_variables.map { |attr| [attr.to_s.sub('@', ''), instance_variable_get(attr)] }.flatten])
end