Module: Aws::ActiveJob::SQS::LambdaHandler

Defined in:
lib/aws/active_job/sqs/lambda_handler.rb

Overview

Lambda event handler to run jobs from an SQS queue trigger

Class Method Summary collapse

Class Method Details

.job_handler(event:, context:) ⇒ Object

A lambda event handler to run jobs from an SQS queue trigger. Configure the entrypoint to: config/environment.Aws::ActiveJob::SQS::LambdaHandler.job_handler This will load your Rails environment, and then use this method as the handler.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/aws/active_job/sqs/lambda_handler.rb', line 14

def job_handler(event:, context:)
  return 'no records to process' unless event['Records']

  puts "job_handler running for #{event} with context: #{context}"

  event['Records'].each do |record|
    sqs_msg = to_sqs_msg(record)
    job = Aws::ActiveJob::SQS::JobRunner.new(sqs_msg)
    puts "Running job: #{job.id}[#{job.class_name}]"
    job.run
    sqs_msg.delete
  end
  "Processed #{event['Records'].length} jobs."
end