-
-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add job to upload pre-rendered versions files to S3
- Loading branch information
Showing
6 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class UploadVersionsFileJob < ApplicationJob | ||
queue_with_priority PRIORITIES.fetch(:push) | ||
|
||
include GoodJob::ActiveJobExtensions::Concurrency | ||
good_job_control_concurrency_with( | ||
# Maximum number of jobs with the concurrency key to be | ||
# concurrently enqueued (excludes performing jobs) | ||
# | ||
# Because the job only uses current state at time of perform, | ||
# it makes no sense to enqueue more than one at a time | ||
enqueue_limit: good_job_concurrency_enqueue_limit(default: 1), | ||
perform_limit: good_job_concurrency_perform_limit(default: 1), | ||
key: name | ||
) | ||
|
||
def perform | ||
versions_path = Rails.application.config.rubygems["versions_file_location"] | ||
versions_file = CompactIndex::VersionsFile.new(versions_path) | ||
from_date = versions_file.updated_at | ||
|
||
logger.info "Generating versions file from #{from_date}" | ||
|
||
extra_gems = GemInfo.compact_index_versions(from_date) | ||
response_body = CompactIndex.versions(versions_file, extra_gems) | ||
|
||
md5 = Digest::MD5.new.update(response_body) | ||
checksum_sha256 = Digest::SHA256.base64digest(response_body) | ||
|
||
response = RubygemFs.compact_index.store( | ||
"versions", response_body, | ||
metadata: { "surrogate-key" => "versions s3-compact-index s3-versions" }, | ||
cache_control: "max-age=60, public", | ||
content_type: "text/plain; charset=utf-8", | ||
checksum_sha256:, | ||
content_md5: md5.base64digest | ||
) | ||
|
||
logger.info(message: "Uploading versions file succeeded", response:) | ||
|
||
FastlyPurgeJob.perform_later(key: "s3-versions", soft: true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "test_helper" | ||
|
||
class UploadVersionsFileJobTest < ActiveJob::TestCase | ||
test "uploads the versions file" do | ||
version = create(:version) | ||
info_checksum = GemInfo.new(version.rubygem.name).info_checksum | ||
version.update!(info_checksum:) | ||
|
||
UploadVersionsFileJob.perform_now | ||
|
||
content = <<~VERSIONS | ||
created_at: 2015-08-23T17:22:53-07:00 | ||
--- | ||
old_gem_one 1 e63fe62df0f1f459d2f70986f79745c6 | ||
old_gem_two 0.1.0,0.1.1,0.1.2,0.2.0,0.2.1,0.3.0,0.4.0,0.4.1,0.5.0,0.5.1,0.5.2,0.5.3 c54e4b7e14861a5d8c225283b75075f4 | ||
#{version.rubygem.name} #{version.number} #{info_checksum} | ||
VERSIONS | ||
|
||
assert_equal content, RubygemFs.compact_index.get("versions") | ||
|
||
assert_equal( | ||
{ | ||
metadata: { "surrogate-key" => "versions s3-compact-index s3-versions" }, | ||
cache_control: "max-age=60, public", | ||
content_type: "text/plain; charset=utf-8", | ||
checksum_sha256: Digest::SHA256.base64digest(content), | ||
content_md5: Digest::MD5.base64digest(content), | ||
key: "versions" | ||
}, RubygemFs.compact_index.head("versions") | ||
) | ||
|
||
assert_enqueued_with(job: FastlyPurgeJob, args: [{ key: "s3-versions", soft: true }]) | ||
end | ||
end |