diff --git a/app/jobs/communication/website/deuxfleurs/golive_job.rb b/app/jobs/communication/website/deuxfleurs/golive_job.rb new file mode 100644 index 0000000000..cde6178576 --- /dev/null +++ b/app/jobs/communication/website/deuxfleurs/golive_job.rb @@ -0,0 +1,7 @@ +class Communication::Website::Deuxfleurs::GoliveJob < Communication::Website::BaseJob + queue_as :mice + + def execute + website.deuxfleurs_golive_safely + end +end diff --git a/app/models/communication/website/with_deuxfleurs.rb b/app/models/communication/website/with_deuxfleurs.rb index 82adabe418..425cce1cc5 100644 --- a/app/models/communication/website/with_deuxfleurs.rb +++ b/app/models/communication/website/with_deuxfleurs.rb @@ -2,8 +2,8 @@ module Communication::Website::WithDeuxfleurs extend ActiveSupport::Concern included do - before_save :deuxfleurs_golive, if: :hosted_with_deuxfleurs? - after_save :deuxfleurs_setup, if: :hosted_with_deuxfleurs? + after_save :deuxfleurs_setup, if: -> { hosted_with_deuxfleurs? && !deuxfleurs_setup_done? } + after_save :deuxfleurs_golive, if: -> { hosted_with_deuxfleurs? && saved_change_to_in_production? && in_production } end # 4 options: @@ -12,11 +12,13 @@ module Communication::Website::WithDeuxfleurs # 3. repo exists, deuxfleurs hosting : only create deuxfleurs hosting # 4. both exists, deuxfleurs hosting needs to change identifier (Waiting for API possibility) def deuxfleurs_setup - return unless hosted_with_deuxfleurs? - return if deuxfleurs_setup_done? Communication::Website::Deuxfleurs::SetupJob.perform_later(id) end + def deuxfleurs_golive + Communication::Website::Deuxfleurs::GoliveJob.perform_later(id) + end + # Appelé par Communication::Website::Deuxfleurs::SetupJob def deuxfleurs_setup_safely return unless hosted_with_deuxfleurs? @@ -34,6 +36,18 @@ def deuxfleurs_setup_safely end end + def deuxfleurs_golive_safely + return unless hosted_with_deuxfleurs? && in_production? + # https://www.test.com -> www.test.com + new_identifier = URI(url).host + should_rename = self.deuxfleurs_identifier != new_identifier + + if should_rename && !deuxfleurs.rename_bucket(self.deuxfleurs_identifier, new_identifier) + raise "Failed to rename bucket from #{self.deuxfleurs_identifier} to #{new_identifier}" + end + update(deuxfleurs_identifier: new_identifier) + end + def deuxfleurs_destroy_bucket return unless hosted_with_deuxfleurs? deuxfleurs.empty_and_delete_bucket(deuxfleurs_identifier) @@ -45,17 +59,6 @@ def deuxfleurs_setup_done? hosted_with_deuxfleurs? && repository.present? && deuxfleurs_identifier.present? end - def deuxfleurs_golive - return unless in_production_changed? && in_production - # https://www.test.com -> www.test.com - new_identifier = URI(url).host - if deuxfleurs.rename_bucket(self.deuxfleurs_identifier, new_identifier) - self.deuxfleurs_identifier = new_identifier - else - errors.add :url - end - end - def deuxfleurs_create_bucket bucket_info = deuxfleurs.create_bucket(deuxfleurs_default_identifier) update_columns deuxfleurs_identifier: bucket_info[:identifier],