Class: Worker::Base

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

Direct Known Subclasses

Capture, Export

Class Method Summary collapse

Class Method Details

.after_run(result, options) ⇒ Object

Override to do any post run checking. Receives the result (true/false) of the run and the options which may have been altered by the run. Job will fail if after_run returns false



36
37
38
# File 'lib/spree_batch_capture/worker/base.rb', line 36

def self.after_run(result, options)
  return true
end

.before_run(options) ⇒ Object

Override to do any modifications or validation before running the job. Job will fail if before_run returns false.



28
29
30
# File 'lib/spree_batch_capture/worker/base.rb', line 28

def self.before_run(options)
  return true
end

.default_queue_nameObject

Provides a default queue name. Returning nil will default to the class name.



75
76
77
# File 'lib/spree_batch_capture/worker/base.rb', line 75

def self.default_queue_name
  return nil
end

.enqueue(options, custom_queue_name = nil) ⇒ Object

Use enqueue to add a job to the queue to be captured. Returns the result of the enqueue and the queue name as an array.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/spree_batch_capture/worker/base.rb', line 54

def self.enqueue(options, custom_queue_name=nil)
  queue_name = custom_queue_name || default_queue_name || worker_klass_name
  
  # Resque.enqueue_to is currently supported only in the master branch
  # of the gem and not included in any gem. When it is, the following line
  # should work. 
  #added = ::Resque.enqueue_to(queue_name, worker_class, options)
  
  #Until then, we have to do this work-around:
  @queue = queue_name

  added = ::Resque.enqueue(worker_class, options)
  [ added, queue_name ]
end

.handle_error(exception) ⇒ Object

Override to handle any exceptions that come up during the execution of the job. Returning false will fail the job, while returning true will indicate that the job was successful.



43
44
45
# File 'lib/spree_batch_capture/worker/base.rb', line 43

def self.handle_error(exception)
  return false
end

.perform(options = {}) ⇒ Object

Actual method called by Resque. This method cleans up any options and allows a consistent interface for the worker’s run method.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spree_batch_capture/worker/base.rb', line 11

def self.perform(options={})
  begin
    options.symbolize_keys!
    if before_run(options)
      result = run(options)
      return after_run(result, options)
    else
      return false
    end
  rescue => e
    handle_error(e)
  end
end

.run(options) ⇒ Object

Implemented by subclasses and performs the actual work of the worker.



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

def self.run(options)
  return true
end

.worker_klass_nameObject

Override this in the subclass to provide the name of the queue



70
71
72
# File 'lib/spree_batch_capture/worker/base.rb', line 70

def self.worker_klass_name
  return "Base"
end