Module: Operationable::Persisters::Base

Extended by:
ActiveSupport::Concern, Forwardable
Included in:
OperationJob
Defined in:
lib/operationable/persisters/base.rb

Defined Under Namespace

Classes: Killed, NotANumber

Constant Summary collapse

STATUS_INIT =
'init'
STATUS_QUEUED =
'queued'
STATUS_WORKING =
'working'
STATUS_COMPLETED =
'completed'
STATUS_FAILED =
'failed'
STATUS_KILLED =
'killed'
STATUSES =
[
  STATUS_INIT,
  STATUS_QUEUED,
  STATUS_WORKING,
  STATUS_COMPLETED,
  STATUS_FAILED,
  STATUS_KILLED
].freeze

Instance Method Summary collapse

Instance Method Details

#callback_class_nameObject



65
66
67
# File 'lib/operationable/persisters/base.rb', line 65

def callback_class_name
  arguments.first[:q_options][:callback_class_name]
end

#callback_method_nameObject



69
70
71
# File 'lib/operationable/persisters/base.rb', line 69

def callback_method_name
  arguments.first[:q_options][:callback_method_name]
end

#completed(*messages) ⇒ Object

set the status to ‘completed’ passing along any addional messages



83
84
85
86
87
88
# File 'lib/operationable/persisters/base.rb', line 83

def completed(*messages)
  set_status({
    'status' => Operationable::Persisters::Base::STATUS_COMPLETED,
    'message' => "Completed at #{Time.now}"
  }, *messages)
end

#failed(*messages) ⇒ Object

set the status to ‘failed’ passing along any additional messages



78
79
80
# File 'lib/operationable/persisters/base.rb', line 78

def failed(*messages)
  set_status({'status' => Operationable::Persisters::Base::STATUS_FAILED}, *messages)
end

#kill!Object

kill the current job, setting the status to ‘killed’ and raising Killed

Raises:



91
92
93
94
95
96
97
# File 'lib/operationable/persisters/base.rb', line 91

def kill!
  set_status({
    'status' => Operationable::Persisters::Base::STATUS_KILLED,
    'message' => "Killed at #{Time.now}"
  })
  raise Killed
end

#nameObject



61
62
63
# File 'lib/operationable/persisters/base.rb', line 61

def name
  "#{self.class.name}(#{callback_class_name} #{callback_method_name})"
end

#optionsObject



31
32
33
# File 'lib/operationable/persisters/base.rb', line 31

def options
  arguments.first
end

#safe_perform(job, block) ⇒ 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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/operationable/persisters/base.rb', line 39

def safe_perform(job, block)
  working
  block.call
  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)
rescue Killed
  Resque::Plugins::Status::Hash.killed(uuid)
  on_killed if respond_to?(:on_killed)
rescue => e
  failed("The task failed because of an error: #{e}")
  if respond_to?(:on_failure)
    on_failure(e)
  else
    raise e
  end
end

#uuidObject

attr_reader :uuid, :options



27
28
29
# File 'lib/operationable/persisters/base.rb', line 27

def uuid
  self.job_id
end

#workingObject



73
74
75
# File 'lib/operationable/persisters/base.rb', line 73

def working
  set_status({'status' => Operationable::Persisters::Base::STATUS_WORKING})
end