<commit_before>require 'scalr'
require 'scalr/caller'
require 'scalr/check_online_status'

class ServerInstance
  attr_accessor :status
  attr_reader :id, :role

  def initialize(id, role, status = :Original)
    @id = id
    @role = role
    @status = status
  end
end

class Role
  attr_accessor :name, :id

  def initialize(name, id)
    @name = name
    @id = id
    @finished = false
  end
end

class Recycler
  def initialize(farm_id, roles_to_recycle)
    @roles_to_recycle = roles_to_recycle
    @farm_id = farm_id
    @roles = []
    @servers = []
    @replacement_servers = []
    @recycle_increment = {RailsAppServer: 5, Sidekiq: 2, Reports: 2, Bunchball: 1, DevDebug: 1, SystemWatcher: 1}
  end

  def recycle
    setup
    while @servers.length > 0 do
      @roles.each do |role|
        launch_replacements(role)
        break
      end

      monitor_startup

      @roles.each do |role|
        #terminate_originals(role)
      end
    end
  end

  private

  def setup
    @roles_to_recycle.each do |role|
      @roles << Role.new(role.name, role.id)
      role.servers.each do |server|
        @servers << ServerInstance.new(server.id, role.name) if server.status == 'Running'
      end
    end
  end

  def launch_replacements(role)
    servers_to_launch = @servers.select { |server| server.role == role.name && server.status != :Launched}
    count = @recycle_increment[role.name.to_sym] > servers_to_launch.length ? servers_to_launch.length : @recycle_increment[role.name.to_sym]
    (0..(count - 1)).each do |index|
      response = perform_launch({farm_id: @farm_id, farm_role_id: role.id, increase_max_instances: true})
      @replacement_servers << ServerInstance.new(response.content, role.name, :New)

      @servers.map! do |server|
        if server.id == servers_to_launch[index].id
          ServerInstance.new(server.id, role.name, :Launched)
        else
          server
        end
      end
    end
  end

  def monitor_startup<commit_msg>Cleanup launch routine<commit_after>require 'scalr'
require 'scalr/caller'
require 'scalr/check_online_status'

class ServerInstance
  attr_accessor :status
  attr_reader :id, :role

  def initialize(id, role, status = :Original)
    @id = id
    @role = role
    @status = status
  end
end

class Role
  attr_accessor :name, :id

  def initialize(name, id)
    @name = name
    @id = id
    @finished = false
  end
end

class Recycler
  def initialize(farm_id, roles_to_recycle)
    @roles_to_recycle = roles_to_recycle
    @farm_id = farm_id
    @roles = []
    @servers = []
    @replacement_servers = []
    @recycle_increment = {RailsAppServer: 5, Sidekiq: 2, Reports: 2, Bunchball: 1, DevDebug: 1, SystemWatcher: 1}
  end

  def recycle
    setup
    while @servers.length > 0 do
      @roles.each do |role|
        launch_replacements(role)
        break
      end

      monitor_startup

      @roles.each do |role|
        #terminate_originals(role)
      end
    end
  end

  private

  def setup
    @roles_to_recycle.each do |role|
      @roles << Role.new(role.name, role.id)
      role.servers.each do |server|
        @servers << ServerInstance.new(server.id, role.name) if server.status == 'Running'
      end
    end
  end

  def launch_replacements(role)
    servers_to_launch = @servers.select { |server|
      server.role == role.name && server.status == :Original
    }.first(@recycle_increment[role.name.to_sym])

    servers_to_launch.each do |server|
      response = perform_launch({farm_id: @farm_id, farm_role_id: role.id, increase_max_instances: true})
      @replacement_servers << ServerInstance.new(response.content, role.name, :New)
      @servers.map! do |s|
        (s.id == server.id) ? ServerInstance.new(s.id, s.role, :Launched) : s
      end
    end
  end

  def monitor_startup
{'fraction_non_alphanumeric': 0.10577683988393563, 'fraction_numerical': 0.0042205222896333425, 'mean_word_length': 2.9173553719008263, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 6, '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': '620641', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1317, 'n_words': 305}