From 4442338a68a2f130f104b5b5fffdb8d54639440e Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 14:57:30 -0300 Subject: [PATCH 1/5] 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 d99f02bd0cd08558621ceaeed7e3b48b565d719b Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:06:26 -0300 Subject: [PATCH 2/5] =?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 2fac95d2e8be45eeeea51bdbadbbae71bcd27224 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:06:46 -0300 Subject: [PATCH 3/5] =?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 cd7aa1d124c3e9b7ca7d9a182dec1fd3b7758608 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:10:53 -0300 Subject: [PATCH 4/5] =?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 b2472035..fe2ab1bc 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 ae7481550a3c6d92c0cbe34df6c2c72cbdcc94fd Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:12:10 -0300 Subject: [PATCH 5/5] feat: hacer limpieza de emergencia #12854 --- Procfile | 2 +- lib/tasks/cleanup.rake | 3 ++- monit.conf | 7 +++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Procfile b/Procfile index 4cc6e5b3..14bdb0c5 100644 --- a/Procfile +++ b/Procfile @@ -8,4 +8,4 @@ prometheus: bundle exec prometheus_exporter -b 0.0.0.0 --prefix "sutty_" distributed_press_tokens_renew: bundle exec rake distributed_press:tokens:renew cleanup: bundle exec rake cleanup:everything stats: bundle exec rake stats:process_all -distributed_press_renew_tokens: bundle exec rake distributed_press:tokens:renew +emergency_cleanup: bundle exec rake cleanup:everything BEFORE=7 diff --git a/lib/tasks/cleanup.rake b/lib/tasks/cleanup.rake index 1c52abaf..20044871 100644 --- a/lib/tasks/cleanup.rake +++ b/lib/tasks/cleanup.rake @@ -2,8 +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 + CleanupJob.perform_later(before) end end diff --git a/monit.conf b/monit.conf index 0bd18907..b0aa9884 100644 --- a/monit.conf +++ b/monit.conf @@ -19,7 +19,6 @@ check program stats every "0 1 * * *" if status != 0 then alert -check program distributed_press_tokens_renew - with path "/usr/bin/foreman run -f /srv/Procfile -d /srv distributed_press_tokens_renew" as uid "rails" gid "www-data" - every "0 3 * * *" - 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"