You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: purge_ems.rb
    path of file: ./repos/manageiq/tools
    
    the code of the file until where you have to start completion: #!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
require 'optimist'

ARGV.shift if ARGV[0] == '--' # if invoked with rails runner
batch_message = "For each relationship
    
def destroy_parent_ems_in_batches(ems_id, batch, timeout)
  ems     = ExtManagementSystem.find(ems_id)
  timeout = timeout.to_i.minutes

  log("Adding work items to remove child managers first...")
  ems.child_managers.each { |e| destroy_ems_in_batches(e, batch, timeout, PRIORITY - 10) }
  log("Adding work items to remove child managers first...Complete")

  log("Adding work items to remove targeted management system...")
  destroy_ems_in_batches(ems, batch, timeout, PRIORITY)
  log("Adding work items to remove targeted management system...Complete")
rescue ActiveRecord::RecordNotFound
  log("Management System with id: #{ems_id} not found!")
  exit 1
end