Class: ResqueFuture::FutureJob

Inherits:
Resque::Job
  • Object
show all
Defined in:
lib/resque_future/future_job.rb

Overview

A FutureJob is a job schedule to run in the future that you still want to be able to track it’s result and status.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, uuid, payload) ⇒ FutureJob

Override constructor to receive an UUID



11
12
13
14
15
16
# File 'lib/resque_future/future_job.rb', line 11

def initialize(queue, uuid, payload)
  @uuid = uuid || self.generate_uuid
  payload["args"].unshift(@uuid)
  super(queue, payload)
  set_result('enqueued_at' => Time.now.utc)
end

Instance Attribute Details

#uuidObject (readonly)

Returns the value of attribute uuid.



8
9
10
# File 'lib/resque_future/future_job.rb', line 8

def uuid
  @uuid
end

Class Method Details

.create(queue, uuid, klass, *args) ⇒ Object

Override Job.create method so that it returns a instance of FutureJob allowing for late querying for the result.



20
21
22
23
24
25
# File 'lib/resque_future/future_job.rb', line 20

def self.create(queue, uuid, klass, *args)
  job = self.new(queue, uuid, 'class' => klass, 'args' => args)
  super(queue, klass, *args)
  job.persist
  job
end

.get(queue, uuid) ⇒ Object

Query for a FutureJob using it’s UUID



28
29
30
31
32
# File 'lib/resque_future/future_job.rb', line 28

def self.get(queue, uuid)
  if payload = self.payload(uuid)
    self.new(queue, uuid, payload)
  end
end

Instance Method Details

#enqueued_atObject

Returns the time that the job was enqueued



71
72
73
# File 'lib/resque_future/future_job.rb', line 71

def enqueued_at
  Time.parse(self.result_payload["enqueued_at"]) if self.result_payload && self.result_payload["enqueued_at"]
end

#finished_atObject

Returns the time that the result was written to redis and the job finished processing.



66
67
68
# File 'lib/resque_future/future_job.rb', line 66

def finished_at
  Time.parse(self.result_payload["finished_at"]) if self.result_payload && self.result_payload["finished_at"]
end

#performObject

Perform the job



50
51
52
53
54
# File 'lib/resque_future/future_job.rb', line 50

def perform
  set_result("started_at" => Time.now.utc)
  super
  set_result("finished_at" => Time.now.utc)
end

#persistObject

Persist the job information into redis



35
36
37
# File 'lib/resque_future/future_job.rb', line 35

def persist
  redis.set(self.payload_key, encode(self.payload))
end

#processing?Boolean

Returns true/false whether it is waiting in the queue or being processed

Returns:

  • (Boolean)


45
46
47
# File 'lib/resque_future/future_job.rb', line 45

def processing?
  self.result_payload(true).has_key?('started_at') && !self.result_payload(true).has_key?('finished_at')
end

#ready?Boolean

Returns true/false if the job has been processed yet

Returns:

  • (Boolean)


40
41
42
# File 'lib/resque_future/future_job.rb', line 40

def ready?
  self.result_payload(true).has_key?('finished_at')
end

#resultObject

Returns the result for the job



57
58
59
# File 'lib/resque_future/future_job.rb', line 57

def result
  self.result_payload["result"] if self.result_payload
end

#result=(result_value) ⇒ Object



61
62
63
# File 'lib/resque_future/future_job.rb', line 61

def result=(result_value)
  set_result("result" => result_value)
end

#started_atObject

Returns the time that the job has started processing



76
77
78
# File 'lib/resque_future/future_job.rb', line 76

def started_at
  Time.parse(self.result_payload["started_at"]) if self.result_payload && self.result_payload["started_at"]
end