diff --git a/Gemfile b/Gemfile index 21d46967..556140dc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,9 @@ # frozen_string_literal: true -puts 'Usa haini.sh para generar un entorno de trabajo reproducible' +if ENV['RAILS_ENV'] != 'production' && ENV['HAIN_ENV'].nil? + puts 'Usa haini.sh para generar un entorno de trabajo reproducible' +end + source 'https://gems.sutty.nl' ruby '~> 2.7' diff --git a/Procfile b/Procfile index 0eb0fccb..16af87c6 100644 --- a/Procfile +++ b/Procfile @@ -2,3 +2,4 @@ cleanup: bundle exec rake cleanup:everything stats: bundle exec rake stats:process_all distributed_press_renew_tokens: bundle exec rake distributed_press:tokens:renew que: daemonize -c /srv/ -p /srv/tmp/que.pid -u rails /usr/local/bin/syslogize bundle exec que +emergency_cleanup: bundle exec rake cleanup:everything BEFORE=7 diff --git a/app/jobs/cleanup_job.rb b/app/jobs/cleanup_job.rb new file mode 100644 index 00000000..13053cb0 --- /dev/null +++ b/app/jobs/cleanup_job.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Realiza tareas de limpieza en segundo plano +class CleanupJob < ApplicationJob + def perform(before = nil) + CleanupService.new(before: before).cleanup_everything! + end +end diff --git a/app/services/cleanup_service.rb b/app/services/cleanup_service.rb index ad87cf9a..b0792159 100644 --- a/app/services/cleanup_service.rb +++ b/app/services/cleanup_service.rb @@ -23,9 +23,11 @@ class CleanupService # # @return [nil] def cleanup_older_sites! - Site.where('updated_at < ?', before).find_each do |site| + Site.where('updated_at < ?', before).order(updated_at: :desc).find_each do |site| next unless File.directory? site.path + Rails.logger.info "Limpiando dependencias, archivos temporales y repositorio git de #{site.name}" + site.deploys.find_each(&:cleanup!) site.repository.gc @@ -37,9 +39,11 @@ class CleanupService # # @return [nil] def cleanup_newer_sites! - Site.where('updated_at >= ?', before).find_each do |site| + Site.where('updated_at >= ?', before).order(updated_at: :desc).find_each do |site| next unless File.directory? site.path + Rails.logger.info "Limpiando repositorio git de #{site.name}" + site.repository.gc site.touch end diff --git a/lib/tasks/cleanup.rake b/lib/tasks/cleanup.rake index e14693bc..20044871 100644 --- a/lib/tasks/cleanup.rake +++ b/lib/tasks/cleanup.rake @@ -2,10 +2,9 @@ namespace :cleanup do desc 'Cleanup sites' - task everything: :environment do + task everything: :environment do |_, args| before = ENV.fetch('BEFORE', '30').to_i.days.ago - service = CleanupService.new(before: before) - service.cleanup_everything! + CleanupJob.perform_later(before) end end diff --git a/monit.conf b/monit.conf index 567f72a6..91f4c25f 100644 --- a/monit.conf +++ b/monit.conf @@ -22,3 +22,7 @@ check program distributed_press_tokens_renew check process que with pidfile /srv/tmp/que.pid start program = "/usr/bin/foreman run -f /srv/Procfile -d /srv que" stop program = "/bin/sh -c 'cat /srv/tmp/que.pid | xargs -r kill'" + +check filesystem root with path / + if space usage > 80% for 5 times within 15 cycles then alert + if space usage > 90% for 5 cycles then exec "/usr/bin/foreman run -f /srv/Procfile -d /srv emergency_cleanup" as uid "rails" gid "www-data"