Commit b2d0410c authored by James Lopez's avatar James Lopez

refactor code based on feedback

parent 5ad48b83
......@@ -127,13 +127,11 @@ module Geo
@registry ||= Geo::ProjectRegistry.find_or_initialize_by(project_id: project.id)
end
def update_registry(started_at: nil, finished_at: nil)
def update_registry!(started_at: nil, finished_at: nil, attrs: {})
return unless started_at || finished_at
log_info("Updating #{type} sync information")
attrs = {}
if started_at
attrs["last_#{type}_synced_at"] = started_at
attrs["#{type}_retry_count"] = retry_count + 1
......@@ -150,6 +148,15 @@ module Geo
registry.update!(attrs)
end
def fail_registry!(message, error, attrs = {})
log_error(message, error)
attrs["last_#{type}_sync_failure"] = "#{message}: #{error.message}"
attrs["#{type}_retry_count"] = retry_count + 1
registry.update!(attrs)
end
def type
self.class.type
end
......
......@@ -11,7 +11,7 @@ module Geo
def fetch_project_repository(redownload)
log_info('Trying to fetch project repository')
update_registry(started_at: DateTime.now)
update_registry!(started_at: DateTime.now)
if redownload
log_info('Redownloading repository')
......@@ -22,19 +22,17 @@ module Geo
fetch_geo_mirror(project.repository)
end
update_registry(finished_at: DateTime.now, last_repository_sync_failure: nil)
update_registry!(finished_at: DateTime.now, attrs: { last_repository_sync_failure: nil })
log_info('Finished repository sync',
update_delay_s: update_delay_in_seconds,
download_time_s: download_time_in_seconds)
rescue Gitlab::Shell::Error,
Gitlab::Git::RepositoryMirroring::RemoteError,
Geo::EmptyCloneUrlPrefixError => e
fail_registry('Error syncing repository', e)
fail_registry!('Error syncing repository', e)
rescue Gitlab::Git::Repository::NoRepository => e
fail_registry('Invalid repository', e)
log_info('Setting force_to_redownload flag')
registry.update(force_to_redownload_repository: true)
fail_registry!('Invalid repository', e, force_to_redownload_repository: true)
log_info('Expiring caches')
project.repository.after_create
......@@ -55,13 +53,6 @@ module Geo
project.repository
end
def fail_registry(message, error)
log_error(message, error)
registry.update!(last_repository_sync_failure: "#{message}: #{error.message}",
repository_retry_count: retry_count + 1)
end
def retry_count
registry.public_send("#{type}_retry_count") || -1 # rubocop:disable GitlabSecurity/PublicSend
end
......
......@@ -10,7 +10,7 @@ module Geo
def fetch_wiki_repository(redownload)
log_info('Fetching wiki repository')
update_registry(started_at: DateTime.now)
update_registry!(started_at: DateTime.now)
if redownload
log_info('Redownloading wiki')
......@@ -21,7 +21,7 @@ module Geo
fetch_geo_mirror(project.wiki.repository)
end
update_registry(finished_at: DateTime.now, last_wiki_sync_failure: nil)
update_registry!(finished_at: DateTime.now, attrs: { last_wiki_sync_failure: nil})
log_info('Finished wiki sync',
update_delay_s: update_delay_in_seconds,
......@@ -30,12 +30,10 @@ module Geo
Gitlab::Shell::Error,
ProjectWiki::CouldNotCreateWikiError,
Geo::EmptyCloneUrlPrefixError => e
fail_registry('Error syncing wiki repository', e)
fail_registry!('Error syncing wiki repository', e)
rescue Gitlab::Git::Repository::NoRepository => e
fail_registry('Invalid wiki', e)
log_info('Setting force_to_redownload flag')
registry.update(force_to_redownload_wiki: true)
fail_registry!('Invalid wiki', e, force_to_redownload_wiki: true)
ensure
clean_up_temporary_repository if redownload
end
......@@ -48,13 +46,6 @@ module Geo
project.wiki.repository
end
def fail_registry(message, error)
log_error(message, error)
registry.update!(last_wiki_sync_failure: "#{message}: #{error.message}",
wiki_retry_count: retry_count + 1)
end
def retry_count
registry.public_send("#{type}_retry_count") || -1 # rubocop:disable GitlabSecurity/PublicSend
end
......
......@@ -149,9 +149,9 @@ GET /geo_nodes/:id/failures
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
| `type` | string | no | Type of failure (repository/wiki) |
| `type` | string | no | Type of failure (`repository`/`wiki`) |
This function takes pagination parameters `page` and `per_page` to limit the list of failures.
This endpoint uses [Pagination](README.md#pagination).
```bash
curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab.example.com/api/v4/geo_nodes/2/failures
......
......@@ -81,7 +81,7 @@ module API
# Get project registry failures for the current Geo node
#
# Example request:
# GET /geo_nodes/:id/failures/:type
# GET /geo_nodes/:id/failures
desc 'Get project registry failures for the current Geo node' do
success Entities::GeoNode
end
......@@ -95,7 +95,7 @@ module API
forbidden!('Geo node is not the current node.')
end
geo_node = GeoNode.find(params[:id])
geo_node = Gitlab::Geo.current_node
not_found('Geo node not found') unless geo_node
......
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