Commit a0bede92 authored by Jacob Vosmaer's avatar Jacob Vosmaer

More code changes, thanks Robert

parent 525ab25a
...@@ -25,9 +25,11 @@ class RepositoryCheckWorker ...@@ -25,9 +25,11 @@ class RepositoryCheckWorker
private private
# In an ideal world we would use Project.where(...).find_each. # Project.find_each does not support WHERE clauses and
# Unfortunately, calling 'find_each' drops the 'where', so we must build # Project.find_in_batches does not support ordering. So we just build an
# an array of IDs instead. # array of ID's. This is OK because we do it only once an hour, because
# getting ID's from Postgres is not terribly slow, and because no user
# has to sit and wait for this query to finish.
def project_ids def project_ids
limit = 10_000 limit = 10_000
never_checked_projects = Project.where('last_repository_check_at IS NULL').limit(limit). never_checked_projects = Project.where('last_repository_check_at IS NULL').limit(limit).
...@@ -40,17 +42,20 @@ class RepositoryCheckWorker ...@@ -40,17 +42,20 @@ class RepositoryCheckWorker
def try_obtain_lease(id) def try_obtain_lease(id)
# Use a 24-hour timeout because on servers/projects where 'git fsck' is # Use a 24-hour timeout because on servers/projects where 'git fsck' is
# super slow we definitely do not want to run it twice in parallel. # super slow we definitely do not want to run it twice in parallel.
lease = Gitlab::ExclusiveLease.new( Gitlab::ExclusiveLease.new(
"project_repository_check:#{id}", "project_repository_check:#{id}",
timeout: 24.hours timeout: 24.hours
) ).try_obtain
lease.try_obtain
end end
def current_settings def current_settings
# No caching of the settings! If we cache them and an admin disables # No caching of the settings! If we cache them and an admin disables
# this feature, an active RepositoryCheckWorker would keep going for up # this feature, an active RepositoryCheckWorker would keep going for up
# to 1 hour after the feature was disabled. # to 1 hour after the feature was disabled.
ApplicationSetting.current || Gitlab::CurrentSettings.fake_application_settings if Rails.env.test?
Gitlab::CurrentSettings.fake_application_settings
else
ApplicationSetting.current
end
end end
end end
...@@ -5,30 +5,29 @@ class SingleRepositoryCheckWorker ...@@ -5,30 +5,29 @@ class SingleRepositoryCheckWorker
def perform(project_id) def perform(project_id)
project = Project.find(project_id) project = Project.find(project_id)
update(project, success: check(project)) project.update_columns(
last_repository_check_failed: !check(project),
last_repository_check_at: Time.now,
)
end end
private private
def check(project) def check(project)
[project.repository.path_to_repo, project.wiki.wiki.path].all? do |path| [project.repository, project.wiki.repository].all? do |repository|
git_fsck(path) git_fsck(repository.path_to_repo)
end end
end end
def git_fsck(path) def git_fsck(path)
cmd = %W(nice git --git-dir=#{path} fsck) cmd = %W(nice git --git-dir=#{path} fsck)
output, status = Gitlab::Popen.popen(cmd) output, status = Gitlab::Popen.popen(cmd)
return true if status.zero?
if status.zero?
true
else
Gitlab::RepositoryCheckLogger.error("command failed: #{cmd.join(' ')}\n#{output}") Gitlab::RepositoryCheckLogger.error("command failed: #{cmd.join(' ')}\n#{output}")
false false
end end
def update(project, success:)
project.update_columns(
last_repository_check_failed: !success,
last_repository_check_at: Time.now,
)
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