Skip to content

Instantly share code, notes, and snippets.

@rawman
Created November 23, 2018 06:26
Show Gist options
  • Save rawman/087a4378e6659e44d389a3067015b7d6 to your computer and use it in GitHub Desktop.
Save rawman/087a4378e6659e44d389a3067015b7d6 to your computer and use it in GitHub Desktop.
class MarkObsoleteAppointmentsJobConsole
BATCH_SIZE = 100
def perform
latest_end_at = LastObsolete.first.end_at
logger = Logger.new("log/#{Rails.env}-obsolete.log")
appointments = Appointment.
from("appointments FORCE INDEX (index_appointments_on_end_at)").
includes(:appointment_contexts, :appointment_invitations, :appointment_associations).
where("end_at <= :obsolete_threshold and end_at > :last_obsolete", {
obsolete_threshold: Time.now - Rails.configuration.cosmic_sync.obsolete_threshold,
last_obsolete: latest_end_at,
});nil
logger.info("Marking #{appointments.count} as obsolete")
count = 0
appointments.find_each(batch_size: 100) do |appointment|
publish_for_appointment(appointment) unless is_eligible?(appointment)
latest_end_at = appointment.end_at if latest_end_at < appointment.end_at
puts "progress #{count}"
count = count + 1
end
logger.info("New last obsolete end_at: #{latest_end_at}")
logger.info("Done")
LastObsolete.update_all(end_at: latest_end_at)
end
private
def publish_for_appointment(appointment)
EventV2Publisher.sync_remove_event(appointment)
end
def is_eligible?(appointment)
SyncEligibility.check(appointment.account_id, appointment.start_at, appointment.end_at)
end
end
SELECT `appointments`.* FROM `appointments` WHERE (end_at <= '2018-10-02 00:22:39' and end_at > '2018-10-01 23:12:34') AND (`appointments`.`id` > 73251903) ORDER BY `appointments`.`id` ASC LIMIT 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment