Module: Resque::Plugins::Status

Defined in:
lib/resque_manager/overrides/resque_status/hash.rb,
lib/resque_manager/overrides/resque_status/status.rb

Defined Under Namespace

Modules: ClassOverridesAndExtensions Classes: Hash

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

OVERRIDE so we can add OverridesAndExtensionsClassMethods



7
8
9
10
11
12
13
14
15
16
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 7

def self.included(base)
  base.class_eval do
    attr_reader :worker

    # can't call super, so add ClassMethods here that resque-status was doing
    extend(ClassMethods) #add the methods in the resque-status gem
    extend(ClassOverridesAndExtensions)
    include(SemanticLogger::Loggable)
  end
end

Instance Method Details

#counter(counter) ⇒ Object



158
159
160
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 158

def counter(counter)
  self.class.counter(counter, uuid)
end

#incr_counter(counter) ⇒ Object



154
155
156
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 154

def incr_counter(counter)
  self.class.incr_counter(counter, uuid)
end

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

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



107
108
109
110
111
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 107

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

#overview_message=(message) ⇒ Object

sets a message for the job on the overview page it can be set repeatedly durring the job’s processing to indicate the status of the job.



149
150
151
152
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 149

def overview_message=(message)
  # there is no worker when run inline
  self.worker.overview_message = message if self.worker
end

#pause!Object

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



98
99
100
101
102
103
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 98

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. OVERRIDE to kill it. The parent job may have been killed, so all child jobs should die as well.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 118

def safe_perform!
  k = should_kill?
  kill! if k
  unless k || (status && status.killed?)
    set_status({'status' => 'working'})
    perform
    if status && status.failed?
      on_failure(status.message) if respond_to?(:on_failure)
      return
    elsif status && !status.completed?
      completed
    end
    on_success if respond_to?(:on_success)
  end
rescue Killed
  logger.info "Job #{self} Killed at #{Time.now}"
  Resque::Plugins::Status::Hash.killed(uuid)
  on_killed if respond_to?(:on_killed)
rescue Exception => e
  logger.error 'The task failed because of an 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()



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/resque_manager/overrides/resque_status/status.rb', line 81

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