Class: Jobs::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_record, logger, lock) ⇒ Base



5
6
7
8
9
# File 'lib/jobs/base.rb', line 5

def initialize(job_record, logger, lock)
  @record = job_record
  @logger = logger
  @lock = lock
end

Instance Attribute Details

#lockObject (readonly)

Returns the value of attribute lock.



3
4
5
# File 'lib/jobs/base.rb', line 3

def lock
  @lock
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/jobs/base.rb', line 3

def logger
  @logger
end

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/jobs/base.rb', line 3

def record
  @record
end

Instance Method Details

#executeObject



11
12
# File 'lib/jobs/base.rb', line 11

def execute
end

#real_executeObject

really run the execute method



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jobs/base.rb', line 15

def real_execute
  timer = Time.now

  res = execute

  @record.duration = Time.now - timer

  @record.status = res ? "complete" : "error"

rescue Object => e
  # add error details to the record
  @record.details ||= ""
  @record.details << "#{e.message}\n#{e.backtrace.join("\n")}"

  # log the error
  @logger.error "#{e.message}\n#{e.backtrace.join("\n")}"
  @record.status = "error"
ensure
  # it's no longer processing, so we must have landed here by error
  @record.status = 'error' if @record.status == 'processing'
  @record.locked = false
  @record.save
end