5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 04:26:22 +00:00

BREAKING CHANGE: usar global ids en los jobs

This commit is contained in:
f 2024-04-10 11:00:18 -03:00
parent 6dbc8fa0c1
commit b0bf0c0c53
No known key found for this signature in database
17 changed files with 37 additions and 68 deletions

View file

@ -18,7 +18,7 @@ module Api
# Si todo salió bien, enviar los correos y redirigir al sitio.
# El sitio nos dice a dónde tenemos que ir.
ContactJob.perform_later site.id,
ContactJob.perform_later site,
params[:form],
contact_params.to_h.symbolize_keys,
params[:redirect]

View file

@ -11,7 +11,7 @@ module Api
# respondemos con lo mismo.
def create
if (site&.airbrake_valid? airbrake_token) && !detected_device.bot?
BacktraceJob.perform_later site_id: params[:site_id],
BacktraceJob.perform_later site: site,
params: airbrake_params.to_h
end

View file

@ -4,14 +4,10 @@
class ApplicationJob < ActiveJob::Base
include Que::ActiveJob::JobExtensions
attr_reader :site
# Si falla por cualquier cosa informar y descartar
discard_on(Exception) do |error|
ExceptionNotifier.notify_exception(error, data: { site: site.name })
end
private
def site
@site ||= Site.find @params[:site_id]
end
end

View file

@ -6,10 +6,9 @@ class BacktraceJob < ApplicationJob
EMPTY_SOURCEMAP = { 'mappings' => '' }.freeze
attr_reader :params, :site_id
attr_reader :params
def perform(site_id:, params:)
@site_id = site_id
def perform(site:, params:)
@params = params
unless sources.empty?
@ -44,10 +43,6 @@ class BacktraceJob < ApplicationJob
private
def site
@site ||= Site.find_by_id(site_id)
end
# Obtiene todos los archivos del backtrace solo si los puede descargar
# desde fuentes seguras.
#

View file

@ -5,11 +5,7 @@ class ContactJob < ApplicationJob
# @param [Integer]
# @param [String]
# @param [Hash]
def perform(site_id, form_name, form, origin = nil)
# Retrocompabilidad al actualizar a 2.7.1
# @see ApplicationJob#site
@params = { site_id: site_id }
def perform(site, form_name, form, origin = nil)
# Sanitizar los valores
form.each_key do |key|
form[key] = ActionController::Base.helpers.sanitize form[key]
@ -23,7 +19,7 @@ class ContactJob < ApplicationJob
usuaries.each_slice(10) do |u|
ContactMailer.with(form_name: form_name,
form: form,
site_id: site_id,
site: site,
usuaries_emails: u,
origin: origin)
.notify_usuaries.deliver_now

View file

@ -16,34 +16,32 @@ class DeployJob < ApplicationJob
# rubocop:disable Metrics/MethodLength
def perform(site, notify: true, time: Time.now, output: false)
@output = output
@site = site
ActiveRecord::Base.connection_pool.with_connection do
@site = Site.find(site)
# Si ya hay una tarea corriendo, aplazar esta. Si estuvo
# esperando más de 10 minutos, recuperar el estado anterior.
#
# Como el trabajo actual se aplaza al siguiente, arrastrar la
# hora original para poder ir haciendo timeouts.
if @site.building?
if site.building?
notify = false
if 10.minutes.ago >= time
raise DeployTimedOutException,
"#{@site.name} la tarea estuvo más de 10 minutos esperando, volviendo al estado original"
"#{site.name} la tarea estuvo más de 10 minutos esperando, volviendo al estado original"
else
raise DeployAlreadyRunningException
end
end
@deployed = {}
@site.update status: 'building'
@site.deployment_list.each do |d|
site.update status: 'building'
site.deployment_list.each do |d|
begin
raise DeployException, 'Una dependencia falló' if failed_dependencies? d
status = d.deploy(output: @output)
status = d.deploy(output: output)
seconds = d.build_stats.last.try(:seconds) || 0
size = d.size
urls = d.urls.map do |url|
@ -52,7 +50,7 @@ class DeployJob < ApplicationJob
nil
end.compact
if d == @site.deployment_list.last && !status
if d == site.deployment_list.last && !status
raise DeployException, 'Falló la compilación'
end
rescue StandardError => e
@ -73,7 +71,7 @@ class DeployJob < ApplicationJob
}
end
return unless @output
return unless output
puts (Terminal::Table.new do |t|
t << (%w[type] + @deployed.values.first.keys)
@ -83,12 +81,12 @@ class DeployJob < ApplicationJob
end
end)
ensure
if @site.present?
@site.update status: 'waiting'
if site.present?
site.update status: 'waiting'
notify_usuaries if notify
puts "\a" if @output
puts "\a" if output
end
end
end
@ -118,7 +116,7 @@ class DeployJob < ApplicationJob
# @param :deploy [Deploy]
def notify_exception(exception, deploy = nil)
data = {
site: @site.id,
site: site.name,
deploy: deploy&.type,
log: deploy&.build_stats&.last&.log,
failed_dependencies: (failed_dependencies(deploy) if deploy)
@ -128,8 +126,10 @@ class DeployJob < ApplicationJob
end
def notify_usuaries
@site.roles.where(rol: 'usuarie', temporal: false).pluck(:usuarie_id).each do |usuarie|
DeployMailer.with(usuarie: usuarie, site: @site.id)
usuarie_ids = site.roles.where(rol: 'usuarie', temporal: false).pluck(:usuarie_id)
Usuarie.where(id: usuarie_ids).find_each do |usuarie|
DeployMailer.with(usuarie_id: usuarie, site: site)
.deployed(@deployed)
.deliver_now
end

View file

@ -3,13 +3,9 @@
# Permite pushear los cambios cada vez que se
# hacen commits en un sitio
class GitPushJob < ApplicationJob
attr_reader :site
# @param :site [Site]
# @return [nil]
def perform(site)
@site = site
site.repository.push if site.repository.origin
end
end

View file

@ -15,8 +15,7 @@
# Lo mismo para salir de mantenimiento, agregando el atributo
# are_we_back: true al crear el Maintenance.
class MaintenanceJob < ApplicationJob
def perform(maintenance_id:)
maintenance = Maintenance.find(maintenance_id)
def perform(maintenance:)
# Decidir cuál vamos a enviar según el estado de Maintenance
mailer = maintenance.are_we_back ? :were_back : :notice

View file

@ -6,9 +6,6 @@ class PeriodicJob < ApplicationJob
STARTING_INTERVAL = Stat::INTERVALS.first
# Tener el sitio a mano
attr_reader :site
# Descartar y notificar si pasó algo más.
#
# XXX: En realidad deberíamos seguir reintentando?

View file

@ -7,8 +7,8 @@ class StatCollectionJob < PeriodicJob
STAT_NAME = 'stat_collection_job'
def perform(site_id:, once: true)
@site = Site.find site_id
def perform(site:, once: true)
@site = site
beginning = beginning_of_interval
stat = site.stats.create! name: STAT_NAME
@ -22,7 +22,7 @@ class StatCollectionJob < PeriodicJob
rollup.average(:seconds)
end
dimensions = { site_id: site_id }
dimensions = { site_id: site.id }
reduce_rollup(name: 'builds', operation: :sum, dimensions: dimensions)
reduce_rollup(name: 'space_used', operation: :average, dimensions: dimensions)

View file

@ -16,8 +16,8 @@ class UriCollectionJob < PeriodicJob
IMAGES = %w[.png .jpg .jpeg .gif .webp .jfif].freeze
STAT_NAME = 'uri_collection_job'
def perform(site_id:, once: true)
@site = Site.find site_id
def perform(site:, once: true)
@site = site
# Obtener el principio del intervalo anterior
beginning_of_interval

View file

@ -10,7 +10,7 @@ class ApplicationMailer < ActionMailer::Base
private
def site
@site ||= Site.find @params[:site_id]
@site ||= @params[:site]
end
def inline_logo!

View file

@ -13,8 +13,7 @@ class DeployMailer < ApplicationMailer
# rubocop:disable Metrics/AbcSize
def deployed(deploys = {})
usuarie = Usuarie.find(params[:usuarie])
site = usuarie.sites.find(params[:site])
usuarie = params[:usuarie]
hostname = site.hostname
deploys ||= {}

View file

@ -1,9 +0,0 @@
# frozen_string_literal: true
class InvitadxMailer < ApplicationMailer
def confirmation_required
@invitadx = params[:invitadx]
@site = params[:site]
mail from: "#{@site.config.dig('title')} <#{ENV.fetch('DEFAULT_FROM', 'sutty@kefir.red')}>", to: @invitadx.email, subject: t('.subject')
end
end

View file

@ -11,7 +11,7 @@ class LogEntry < ApplicationRecord
def resend
return if sent
ContactJob.perform_later site_id, params[:form], params
ContactJob.perform_later site, params[:form], params
end
def params

View file

@ -5,7 +5,7 @@
SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
def deploy
site.enqueue!
DeployJob.perform_later site.id
DeployJob.perform_later site
end
# Crea un sitio, agrega un rol nuevo y guarda los cambios a la

View file

@ -3,9 +3,9 @@
namespace :stats do
desc 'Process stats'
task process_all: :environment do
Site.all.pluck(:id).each do |site_id|
UriCollectionJob.perform_now site_id: site_id, once: true
StatCollectionJob.perform_now site_id: site_id, once: true
Site.all.find_each do |site|
UriCollectionJob.perform_now site: site, once: true
StatCollectionJob.perform_now site: site, once: true
end
end
end