From 1c931f0784fc15601101fe356585812c93ac1587 Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Mon, 5 Mar 2018 21:57:48 +0900 Subject: Rework to minimize changes --- app/services/projects/update_pages_service.rb | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'app/services/projects/update_pages_service.rb') diff --git a/app/services/projects/update_pages_service.rb b/app/services/projects/update_pages_service.rb index c760bd3b626..d3e792b9232 100644 --- a/app/services/projects/update_pages_service.rb +++ b/app/services/projects/update_pages_service.rb @@ -1,5 +1,6 @@ module Projects class UpdatePagesService < BaseService + InvaildStateError = Class.new(StandardError) BLOCK_SIZE = 32.kilobytes MAX_SIZE = 1.terabyte SITE_PATH = 'public/'.freeze @@ -11,13 +12,15 @@ module Projects end def execute + register_attempt + # Create status notifying the deployment of pages @status = create_status @status.enqueue! @status.run! - raise 'missing pages artifacts' unless build.artifacts? - raise 'pages are outdated' unless latest? + raise InvaildStateError, 'missing pages artifacts' unless build.artifacts? + raise InvaildStateError, 'pages are outdated' unless latest? # Create temporary directory in which we will extract the artifacts FileUtils.mkdir_p(tmp_path) @@ -26,24 +29,22 @@ module Projects # Check if we did extract public directory archive_public_path = File.join(archive_path, 'public') - raise 'pages miss the public folder' unless Dir.exist?(archive_public_path) - raise 'pages are outdated' unless latest? + raise InvaildStateError, 'pages miss the public folder' unless Dir.exist?(archive_public_path) + raise InvaildStateError, 'pages are outdated' unless latest? deploy_page!(archive_public_path) success end - rescue => e + rescue InvaildStateError => e register_failure error(e.message) - ensure - register_attempt - build.erase_artifacts! unless build.has_expiring_artifacts? end private def success @status.success + delete_artifact! super end @@ -52,6 +53,7 @@ module Projects @status.allow_failure = !latest? @status.description = message @status.drop(:script_failure) + delete_artifact! super end @@ -163,6 +165,11 @@ module Projects build.artifacts_file.path end + def delete_artifact! + build.reload + build.erase_artifacts! unless build.has_expiring_artifacts? + end + def latest_sha project.commit(build.ref).try(:sha).to_s end -- cgit v1.2.1 From bbbf8e6a0234f1195a69facabab840679fb70dde Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Mon, 5 Mar 2018 21:59:16 +0900 Subject: Fix comment --- app/services/projects/update_pages_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/services/projects/update_pages_service.rb') diff --git a/app/services/projects/update_pages_service.rb b/app/services/projects/update_pages_service.rb index d3e792b9232..016660d5a72 100644 --- a/app/services/projects/update_pages_service.rb +++ b/app/services/projects/update_pages_service.rb @@ -166,7 +166,7 @@ module Projects end def delete_artifact! - build.reload + build.reload # Reload stable object to prevent erase artifacts with old state build.erase_artifacts! unless build.has_expiring_artifacts? end -- cgit v1.2.1 From c8d1a04f30f858ff66cff6f9168b4a3fc6f88acf Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Tue, 6 Mar 2018 03:15:42 +0900 Subject: Add empty line after custom error difinition --- app/services/projects/update_pages_service.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/services/projects/update_pages_service.rb') diff --git a/app/services/projects/update_pages_service.rb b/app/services/projects/update_pages_service.rb index 016660d5a72..cc50a23e8f7 100644 --- a/app/services/projects/update_pages_service.rb +++ b/app/services/projects/update_pages_service.rb @@ -1,6 +1,7 @@ module Projects class UpdatePagesService < BaseService InvaildStateError = Class.new(StandardError) + BLOCK_SIZE = 32.kilobytes MAX_SIZE = 1.terabyte SITE_PATH = 'public/'.freeze -- cgit v1.2.1 From 7421604bd0855f7855f5562769acc9bc871fb631 Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Tue, 6 Mar 2018 04:37:09 +0900 Subject: Introduce FailedToExtractError. Fix spec. Add DNS test mock. --- app/services/projects/update_pages_service.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'app/services/projects/update_pages_service.rb') diff --git a/app/services/projects/update_pages_service.rb b/app/services/projects/update_pages_service.rb index cc50a23e8f7..00fdd047208 100644 --- a/app/services/projects/update_pages_service.rb +++ b/app/services/projects/update_pages_service.rb @@ -1,6 +1,7 @@ module Projects class UpdatePagesService < BaseService InvaildStateError = Class.new(StandardError) + FailedToExtractError = Class.new(StandardError) BLOCK_SIZE = 32.kilobytes MAX_SIZE = 1.terabyte @@ -30,13 +31,13 @@ module Projects # Check if we did extract public directory archive_public_path = File.join(archive_path, 'public') - raise InvaildStateError, 'pages miss the public folder' unless Dir.exist?(archive_public_path) + raise FailedToExtractError, 'pages miss the public folder' unless Dir.exist?(archive_public_path) raise InvaildStateError, 'pages are outdated' unless latest? deploy_page!(archive_public_path) success end - rescue InvaildStateError => e + rescue InvaildStateError, FailedToExtractError => e register_failure error(e.message) end @@ -75,7 +76,7 @@ module Projects elsif artifacts.ends_with?('.zip') extract_zip_archive!(temp_path) else - raise 'unsupported artifacts format' + raise FailedToExtractError, 'unsupported artifacts format' end end @@ -84,17 +85,17 @@ module Projects %W(dd bs=#{BLOCK_SIZE} count=#{blocks}), %W(tar -x -C #{temp_path} #{SITE_PATH}), err: '/dev/null') - raise 'pages failed to extract' unless results.compact.all?(&:success?) + raise FailedToExtractError, 'pages failed to extract' unless results.compact.all?(&:success?) end def extract_zip_archive!(temp_path) - raise 'missing artifacts metadata' unless build.artifacts_metadata? + raise FailedToExtractError, 'missing artifacts metadata' unless build.artifacts_metadata? # Calculate page size after extract public_entry = build.artifacts_metadata_entry(SITE_PATH, recursive: true) if public_entry.total_size > max_size - raise "artifacts for pages are too large: #{public_entry.total_size}" + raise FailedToExtractError, "artifacts for pages are too large: #{public_entry.total_size}" end # Requires UnZip at least 6.00 Info-ZIP. @@ -103,7 +104,7 @@ module Projects # We add * to end of SITE_PATH, because we want to extract SITE_PATH and all subdirectories site_path = File.join(SITE_PATH, '*') unless system(*%W(unzip -qq -n #{artifacts} #{site_path} -d #{temp_path})) - raise 'pages failed to extract' + raise FailedToExtractError, 'pages failed to extract' end end -- cgit v1.2.1