playbooks/roles/jenkins/files/opt/lifecycle-poller/worker.rb
#------------------------------------------------------------------------------
# 
# FUNCTION: pollSQS
#
# DESCRIPTION: This is the main loop of the daemon. It listens to Autoscaling 
#   events (terminating in this case), and remove the corresponding agent
#   from the Jenkins list of available agents.
#
#------------------------------------------------------------------------------
pollSQS
DESCRIPTION: This is the main loop of the daemon. It listens to Autoscaling
events (terminating in this case), and remove the corresponding agent
from the Jenkins list of available agents.
pollSQS DESCRIPTION : This is the main loop of the daemon . It listens to Autoscaling events ( terminating in this case ) and remove the corresponding agent from the Jenkins list of available agents .
def pollSQS()
  poller = Aws::SQS::QueuePoller.new($conf['queueUrl'])
  poller.poll do |msg|
    begin
      body = JSON.parse(msg.body)
      $log.debug "message #{body}"
      if body['Event'] == 'autoscaling:TEST_NOTIFICATION'
        $log.info 'received test notification'
      else
        if body['LifecycleTransition'] == 'autoscaling:EC2_INSTANCE_TERMINATING'
          $log.info "lifecycle transition for agent #{body['EC2InstanceId']}"
          takeAgentTemporarilyOffline body['EC2InstanceId']
          awaitAgentIdle body['EC2InstanceId']
          deleteAgent body['EC2InstanceId']
          completeLifecycleAction body['LifecycleActionToken'], body['LifecycleHookName'], body['AutoScalingGroupName']
        else
          $log.error "received unsupported lifecycle transition: #{body['LifecycleTransition']}"
        end
      end
    rescue Exception => e
      $log.error "message failed: #{e.inspect} #{msg.inspect}"
      raise e
    end
  end
end
def pollSQS ( ) poller = Aws :: SQS :: QueuePoller . new ( $conf [ 'queueUrl' ] ) poller . poll do | msg | begin body = JSON . parse ( msg . body ) $log . debug "message #{body}" if body [ 'Event' ] == 'autoscaling:TEST_NOTIFICATION' $log . info 'received test notification' else if body [ 'LifecycleTransition' ] == 'autoscaling:EC2_INSTANCE_TERMINATING' $log . info "lifecycle transition for agent #{body['EC2InstanceId']}" takeAgentTemporarilyOffline body [ 'EC2InstanceId' ] awaitAgentIdle body [ 'EC2InstanceId' ] deleteAgent body [ 'EC2InstanceId' ] completeLifecycleAction body [ 'LifecycleActionToken' ] , body [ 'LifecycleHookName' ] , body [ 'AutoScalingGroupName' ] else $log . error "received unsupported lifecycle transition: #{body['LifecycleTransition']}" end end rescue Exception => e $log . error "message failed: #{e.inspect} #{msg.inspect}" raise e end end end
FUNCTION: pollSQS
DESCRIPTION: This is the main loop of the daemon.
FUNCTION : pollSQS DESCRIPTION : This is the main loop of the daemon .