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



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)
  failed_jobs = jobs.select { |j| j.error }
  item_failures = failed_jobs.map { |j| { itemIdentifier: j.provider_job_id } }
  { batchItemFailures: item_failures }
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



55
56
57
58
59
60
61
# File 'lib/lambdakiq/job.rb', line 55

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



47
48
49
50
51
52
53
# File 'lib/lambdakiq/job.rb', line 47

def perform
  if fifo_delay?
    fifo_delay
    return
  end
  execute
end

#provider_job_idObject



43
44
45
# File 'lib/lambdakiq/job.rb', line 43

def provider_job_id
  active_job.provider_job_id
end

#queueObject



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

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