5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-17 05:16:22 +00:00

Merge branch 'cleanup' into 'rails'

hacer tareas de limpieza

See merge request sutty/sutty!91
This commit is contained in:
fauno 2023-03-23 21:53:31 +00:00
commit 4aba6379d8
9 changed files with 97 additions and 33 deletions

View file

@ -2,3 +2,4 @@
*
# Solo agregar lo que usamos en COPY
# !./archivo
!./monit.conf

View file

@ -15,6 +15,8 @@ RUN apk add --no-cache libxslt libxml2 postgresql-libs libssh2 \
RUN gem install --no-document --no-user-install foreman
RUN wget https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-linux-amd64.tar.gz -O - | tar --strip-components 1 -xvzf - pandoc-${PANDOC_VERSION}/bin/pandoc && mv /bin/pandoc /usr/bin/pandoc
COPY ./monit.conf /etc/monit.d/sutty.conf
VOLUME "/srv"
EXPOSE 3000

View file

@ -1,8 +1,2 @@
migrate: bundle exec rake db:prepare db:seed
sutty: bundle exec puma config.ru
blazer_5m: bundle exec rake blazer:run_checks SCHEDULE="5 minutes"
blazer_1h: bundle exec rake blazer:run_checks SCHEDULE="1 hour"
blazer_1d: bundle exec rake blazer:run_checks SCHEDULE="1 day"
blazer: bundle exec rake blazer:send_failing_checks
prometheus: bundle exec prometheus_exporter -b 0.0.0.0 --prefix "sutty_"
cleanup: bundle exec rake cleanup:everything
stats: bundle exec rake stats:process_all

View file

@ -23,6 +23,9 @@ class Deploy < ApplicationRecord
raise NotImplementedError
end
# Realizar tareas de limpieza.
def cleanup!; end
def time_start
@start = Time.now
end

View file

@ -45,6 +45,17 @@ class DeployLocal < Deploy
File.join(Rails.root, '_deploy', site.hostname)
end
# Libera espacio eliminando archivos temporales
#
# @return [nil]
def cleanup!
FileUtils.rm_rf(gems_dir)
FileUtils.rm_rf(yarn_cache_dir)
FileUtils.rm_rf(File.join(site.path, 'node_modules'))
FileUtils.rm_rf(File.join(site.path, '.sass-cache'))
FileUtils.rm_rf(File.join(site.path, '.jekyll-cache'))
end
private
def mkdir

View file

@ -147,6 +147,23 @@ class Site
rugged.index.remove(relativize(file))
end
# Garbage collection
#
# @return [Boolean]
def gc
env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path }
cmd = 'git gc'
r = nil
Dir.chdir(path) do
Open3.popen2e(env, cmd, unsetenv_others: true) do |_, _, t|
r = t.value
end
end
r&.success?
end
private
# Si Sutty tiene una llave privada de tipo ED25519, devuelve las

View file

@ -0,0 +1,47 @@
# 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!
Site.where('updated_at < ?', before).find_each do |site|
next unless File.directory? site.path
site.deploys.find_each(&:cleanup!)
site.repository.gc
site.touch
end
end
# Tareas para los sitios en uso
#
# @return [nil]
def cleanup_newer_sites!
Site.where('updated_at >= ?', before).find_each do |site|
next unless File.directory? site.path
site.repository.gc
site.touch
end
end
end

11
lib/tasks/cleanup.rake Normal file
View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
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!
end
end

View file

@ -1,29 +1,7 @@
check process sutty with pidfile /srv/tmp/puma.pid
start program = "/usr/local/bin/sutty start"
stop program = "/usr/local/bin/sutty stop"
check process prometheus with pidfile /tmp/prometheus.pid
start program = "/usr/local/bin/sutty prometheus start"
stop program = "/usr/local/bin/sutty prometheus start"
check program blazer_5m
with path "/usr/local/bin/sutty blazer 5m"
every 5 cycles
if status != 0 then alert
check program blazer_1h
with path "/usr/local/bin/sutty blazer 1h"
every 60 cycles
if status != 0 then alert
check program blazer_1d
with path "/usr/local/bin/sutty blazer 1d"
every 1440 cycles
if status != 0 then alert
check program blazer
with path "/usr/local/bin/sutty blazer"
every 61 cycles
# Limpiar mensualmente
check program cleanup
with path "/usr/bin/foreman run -f /srv/Procfile -d /srv cleanup" as uid "rails" gid "www-data"
every "0 3 1 * *"
if status != 0 then alert
check program access_logs