2022-04-15 13:41:22 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Realiza tareas de limpieza en todos los sitios, para optimizar y
|
|
|
|
# liberar espacio.
|
|
|
|
class CleanupService
|
|
|
|
# Días de antigüedad de los sitios
|
|
|
|
attr_reader :before
|
|
|
|
|
|
|
|
# @param :before [ActiveSupport::TimeWithZone] Cuánto tiempo lleva sin usarse un sitio.
|
|
|
|
def initialize(before: 30.days.ago)
|
|
|
|
@before = before
|
|
|
|
end
|
|
|
|
|
|
|
|
# Limpieza general
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
def cleanup_everything!
|
|
|
|
cleanup_older_sites!
|
|
|
|
cleanup_newer_sites!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Encuentra todos los sitios sin actualizar y realiza limpieza.
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
def cleanup_older_sites!
|
2023-03-29 18:06:46 +00:00
|
|
|
Site.where('updated_at < ?', before).order(updated_at: :desc).find_each do |site|
|
2022-04-15 13:41:22 +00:00
|
|
|
next unless File.directory? site.path
|
|
|
|
|
2023-03-29 18:06:26 +00:00
|
|
|
Rails.logger.info "Limpiando dependencias, archivos temporales y repositorio git de #{site.name}"
|
|
|
|
|
2022-04-15 13:41:22 +00:00
|
|
|
site.deploys.find_each(&:cleanup!)
|
|
|
|
|
|
|
|
site.repository.gc
|
2023-12-12 17:15:23 +00:00
|
|
|
lfs_cleanup
|
2022-04-15 13:41:22 +00:00
|
|
|
site.touch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tareas para los sitios en uso
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
def cleanup_newer_sites!
|
2023-03-29 18:06:46 +00:00
|
|
|
Site.where('updated_at >= ?', before).order(updated_at: :desc).find_each do |site|
|
2022-04-15 13:41:22 +00:00
|
|
|
next unless File.directory? site.path
|
|
|
|
|
2023-03-29 18:06:26 +00:00
|
|
|
Rails.logger.info "Limpiando repositorio git de #{site.name}"
|
|
|
|
|
2022-04-15 13:41:22 +00:00
|
|
|
site.repository.gc
|
2023-12-12 17:15:23 +00:00
|
|
|
lfs_cleanup
|
2022-04-15 13:41:22 +00:00
|
|
|
site.touch
|
|
|
|
end
|
|
|
|
end
|
2023-12-12 17:15:23 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
def lfs_cleanup
|
|
|
|
site.repository.git_sh("git", "lfs", "prune")
|
|
|
|
site.repository.git_sh("git", "lfs", "dedup")
|
|
|
|
end
|
2022-04-15 13:41:22 +00:00
|
|
|
end
|