namespace :ext_batch do

desc 'Initialize and install necessary workers to monitor files and corresponding processes in batch_pids'
task install: :environment do |task|
  require 'FileUtils'
  require 'ext_logger/common'
  message = "[#{task.name}]"
  # Next is task logic.
  begin
    puts "#{message} START"

    if !defined? Rails
      puts "#{message} ERROR: Rails class not found."
      exit!(0)
    end

    batch_file_path = "../../templates/workers/ext_batch_monitor.rb"
    target_path = "app/workers"

    FileUtils.mkdir_p(File.dirname(target_path))

    FileUtils.cp_r(batch_file_path, target_path)

    puts "#{message} END"
  rescue Exception => e
    puts e.message + "\n" + e.backtrace.join("\n")
  end
end

end