Class: LambdaPunch::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_punch/worker.rb

Overview

This ‘LambdaPunch::Worker` has a few responsibilities:

1. Maintain a class level DRb reference to your function's `LambdaPunch::Queue` object.
2. Process extension `INVOKE` events by waiting for your function to complete.
3. Triggering your application to perform work after each request.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_payload) ⇒ Worker

Returns a new instance of Worker.



54
55
56
57
58
59
60
# File 'lib/lambda_punch/worker.rb', line 54

def initialize(event_payload)
  @invoked = false
  @event_payload = event_payload
  @notifier = Notifier.new
  @notifier.watch { |request_id| notified(request_id) }
  @request_id_notifier = nil
end

Class Method Details

.call(event_payload) ⇒ Object

Creates a new instance of this object with the event payload from the ‘LambdaPunch::Api#invoke` method and immediately performs the `call` method which waits for the function’s handler to complete.



25
26
27
# File 'lib/lambda_punch/worker.rb', line 25

def call(event_payload)
  new(event_payload).call
end

.call_queueObject

A safe and resilient way to call the remote queue.



31
32
33
34
35
36
37
# File 'lib/lambda_punch/worker.rb', line 31

def call_queue
  queue.call
rescue DRb::DRbConnError
  LambdaPunch.logger.error "Worker#call_queue => DRb::DRbConnError"
  new_drb_queue
  queue.call
end

.start!Object

Method to lazily require rb-inotify and start the DRb service.



14
15
16
17
18
19
20
# File 'lib/lambda_punch/worker.rb', line 14

def start!
  LambdaPunch.logger.info "Worker.start!..."
  require 'timeout'
  require 'rb-inotify'
  DRb.start_service
  new_drb_queue
end

Instance Method Details

#callObject

Here we wait for the application’s handler to signal it is done via the ‘LambdaPunch::Notifier` or if the function has timed out. In either event there may be work to perform in the `LambdaPunch::Queue`. This method also ensures any clean up is done. For example, closing file notifications.



66
67
68
69
70
71
72
73
# File 'lib/lambda_punch/worker.rb', line 66

def call
  timeout { @notifier.process unless invoked? }
rescue Timeout::Error
  logger.error "Worker#call => Function timeout reached."
ensure
  @notifier.close
  self.class.call_queue
end