From 45559ad58f30dd3034a3f01e44cfe7dbcaf55506 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 7 Aug 2021 18:30:07 -0300 Subject: [PATCH] Refactorizar DeployJob --- app/jobs/deploy_job.rb | 49 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/app/jobs/deploy_job.rb b/app/jobs/deploy_job.rb index a5e05e4..31803ae 100644 --- a/app/jobs/deploy_job.rb +++ b/app/jobs/deploy_job.rb @@ -4,64 +4,63 @@ class DeployJob < ApplicationJob class DeployException < StandardError; end - attr_reader :site + attr_reader :site, :deployed # rubocop:disable Metrics/MethodLength - def perform(site, notify = true, time = Time.now) + def perform(site_id, notify = true, time = Time.now) ActiveRecord::Base.connection_pool.with_connection do - @site = Site.find(site) + @site = Site.find(site_id) + @deployed = {} # 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? if 10.minutes.ago >= time - @site.update status: 'waiting' + site.update status: 'waiting' raise DeployException, - "#{@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" end - DeployJob.perform_in(60, site, notify, time) + DeployJob.perform_in(60, site_id, notify, time) return end - @site.update status: 'building' + site.update status: 'building' # Asegurarse que DeployLocal sea el primero! - @deployed = { deploy_local: site.deploy_local.deploy } + deployed[:deploy_local] = site.deploy_local.deploy - # TODO: No es opcional? - unless @deployed[:deploy_local] - @site.update status: 'waiting' - notify_usuaries if notify - - # Hacer fallar la tarea - raise DeployException, deploy_local.build_stats.last.log - end - - deploy_others + deploy_others if deployed[:deploy_local] # Volver a la espera - @site.update status: 'waiting' + site.update status: 'waiting' notify_usuaries if notify + + # Hacer fallar la tarea para enterarnos. + raise DeployException, site.deploy_local.build_stats.last.log unless deployed[:deploy_local] end end # rubocop:enable Metrics/MethodLength private + # Correr todas las tareas que no sean el deploy local. def deploy_others - @site.deploys.where.not(type: 'DeployLocal').find_each do |d| - @deployed[d.type.underscore.to_sym] = d.deploy + site.deploys.where.not(type: 'DeployLocal').find_each do |d| + deployed[d.type.underscore.to_sym] = d.deploy end end + # Notificar a todes les usuaries no temporales. + # + # TODO: Poder configurar quiénes quieren recibir notificaciones. def notify_usuaries - @site.roles.where(rol: 'usuarie', temporal: false).pluck(:usuarie_id).each do |usuarie| - DeployMailer.with(usuarie: usuarie, site: @site.id) - .deployed(@deployed) + site.roles.where(rol: 'usuarie', temporal: false).pluck(:usuarie_id).each do |usuarie| + DeployMailer.with(usuarie: usuarie, site: site.id) + .deployed(deployed) .deliver_now end end