Commit 915f2a9c authored by ggelatti's avatar ggelatti Committed by Steve Abrams

Composer cache update worker

Updates composer cache upon
package creation and deletion.
parent 9a0212d1
......@@ -132,6 +132,8 @@ class Packages::Package < ApplicationRecord
scope :order_project_path_desc, -> { joins(:project).reorder('projects.path DESC, id DESC') }
scope :order_by_package_file, -> { joins(:package_files).order('packages_package_files.created_at ASC') }
after_commit :update_composer_cache, on: :destroy, if: -> { composer? }
def self.for_projects(projects)
return none unless projects.any?
......@@ -232,6 +234,12 @@ class Packages::Package < ApplicationRecord
private
def update_composer_cache
return unless composer?
::Packages::Composer::CacheUpdateWorker.perform_async(project_id, name, composer_metadatum.version_cache_sha) # rubocop:disable CodeReuse/Worker
end
def composer_tag_version?
composer? && !Gitlab::Regex.composer_dev_version_regex.match(version.to_s)
end
......
......@@ -17,6 +17,8 @@ module Packages
})
end
::Packages::Composer::CacheUpdateWorker.perform_async(created_package.project_id, created_package.name, nil)
created_package
end
......
......@@ -1908,6 +1908,14 @@
:weight: 2
:idempotent:
:tags: []
- :name: packages_composer_cache_update
:feature_category: :package_registry
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: pages
:feature_category: :pages
:has_external_dependencies:
......
# frozen_string_literal: true
module Packages
module Composer
class CacheUpdateWorker
include ApplicationWorker
feature_category :package_registry
idempotent!
def perform(project_id, package_name, last_page_sha)
project = Project.find_by_id(project_id)
return unless project
Gitlab::Composer::Cache.new(project: project, name: package_name, last_page_sha: last_page_sha).execute
rescue => e
Gitlab::ErrorTracking.log_exception(e, project_id: project_id)
end
end
end
end
---
title: Composer cache update worker
merge_request: 54551
author:
type: other
......@@ -240,6 +240,8 @@
- 1
- - package_repositories
- 1
- - packages_composer_cache_update
- 1
- - pages
- 1
- - pages_domain_ssl_renewal
......
......@@ -854,4 +854,22 @@ RSpec.describe Packages::Package, type: :model do
it_behaves_like 'not enqueuing a sync worker job'
end
end
context 'destroying a composer package' do
let_it_be(:package_name) { 'composer-package-name' }
let_it_be(:json) { { 'name' => package_name } }
let_it_be(:project) { create(:project, :custom_repo, files: { 'composer.json' => json.to_json } ) }
let!(:package) { create(:composer_package, :with_metadatum, project: project, name: package_name, version: '1.0.0', json: json) }
before do
Gitlab::Composer::Cache.new(project: project, name: package_name).execute
package.composer_metadatum.reload
end
it 'schedule the update job' do
expect(::Packages::Composer::CacheUpdateWorker).to receive(:perform_async).with(project.id, package_name, package.composer_metadatum.version_cache_sha)
package.destroy!
end
end
end
......@@ -28,6 +28,8 @@ RSpec.describe Packages::Composer::CreatePackageService do
let(:branch) { project.repository.find_branch('master') }
it 'creates the package' do
expect(::Packages::Composer::CacheUpdateWorker).to receive(:perform_async).with(project.id, package_name, nil)
expect { subject }
.to change { Packages::Package.composer.count }.by(1)
.and change { Packages::Composer::Metadatum.count }.by(1)
......@@ -54,6 +56,8 @@ RSpec.describe Packages::Composer::CreatePackageService do
end
it 'creates the package' do
expect(::Packages::Composer::CacheUpdateWorker).to receive(:perform_async).with(project.id, package_name, nil)
expect { subject }
.to change { Packages::Package.composer.count }.by(1)
.and change { Packages::Composer::Metadatum.count }.by(1)
......@@ -80,6 +84,8 @@ RSpec.describe Packages::Composer::CreatePackageService do
end
it 'does not create a new package' do
expect(::Packages::Composer::CacheUpdateWorker).to receive(:perform_async).with(project.id, package_name, nil)
expect { subject }
.to change { Packages::Package.composer.count }.by(0)
.and change { Packages::Composer::Metadatum.count }.by(0)
......@@ -101,6 +107,8 @@ RSpec.describe Packages::Composer::CreatePackageService do
let!(:other_package) { create(:package, name: package_name, version: 'dev-master', project: other_project) }
it 'creates the package' do
expect(::Packages::Composer::CacheUpdateWorker).to receive(:perform_async).with(project.id, package_name, nil)
expect { subject }
.to change { Packages::Package.composer.count }.by(1)
.and change { Packages::Composer::Metadatum.count }.by(1)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Composer::CacheUpdateWorker, type: :worker do
describe '#perform' do
let_it_be(:package_name) { 'sample-project' }
let_it_be(:json) { { 'name' => package_name } }
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, :custom_repo, files: { 'composer.json' => json.to_json }, group: group) }
let(:last_sha) { nil }
let!(:package) { create(:composer_package, :with_metadatum, project: project, name: package_name, version: '1.0.0', json: json) }
let(:job_args) { [project.id, package_name, last_sha] }
subject { described_class.new.perform(*job_args) }
before do
stub_composer_cache_object_storage
end
include_examples 'an idempotent worker' do
context 'creating a package' do
it 'updates the cache' do
expect { subject }.to change { Packages::Composer::CacheFile.count }.by(1)
end
end
context 'deleting a package' do
let!(:last_sha) do
Gitlab::Composer::Cache.new(project: project, name: package_name).execute
package.reload.composer_metadatum.version_cache_sha
end
before do
package.destroy!
end
it 'marks the file for deletion' do
expect { subject }.not_to change { Packages::Composer::CacheFile.count }
cache_file = Packages::Composer::CacheFile.last
expect(cache_file.reload.delete_at).not_to be_nil
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment