<commit_before>SERVICE_STATES = ['starting', 'running', 'stopping', 'stopped', 'removed']

atLeastOneWithState = (services, states...) ->
  _.reduce services, ((memo, service) -> memo or service.state in states), false
allWithState =  (services, states...) ->
  _.reduce services, ((memo, service) -> memo and service.state in states), true

runningIsDesired = (doc, services) ->
  if allWithState services, 'running'
    'running'
  else if atLeastOneWithState services, 'starting'
    'starting'

stoppedIsDesired = (doc, services) ->
  if allWithState services, 'running'
    'running'
  else if allWithState services, 'removed'
    'removed'
  else if allWithState services, 'stopped', 'removed'
    'stopped'
  else if atLeastOneWithState services, 'stopping', 'stopped', 'removed'
    'stopping'


determineState = (doc, stateF) ->
  console.log 'determineState', doc
  services = _.values doc.services
  overallState = stateF doc, services
  overallState = 'failing' unless overallState
  Instances.update doc._id, $set: state: overallState

  for serviceName, service of doc.services
    if service.state is 'running'
      Instances.update {_id: doc._id, 'steps.name': serviceName}, {$set: {'steps.$.completed': true}}

f = false

Instances.find({}, fields: {_id: 1}).observe
  added: (doc) ->
    if f then Instances.update doc._id, $set:
      state: 'created'
      desiredState: 'running'

Instances.find({state: 'removed', desiredState: 'stopped'}, fields: {_id: 1}).observe
  added: (doc, olddoc) -> Instances.remove doc._id

Instances.find({desiredState: 'running'}, fields: {services: 1}).observe
  changed: (doc, oldDoc) -> determineState doc, runningIsDesired

Instances.find({desiredState: 'stopped'}, fields: {services: 1}).observe
  changed: (doc, oldDoc) -> determineState doc, stoppedIsDesired

setStateDescription = (instanceName, stateDescription) ->
  Instances.upsert {name: instanceName}, $set:
    'state.description': stateDescription

f = true

module.exports =
  updateServiceState: (mappedState, labels) ->
    unless mappedState in SERVICE_STATES
      throw "Service state '#{mappedState}' is not supported."
    if name = labels['bigboat/instance/name']
      updateDoc = name: name

      if (appName = labels['bigboat/application/name']) and
        (appVersion = labels['bigboat/application/version'])
          updateDoc.app = name: appName, version: appVersion

      updateDoc.startedBy = startedBy if startedBy = labels['bigboat/startedBy']

      updateDoc['agent.url'] = val if val = labels['bigboat/agent/url']

      service = labels['bigboat/service/name']
      search = {name: name}

      updateDoc["services.#{service}.state"] = mappedState

      Instances.upsert search, $set: updateDoc

  #
  # Updates the internal state based on image pulls
  #
  imagePull: (image) ->
    updated = Instances.update {'steps.image': image}, {$set: {'steps.$.completed': true}}
<commit_msg>allow 'restarting' as service state. expose functions to reconcile service state<commit_after>SERVICE_STATES = ['starting', 'restarting', 'running', 'stopping', 'stopped', 'removed']

atLeastOneWithState = (services, states...) ->
  _.reduce services, ((memo, service) -> memo or service.state in states), false
allWithState =  (services, states...) ->
  _.reduce services, ((memo, service) -> memo and service.state in states), true

runningIsDesired = (doc, services) ->
  if allWithState services, 'running'
    'running'
  else if atLeastOneWithState services, 'starting'
    'starting'

stoppedIsDesired = (doc, services) ->
  if allWithState services, 'running'
    'running'
  else if allWithState services, 'removed'
    'removed'
  else if allWithState services, 'stopped', 'removed'
    'stopped'
  else if atLeastOneWithState services, 'stopping', 'stopped', 'removed'
    'stopping'


determineState = (doc, stateF) ->
  console.log 'determineState', doc
  services = _.values doc.services
  overallState = stateF doc, services
  overallState = 'failing' unless overallState
  Instances.update doc._id, $set: state: overallState

  for serviceName, service of doc.services
    if service.state is 'running'
      Instances.update {_id: doc._id, 'steps.name': serviceName}, {$set: {'steps.$.completed': true}}

f = false

Instances.find({}, fields: {_id: 1}).observe
  added: (doc) ->
    if f then Instances.update doc._id, $set:
      state: 'created'
      desiredState: 'running'
      status: 'Created'

Instances.find({state: 'removed', desiredState: 'stopped'}, fields: {_id: 1}).observe
  added: (doc, olddoc) -> Instances.remove doc._id

Instances.find({desiredState: 'running'}, fields: {services: 1}).observe
  changed: (doc, oldDoc) -> determineState doc, runningIsDesired

Instances.find({desiredState: 'stopped'}, fields: {services: 1}).observe
  changed: (doc, oldDoc) -> determineState doc, stoppedIsDesired

setStateDescription = (instanceName, stateDescription) ->
  Instances.upsert {name: instanceName}, $set:
    'state.description': stateDescription

f = true

setServiceField = (fieldName, fieldValue, labels) ->
  if (name = labels['bigboat/instance/name']) and (service = labels['bigboat/service/name'])
    Instances.upsert {name: name}, $set: {"services.#{service}.#{fieldName}": fieldValue}

module.exports =
  updateServiceFQDN: (fqdn, labels) -> setServiceField 'fqdn', fqdn, labels

  updateServicePorts: (ports, labels) -> setServiceField 'ports', ports, labels

  updateContainerName: (name, labels) -> setServiceField 'container.name', name, labels


  updateServiceState: (mappedState, labels) ->
    unless mappedState in SERVICE_STATES
      throw "Service state '#{mappedState}' is not supported."
    if name = labels['bigboat/instance/name']
      updateDoc = name: name

      if (appName = labels['bigboat/application/name']) and
        (appVersion = labels['bigboat/application/version'])
          updateDoc.app = name: appName, version: appVersion

      updateDoc.startedBy = startedBy if startedBy = labels['bigboat/startedBy']

      updateDoc['agent.url'] = val if val = labels['bigboat/agent/url']

      service = labels['bigboat/service/name']
      search = {name: name}

      updateDoc["services.#{service}.state"] = mappedState

      Instances.upsert search, $set: updateDoc

  #
  # Updates the internal state based on image pulls
  #
  imagePull: (image) ->
    updated = Instances.update {'steps.image': image}, {$set: {'steps.$.completed': true}}

  imagePulling: (image, instance) ->
    Instances.upsert {name: instance}, $set: status: "Pulling #{image}"

{'fraction_non_alphanumeric': 0.1378048780487805, 'fraction_numerical': 0.0012195121951219512, 'mean_word_length': 4.223726114649682, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 36, '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': '1542790', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1997, 'n_words': 554}