Commit b2d0410c authored by James Lopez's avatar James Lopez

refactor code based on feedback

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