parameters: (self)
docstring: This is the main function of the ``DeployAgent``.
docstring_summary: This is the main function of the ``DeployAgent``.
docstring_tokens: This is the main function of the DeployAgent .
function: def serve_build(self):
          """This is the main function of the ``DeployAgent``.
          """
          log.info('The deploy agent is starting.')
          if not self._executor:
              self._executor = Executor(callback=PingServer(self), config=self._config)
  
          # start to ping server to get the latest deploy goal
          self._response = self._client.send_reports(self._envs)
  
          if self._response:
              report = self._update_internal_deploy_goal(self._response)
              # failed to update
              if report.status_code != AgentStatus.SUCCEEDED:
                  self._update_ping_reports(deploy_report=report)
                  self._client.send_reports(self._envs)
                  return
  
          while self._response and self._response.opCode and self._response.opCode != OpCode.NOOP:
              try:
                  # ensure stat_time_elapsed_internal is always running
                  self.stat_time_elapsed_internal.resume()
                  # update the current deploy goal
                  if self._response.deployGoal:
                      deploy_report = self.process_deploy(self._response)
                      # resume stat_time_elapsed_internal immediately, previous process_deploy may have paused
                      self.stat_time_elapsed_internal.resume()
                  else:
                      log.info('No new deploy goal to get updated')
                      deploy_report = DeployReport(AgentStatus.SUCCEEDED)
  
                  if deploy_report.status_code == AgentStatus.ABORTED_BY_SERVER:
                      log.info('switch to the new deploy goal: {}'.format(self._response.deployGoal))
                      continue
  
              except Exception:
                  # anything catch-up here should be treated as agent failure
                  # resume stat_time_elapsed_internal, previous process_deploy may have paused
                  self.stat_time_elapsed_internal.resume()
                  deploy_report = DeployReport(status_code=AgentStatus.AGENT_FAILED,
                                               error_code=1,
                                               output_msg=traceback.format_exc(),
                                               retry_times=1)
  
              # increment stats - deploy status
              if self._response.deployGoal and deploy_report:
                  tags = {'first_run': self.first_run()}
                  if self._response.deployGoal.deployStage:
                      tags['deploy_stage'] = self._response.deployGoal.deployStage
                  if self._response.deployGoal.envName:
                      tags['env_name'] = self._response.deployGoal.envName
                  if self._response.deployGoal.stageName:
                      tags['stage_name'] = self._response.deployGoal.stageName
                  if deploy_report.status_code:
                      tags['status_code'] = deploy_report.status_code
                  create_sc_increment('deployd.stats.deploy.status.sum',
                                      1,
                                      tags=tags)
  
              if PingStatus.PING_FAILED == self.update_deploy_status(deploy_report):
                  return
              if deploy_report.status_code in [AgentStatus.AGENT_FAILED,
                                               AgentStatus.TOO_MANY_RETRY,
                                               AgentStatus.SCRIPT_TIMEOUT]:
                  log.error('Unexpeted exceptions: {}, error message {}'.format(
                      deploy_report.status_code, deploy_report.output_msg))
                  return
  
          self.clean_stale_builds()
          if self._response and self._response.deployGoal:
              self._update_internal_deploy_goal(self._response)
  
          if self._response:
              log.info('Complete the current deploy with response: {}.'.format(self._response))
          else:
              log.info('Failed to get response from server, exit.')
function_tokens: def serve_build ( self ) : log . info ( 'The deploy agent is starting.' ) if not self . _executor : self . _executor = Executor ( callback = PingServer ( self ) , config = self . _config ) # start to ping server to get the latest deploy goal self . _response = self . _client . send_reports ( self . _envs ) if self . _response : report = self . _update_internal_deploy_goal ( self . _response ) # failed to update if report . status_code != AgentStatus . SUCCEEDED : self . _update_ping_reports ( deploy_report = report ) self . _client . send_reports ( self . _envs ) return while self . _response and self . _response . opCode and self . _response . opCode != OpCode . NOOP : try : # ensure stat_time_elapsed_internal is always running self . stat_time_elapsed_internal . resume ( ) # update the current deploy goal if self . _response . deployGoal : deploy_report = self . process_deploy ( self . _response ) # resume stat_time_elapsed_internal immediately, previous process_deploy may have paused self . stat_time_elapsed_internal . resume ( ) else : log . info ( 'No new deploy goal to get updated' ) deploy_report = DeployReport ( AgentStatus . SUCCEEDED ) if deploy_report . status_code == AgentStatus . ABORTED_BY_SERVER : log . info ( 'switch to the new deploy goal: {}' . format ( self . _response . deployGoal ) ) continue except Exception : # anything catch-up here should be treated as agent failure # resume stat_time_elapsed_internal, previous process_deploy may have paused self . stat_time_elapsed_internal . resume ( ) deploy_report = DeployReport ( status_code = AgentStatus . AGENT_FAILED , error_code = 1 , output_msg = traceback . format_exc ( ) , retry_times = 1 ) # increment stats - deploy status if self . _response . deployGoal and deploy_report : tags = { 'first_run' : self . first_run ( ) } if self . _response . deployGoal . deployStage : tags [ 'deploy_stage' ] = self . _response . deployGoal . deployStage if self . _response . deployGoal . envName : tags [ 'env_name' ] = self . _response . deployGoal . envName if self . _response . deployGoal . stageName : tags [ 'stage_name' ] = self . _response . deployGoal . stageName if deploy_report . status_code : tags [ 'status_code' ] = deploy_report . status_code create_sc_increment ( 'deployd.stats.deploy.status.sum' , 1 , tags = tags ) if PingStatus . PING_FAILED == self . update_deploy_status ( deploy_report ) : return if deploy_report . status_code in [ AgentStatus . AGENT_FAILED , AgentStatus . TOO_MANY_RETRY , AgentStatus . SCRIPT_TIMEOUT ] : log . error ( 'Unexpeted exceptions: {}, error message {}' . format ( deploy_report . status_code , deploy_report . output_msg ) ) return self . clean_stale_builds ( ) if self . _response and self . _response . deployGoal : self . _update_internal_deploy_goal ( self . _response ) if self . _response : log . info ( 'Complete the current deploy with response: {}.' . format ( self . _response ) ) else : log . info ( 'Failed to get response from server, exit.' )