<commit_before>class KodingKite_KloudKite extends KodingKite

  @constructors['kloud'] = this

  @createApiMapping
    stop         : 'stop'
    start        : 'start'
    build        : 'build'
    event        : 'event'
    reinit       : 'reinit'
    resize       : 'resize'
    restart      : 'restart'
    destroy      : 'destroy'
    setDomain    : 'domain.set'
    addDomain    : 'domain.add'
    unsetDomain  : 'domain.unset'
    removeDomain : 'domain.remove'


  constructor: (options) ->
    super options
    @requestingInfo = KD.utils.dict()
    @needsRequest   = KD.utils.dict()

    @_reconnectedOnce = no

  # first info request sends message to kite requesting info
  # subsequent info requests while the first request is pending
  # will be queued up and resolved by the pending request

  info: ({ machineId, currentState }) ->

    if @needsRequest[machineId] in [undefined, yes]

      @needsRequest[machineId] = no

      @askInfoFromKlient machineId, (klientInfo)=>

        if klientInfo?

          @resolveRequestingInfos machineId, klientInfo

        else

          @askInfoFromKloud machineId, currentState

    new Promise (resolve, reject) =>
      @requestingInfo[machineId] ?= []
      @requestingInfo[machineId].push { resolve, reject }


  resolveRequestingInfos: (machineId, info)->

    @requestingInfo?[machineId]?.forEach ({ resolve }) ->
      resolve info

    @requestingInfo[machineId] = null
    @needsRequest[machineId]   = yes


  askInfoFromKlient: (machineId, callback) ->

    {kontrol, computeController} = KD.singletons
    {klient} = kontrol.kites
    machine  = computeController.findMachineFromMachineId machineId

    unless machineId?
      return callback null

    klientKite = klient?[machine.uid]

    unless klientKite?

      if machine.status.state is Machine.State.Running

        klientKite = kontrol.getKite
          name            : "klient"
          queryString     : machine.queryString
          correlationName : machine.uid

      else
        return callback null

    KD.remote.api.DataDog.increment "KlientInfo", noop

    klientKite.ping()

      .then (res)->

        if res is "pong"
        then callback State: Machine.State.Running, via: "klient"
        else
          computeController.invalidateCache machineId
          callback null

      .timeout 5000

      .catch ->

        callback null


  askInfoFromKloud: (machineId, currentState) ->

    {kontrol, computeController} = KD.singletons

    KD.remote.api.DataDog.increment "KloudInfo", noop

    @tell 'info', { machineId }

      .then (info) =>

        @resolveRequestingInfos machineId, info

        unless info.State is Machine.State.Running
          computeController.invalidateCache machineId

      .timeout ComputeController.timeout
<commit_msg>Kloud: remove old fashioned DataDog metric calls<commit_after>class KodingKite_KloudKite extends KodingKite

  @constructors['kloud'] = this

  @createApiMapping
    stop         : 'stop'
    start        : 'start'
    build        : 'build'
    event        : 'event'
    reinit       : 'reinit'
    resize       : 'resize'
    restart      : 'restart'
    destroy      : 'destroy'
    setDomain    : 'domain.set'
    addDomain    : 'domain.add'
    unsetDomain  : 'domain.unset'
    removeDomain : 'domain.remove'


  constructor: (options) ->
    super options
    @requestingInfo = KD.utils.dict()
    @needsRequest   = KD.utils.dict()

    @_reconnectedOnce = no

  # first info request sends message to kite requesting info
  # subsequent info requests while the first request is pending
  # will be queued up and resolved by the pending request

  info: ({ machineId, currentState }) ->

    if @needsRequest[machineId] in [undefined, yes]

      @needsRequest[machineId] = no

      @askInfoFromKlient machineId, (klientInfo)=>

        if klientInfo?

          @resolveRequestingInfos machineId, klientInfo

        else

          @askInfoFromKloud machineId, currentState

    new Promise (resolve, reject) =>
      @requestingInfo[machineId] ?= []
      @requestingInfo[machineId].push { resolve, reject }


  resolveRequestingInfos: (machineId, info)->

    @requestingInfo?[machineId]?.forEach ({ resolve }) ->
      resolve info

    @requestingInfo[machineId] = null
    @needsRequest[machineId]   = yes


  askInfoFromKlient: (machineId, callback) ->

    {kontrol, computeController} = KD.singletons
    {klient} = kontrol.kites
    machine  = computeController.findMachineFromMachineId machineId

    unless machineId?
      return callback null

    klientKite = klient?[machine.uid]

    unless klientKite?

      if machine.status.state is Machine.State.Running

        klientKite = kontrol.getKite
          name            : "klient"
          queryString     : machine.queryString
          correlationName : machine.uid

      else
        return callback null

    klientKite.ping()

      .then (res)->

        if res is "pong"
        then callback State: Machine.State.Running, via: "klient"
        else
          computeController.invalidateCache machineId
          callback null

      .timeout 5000

      .catch ->

        callback null


  askInfoFromKloud: (machineId, currentState) ->

    {kontrol, computeController} = KD.singletons

    @tell 'info', { machineId }

      .then (info) =>

        @resolveRequestingInfos machineId, info

        unless info.State is Machine.State.Running
          computeController.invalidateCache machineId

      .timeout ComputeController.timeout

{'fraction_non_alphanumeric': 0.08757674250631997, 'fraction_numerical': 0.001444564824846515, 'mean_word_length': 2.4727272727272727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5661014', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1545, 'n_words': 394}