debian-mirror-gitlab/app/workers/repository_check/single_repository_worker.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
1.9 KiB
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
module RepositoryCheck
2020-04-08 14:13:33 +05:30
class SingleRepositoryWorker # rubocop:disable Scalability/IdempotentWorker
2018-03-17 18:26:18 +05:30
include ApplicationWorker
2021-06-08 01:23:25 +05:30
2021-10-27 15:23:28 +05:30
data_consistency :always
2021-06-08 01:23:25 +05:30
sidekiq_options retry: 3
2016-11-03 12:29:30 +05:30
include RepositoryCheckQueue
2016-06-02 11:05:42 +05:30
def perform(project_id)
project = Project.find(project_id)
2018-10-15 14:42:47 +05:30
healthy = project_healthy?(project)
update_repository_check_status(project, healthy)
end
private
def update_repository_check_status(project, healthy)
2016-06-02 11:05:42 +05:30
project.update_columns(
2018-10-15 14:42:47 +05:30
last_repository_check_failed: !healthy,
2020-07-28 23:09:34 +05:30
last_repository_check_at: Time.current
2016-06-02 11:05:42 +05:30
)
end
2018-10-15 14:42:47 +05:30
def project_healthy?(project)
repo_healthy?(project) && wiki_repo_healthy?(project)
end
2016-06-02 11:05:42 +05:30
2018-10-15 14:42:47 +05:30
def repo_healthy?(project)
return true unless has_changes?(project)
2016-06-02 11:05:42 +05:30
2018-10-15 14:42:47 +05:30
git_fsck(project.repository)
end
def wiki_repo_healthy?(project)
return true unless has_wiki_changes?(project)
git_fsck(project.wiki.repository)
2016-06-02 11:05:42 +05:30
end
def git_fsck(repository)
2018-03-17 18:26:18 +05:30
return false unless repository.exists?
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
repository.raw_repository.fsck
true
rescue Gitlab::Git::Repository::GitError => e
2021-09-30 23:02:18 +05:30
Gitlab::RepositoryCheckLogger.error("#{repository.full_path}: #{e.message}")
2018-03-17 18:26:18 +05:30
false
2016-06-02 11:05:42 +05:30
end
2016-06-22 15:30:34 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-10-15 14:42:47 +05:30
def has_changes?(project)
2016-06-22 15:30:34 +05:30
Project.with_push.exists?(project.id)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-10-15 14:42:47 +05:30
def has_wiki_changes?(project)
return false unless project.wiki_enabled?
# Historically some projects never had their wiki repos initialized;
# this happens on project creation now. Let's initialize an empty repo
# if it is not already there.
return false unless project.create_wiki
has_changes?(project)
end
2016-06-02 11:05:42 +05:30
end
end
2020-04-22 19:07:51 +05:30
2021-06-08 01:23:25 +05:30
RepositoryCheck::SingleRepositoryWorker.prepend_mod_with('RepositoryCheck::SingleRepositoryWorker')