Class: Resque::JobWithStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/resque_ui/overrides/resque_status/job_with_status.rb

Direct Known Subclasses

ChainedJobWithStatus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, worker = nil, options = {}) ⇒ JobWithStatus

Create a new instance with uuid and options OVERRIDE to add the worker attr



45
46
47
48
49
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 45

def initialize(uuid, worker = nil, options = {})
  @uuid = uuid
  @options = options
  @worker = worker
end

Instance Attribute Details

#workerObject (readonly)

Returns the value of attribute worker.



4
5
6
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 4

def worker
  @worker
end

Class Method Details

.enqueue(klass, options = {}) ⇒ Object

Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>. Returns the UUID of the job override to pass actual parameters instead of a single hash, to make backward compatible with existing resque jobs.



9
10
11
12
13
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 9

def self.enqueue(klass, options = {})
  uuid = Resque::Status.create :name => "#{self.name}: #{options.inspect}"
  Resque.enqueue(klass, uuid, options)
  uuid
end

.perform(uuid = nil, options = {}) ⇒ Object

This is the method called by Resque::Worker when processing jobs. It creates a new instance of the job class and populates it with the uuid and options.

You should not override this method, rather the perform instance method. OVERRIDE to pass the block in order to set the worker status, returns the worker object



57
58
59
60
61
62
63
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 57

def self.perform(uuid=nil, options = {})
  uuid ||= Resque::Status.generate_uuid
  worker = yield if block_given?
  instance = new(uuid, worker, options)
  instance.safe_perform! { |status| yield status if block_given? }
  instance
end

Instance Method Details

#counter(counter) ⇒ Object



99
100
101
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 99

def counter(counter)
  Resque::Status.counter(counter, uuid)
end

#incr_counter(counter) ⇒ Object



95
96
97
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 95

def incr_counter(counter)
  Resque::Status.incr_counter(counter, uuid)
end

#nameObject



91
92
93
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 91

def name
  "#{self.class.name}: #{options.inspect}"
end

#pause!Object

Pause the current job, setting the status to ‘paused’



36
37
38
39
40
41
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 36

def pause!
  set_status({
                 'status' => 'paused',
                 'message' => "#{worker} paused at #{Time.now}"
             })
end

#safe_perform!Object

Run by the Resque::Worker when processing this job. It wraps the perform method ensuring that the final status of the job is set regardless of error. If an error occurs within the job’s work, it will set the status as failed and re-raise the error.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 69

def safe_perform!
  unless should_kill? || (status && status.killed?)
    set_status({'status' => 'working'})
    perform { |status| yield status if block_given?  }
    kill! if should_kill?
    completed unless status && status.completed?
    on_success if respond_to?(:on_success)
  end
rescue Killed
  logger.info "Job #{self} Killed at #{Time.now}"
  Resque::Status.killed(uuid)
  on_killed if respond_to?(:on_killed)
rescue => e
  logger.error e
  failed("The task failed because of an error: #{e}")
  if respond_to?(:on_failure)
    on_failure(e)
  else
    raise e
  end
end

#tick(*messages) ⇒ Object

sets the status of the job for the current iteration. You should use the at method if you have actual numbers to track the iteration count. This will kill the job if it has been added to the kill list with Resque::Status.kill()



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/resque_ui/overrides/resque_status/job_with_status.rb', line 19

def tick(*messages)
  kill! if should_kill? || status.killed?
  set_status({'status' => 'working'}, *messages)
  # check to see if the worker doing the job has been paused, pause the job if so
  if self.worker && self.worker.paused?
    loop do
      # Set the status to paused.
      # May need to do this repeatedly because there could be workers in a chained job still doing work.
      pause! unless status.paused?
      break unless self.worker.paused?
      sleep 60
    end
    set_status({'status' => 'working'}, *messages) unless status && (status.completed? || status.paused? || status.killed?)
  end
end