require "workers/worker"

class DeleteServerWorker < Worker

  # options must contain 'server_id', 'current_user'
  def perform(options)
    server_id = options.fetch('server_id')
    owner = options['user'] || 'SYSTEM'

    call do |out, task|
      server = Devops::Model::Server.find(server_id)
      set_task_data({
        "created_by" => owner,
        "project" => server.project,
        "environment" => server.environment,
        "server_id" => server.id,
        "category" => server.category,
        "type" => Devops::Model::JobTask::DELETE_SERVER_TYPE
      })

      msg = "Trying to delete server '#{server.id}'"
      logger.info msg
      out.puts msg
      out.flush
      e = Devops::Executor::ServerExecutor.new(server, out, owner)
      e.job_task = task
      e.delete_server
    end
  end

end
