2019-07-26 00:36:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Realiza el deploy de un sitio
|
2019-09-16 16:44:29 +00:00
|
|
|
class DeployJob < ApplicationJob
|
2019-09-25 22:34:39 +00:00
|
|
|
class DeployException < StandardError; end
|
|
|
|
|
|
|
|
# rubocop:disable Metrics/MethodLength
|
2021-07-29 17:46:29 +00:00
|
|
|
def perform(site, notify = true, time = Time.now)
|
2019-09-18 19:28:30 +00:00
|
|
|
ActiveRecord::Base.connection_pool.with_connection do
|
2019-09-25 22:34:39 +00:00
|
|
|
@site = Site.find(site)
|
2021-05-10 13:56:08 +00:00
|
|
|
|
2021-07-29 17:46:29 +00:00
|
|
|
# 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.
|
2021-05-10 16:14:02 +00:00
|
|
|
if @site.building?
|
2021-07-29 17:46:29 +00:00
|
|
|
if 10.minutes.ago >= time
|
|
|
|
@site.update status: 'waiting'
|
|
|
|
raise DeployException,
|
|
|
|
"#{@site.name} la tarea estuvo más de 10 minutos esperando, volviendo al estado original"
|
|
|
|
end
|
|
|
|
|
|
|
|
DeployJob.perform_in(60, site, notify, time)
|
2021-05-10 13:56:08 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@site.update status: 'building'
|
2019-09-18 19:28:30 +00:00
|
|
|
# Asegurarse que DeployLocal sea el primero!
|
2022-04-06 23:07:14 +00:00
|
|
|
@deployed = {
|
|
|
|
deploy_local: {
|
|
|
|
status: deploy_locally,
|
|
|
|
seconds: deploy_local.build_stats.last.seconds,
|
2022-04-06 23:51:45 +00:00
|
|
|
size: deploy_local.size,
|
2022-04-06 23:16:31 +00:00
|
|
|
url: deploy_local.url
|
2022-04-06 23:07:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-26 00:36:33 +00:00
|
|
|
|
2019-09-18 19:28:30 +00:00
|
|
|
# No es opcional
|
2019-09-25 22:34:39 +00:00
|
|
|
unless @deployed[:deploy_local]
|
2021-05-10 13:56:08 +00:00
|
|
|
@site.update status: 'waiting'
|
2021-04-11 19:45:26 +00:00
|
|
|
notify_usuaries if notify
|
2020-01-24 15:12:49 +00:00
|
|
|
|
|
|
|
# Hacer fallar la tarea
|
2019-09-25 22:34:39 +00:00
|
|
|
raise DeployException, deploy_local.build_stats.last.log
|
2019-09-18 19:28:30 +00:00
|
|
|
end
|
2019-07-26 00:36:33 +00:00
|
|
|
|
2019-09-25 22:34:39 +00:00
|
|
|
deploy_others
|
2021-07-29 17:46:29 +00:00
|
|
|
|
|
|
|
# Volver a la espera
|
2021-05-10 13:56:08 +00:00
|
|
|
@site.update status: 'waiting'
|
2021-07-29 17:46:29 +00:00
|
|
|
|
|
|
|
notify_usuaries if notify
|
2019-09-18 19:28:30 +00:00
|
|
|
end
|
2019-07-26 00:36:33 +00:00
|
|
|
end
|
2019-09-25 22:34:39 +00:00
|
|
|
# rubocop:enable Metrics/MethodLength
|
2019-07-26 00:36:33 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-09-25 22:34:39 +00:00
|
|
|
def deploy_local
|
|
|
|
@deploy_local ||= @site.deploys.find_by(type: 'DeployLocal')
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_locally
|
|
|
|
deploy_local.deploy
|
2019-07-26 00:36:33 +00:00
|
|
|
end
|
|
|
|
|
2019-09-25 22:34:39 +00:00
|
|
|
def deploy_others
|
|
|
|
@site.deploys.where.not(type: 'DeployLocal').find_each do |d|
|
2022-04-06 23:48:14 +00:00
|
|
|
status = d.deploy
|
|
|
|
build_stat = d.build_stats.last
|
|
|
|
|
2022-04-06 23:07:14 +00:00
|
|
|
@deployed[d.type.underscore.to_sym] = {
|
2022-04-06 23:48:14 +00:00
|
|
|
status: status,
|
|
|
|
seconds: build_stat.try(:seconds) || 0,
|
2022-04-06 23:51:45 +00:00
|
|
|
size: d.size,
|
2022-04-06 23:07:14 +00:00
|
|
|
url: d.url
|
|
|
|
}
|
2019-07-26 00:36:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-25 22:34:39 +00:00
|
|
|
def notify_usuaries
|
2020-04-06 22:03:49 +00:00
|
|
|
@site.roles.where(rol: 'usuarie', temporal: false).pluck(:usuarie_id).each do |usuarie|
|
2020-03-24 17:14:55 +00:00
|
|
|
DeployMailer.with(usuarie: usuarie, site: @site.id)
|
2019-09-25 22:34:39 +00:00
|
|
|
.deployed(@deployed)
|
2019-07-26 00:36:33 +00:00
|
|
|
.deliver_now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|