Class: Lambdakiq::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/lambdakiq/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Job

Returns a new instance of Job.



19
20
21
22
# File 'lib/lambdakiq/job.rb', line 19

def initialize(record)
  @record = Record.new(record)
  @error = false
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



4
5
6
# File 'lib/lambdakiq/job.rb', line 4

def error
  @error
end

#recordObject (readonly)

Returns the value of attribute record.



4
5
6
# File 'lib/lambdakiq/job.rb', line 4

def record
  @record
end

Class Method Details

.handler(event) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
# File 'lib/lambdakiq/job.rb', line 8

def handler(event)
  records = Event.records(event)
  jobs = records.map { |record| new(record) }
  jobs.each(&:perform)
  jwerror = jobs.detect{ |j| j.error }
  return unless jwerror
  raise JobError.new(jwerror.error)
end

Instance Method Details

#active_jobObject



31
32
33
# File 'lib/lambdakiq/job.rb', line 31

def active_job
  @active_job ||= ActiveJob::Base.deserialize(job_data)
end

#executeObject



51
52
53
54
55
56
57
# File 'lib/lambdakiq/job.rb', line 51

def execute
  ActiveJob::Base.execute(job_data)
  delete_message
rescue Exception => e
  increment_executions
  perform_error(e)
end

#executionsObject



39
40
41
# File 'lib/lambdakiq/job.rb', line 39

def executions
  active_job.executions
end

#job_dataObject



24
25
26
27
28
29
# File 'lib/lambdakiq/job.rb', line 24

def job_data
  @job_data ||= JSON.parse(record.body).tap do |data|
    data['provider_job_id'] = record.message_id
    data['executions'] = record.receive_count - 1
  end
end

#performObject



43
44
45
46
47
48
49
# File 'lib/lambdakiq/job.rb', line 43

def perform
  if fifo_delay?
    fifo_delay
    raise FifoDelayError, active_job.job_id
  end
  execute
end

#queueObject



35
36
37
# File 'lib/lambdakiq/job.rb', line 35

def queue
  Lambdakiq.client.queues[active_job.queue_name]
end