5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 10:26:22 +00:00
panel/app/jobs/deploy_job.rb

114 lines
3 KiB
Ruby
Raw Normal View History

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
class DeployException < StandardError; end
class DeployTimedOutException < DeployException; end
discard_on ActiveRecord::RecordNotFound
# rubocop:disable Metrics/MethodLength
def perform(site, notify: true, time: Time.now, output: false)
@output = output
2019-09-18 19:28:30 +00:00
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 10.minutes.ago >= time
2023-03-13 21:46:56 +00:00
notify = false
raise DeployTimedOutException,
"#{@site.name} la tarea estuvo más de 10 minutos esperando, volviendo al estado original"
end
DeployJob.perform_in(60, site, notify: notify, time: time, output: output)
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,
size: deploy_local.size,
urls: [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
unless @deployed[:deploy_local][:status]
2020-01-24 15:12:49 +00:00
# Hacer fallar la tarea
2023-03-13 23:11:12 +00:00
raise DeployException, "#{@site.name}: Falló la compilación"
2019-09-18 19:28:30 +00:00
end
2019-07-26 00:36:33 +00:00
deploy_others
2023-03-13 23:11:12 +00:00
rescue DeployTimedOutException => e
notify_exception e
2023-03-13 23:11:12 +00:00
rescue DeployException => e
notify_exception e, deploy_local
ensure
@site&.update status: 'waiting'
notify_usuaries if notify
2019-09-18 19:28:30 +00:00
end
2019-07-26 00:36:33 +00:00
end
# rubocop:enable Metrics/MethodLength
2019-07-26 00:36:33 +00:00
private
# @param :exception [StandardError]
# @param :deploy [Deploy]
def notify_exception(exception, deploy = nil)
data = {
site: @site.id,
deploy: deploy&.type,
log: deploy&.build_stats&.last&.log
}
ExceptionNotifier.notify_exception(exception, data: data)
end
def deploy_local
@deploy_local ||= @site.deploys.find_by(type: 'DeployLocal')
end
def deploy_locally
deploy_local.deploy(output: @output)
2019-07-26 00:36:33 +00:00
end
def deploy_others
@site.deploys.where.not(type: 'DeployLocal').find_each do |d|
begin
2023-03-23 22:10:37 +00:00
status = d.deploy(output: @output)
seconds = d.build_stats.last.try(:seconds)
rescue StandardError => e
status = false
seconds = 0
notify_exception e, d
end
2022-04-06 23:48:14 +00:00
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: seconds || 0,
size: d.size,
2023-03-13 22:14:36 +00:00
urls: d.respond_to?(:urls) ? d.urls : [d.url].compact
2022-04-06 23:07:14 +00:00
}
2019-07-26 00:36:33 +00:00
end
end
def notify_usuaries
2020-04-06 22:03:49 +00:00
@site.roles.where(rol: 'usuarie', temporal: false).pluck(:usuarie_id).each do |usuarie|
DeployMailer.with(usuarie: usuarie, site: @site.id)
.deployed(@deployed)
2019-07-26 00:36:33 +00:00
.deliver_now
end
end
end