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

71 lines
1.8 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
class SingleRepositoryWorker
2018-03-17 18:26:18 +05:30
include ApplicationWorker
2016-11-03 12:29:30 +05:30
include RepositoryCheckQueue
2016-06-02 11:05:42 +05:30
2019-12-04 20:38:33 +05:30
prepend_if_ee('::EE::RepositoryCheck::SingleRepositoryWorker') # rubocop: disable Cop/InjectEnterpriseEditionModule
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,
2017-09-10 17:25:29 +05:30
last_repository_check_at: Time.now
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
Gitlab::RepositoryCheckLogger.error(e.message)
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