From 5857e9c1cd80b1cb7c0d0fd5d5589b884cfd5a69 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 14:57:30 -0300 Subject: [PATCH 1/6] feat: realizar la limpieza en segundo plano --- app/jobs/cleanup_job.rb | 8 ++++++++ lib/tasks/cleanup.rake | 4 +--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 app/jobs/cleanup_job.rb 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/lib/tasks/cleanup.rake b/lib/tasks/cleanup.rake index e14693bc..1c52abaf 100644 --- a/lib/tasks/cleanup.rake +++ b/lib/tasks/cleanup.rake @@ -4,8 +4,6 @@ namespace :cleanup do desc 'Cleanup sites' task everything: :environment do before = ENV.fetch('BEFORE', '30').to_i.days.ago - service = CleanupService.new(before: before) - - service.cleanup_everything! + CleanupJob.perform_later(before) end end From ee461543307ba6cae84fd72374cbdf3eaf1e7481 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:06:26 -0300 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20informar=20qu=C3=A9=20se=20est?= =?UTF-8?q?=C3=A1=20haciendo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/cleanup_service.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/services/cleanup_service.rb b/app/services/cleanup_service.rb index ad87cf9a..01b42d9e 100644 --- a/app/services/cleanup_service.rb +++ b/app/services/cleanup_service.rb @@ -26,6 +26,8 @@ class CleanupService Site.where('updated_at < ?', before).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 @@ -40,6 +42,8 @@ class CleanupService Site.where('updated_at >= ?', before).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 From 7f263c1557ee88f0a2bd9516d23ec56dd93fe4a6 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:06:46 -0300 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20obtener=20los=20sitios=20ordenados?= =?UTF-8?q?=20para=20ir=20actualiz=C3=A1ndolos=20en=20orden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/cleanup_service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/cleanup_service.rb b/app/services/cleanup_service.rb index 01b42d9e..b0792159 100644 --- a/app/services/cleanup_service.rb +++ b/app/services/cleanup_service.rb @@ -23,7 +23,7 @@ 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}" @@ -39,7 +39,7 @@ 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}" From 63e55f16afea7c68d240b443a7c243c1c61d8eaf Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:10:53 -0300 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20detectar=20cu=C3=A1ndo=20estamos=20e?= =?UTF-8?q?n=20hainish?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index f3799638..7a1a2936 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' From 28c51cab0108284b1b9c5af28ac47f575066a4a4 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:12:10 -0300 Subject: [PATCH 5/6] feat: hacer limpieza de emergencia #12854 --- Procfile | 1 + lib/tasks/cleanup.rake | 5 ++--- monit.conf | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Procfile b/Procfile index 45fe1df7..e7f4ebb6 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,3 @@ cleanup: bundle exec rake cleanup:everything stats: bundle exec rake stats:process_all +emergency_cleanup: bundle exec rake cleanup:everything[7] diff --git a/lib/tasks/cleanup.rake b/lib/tasks/cleanup.rake index 1c52abaf..6ea48c0d 100644 --- a/lib/tasks/cleanup.rake +++ b/lib/tasks/cleanup.rake @@ -2,8 +2,7 @@ namespace :cleanup do desc 'Cleanup sites' - task everything: :environment do - before = ENV.fetch('BEFORE', '30').to_i.days.ago - CleanupJob.perform_later(before) + task everything: :environment do |_, args| + CleanupJob.perform_later((args.extras.first || 30).to_i.days.ago) end end diff --git a/monit.conf b/monit.conf index 39f45d6d..139a401d 100644 --- a/monit.conf +++ b/monit.conf @@ -13,3 +13,7 @@ check program stats with path "/usr/bin/foreman run -f /srv/Procfile -d /srv stats" as uid "rails" gid "www-data" every "0 1 * * *" if status != 0 then alert + +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" From 1d03583b2c2ff6cea8a1b70f72ebb9beab464a8c Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:26:23 -0300 Subject: [PATCH 6/6] fixup! feat: hacer limpieza de emergencia #12854 --- Procfile | 2 +- lib/tasks/cleanup.rake | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Procfile b/Procfile index e7f4ebb6..508a93c6 100644 --- a/Procfile +++ b/Procfile @@ -1,3 +1,3 @@ cleanup: bundle exec rake cleanup:everything stats: bundle exec rake stats:process_all -emergency_cleanup: bundle exec rake cleanup:everything[7] +emergency_cleanup: bundle exec rake cleanup:everything BEFORE=7 diff --git a/lib/tasks/cleanup.rake b/lib/tasks/cleanup.rake index 6ea48c0d..20044871 100644 --- a/lib/tasks/cleanup.rake +++ b/lib/tasks/cleanup.rake @@ -3,6 +3,8 @@ namespace :cleanup do desc 'Cleanup sites' task everything: :environment do |_, args| - CleanupJob.perform_later((args.extras.first || 30).to_i.days.ago) + before = ENV.fetch('BEFORE', '30').to_i.days.ago + + CleanupJob.perform_later(before) end end