class InstanceCleanupJob < ApplicationJob
  queue_as :default

  def perform(*args)
    instances = Instance.all
    to_be_deleted = instances.filter { |i|
      # Mark instances older than 2 days for deletion
      (Time.now.utc - i.created_at) / 2.days >= 1
    }
    Instance.where(id: to_be_deleted.map { |m| m.id }).destroy_all
    print "#{to_be_deleted.length} instances destroyed\n"
  end
end
