2018-03-17 18:26:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
# PageCounter can be used to keep track of the last imported page of a
|
|
|
|
# collection, allowing workers to resume where they left off in the event of
|
|
|
|
# an error.
|
|
|
|
class PageCounter
|
|
|
|
attr_reader :cache_key
|
|
|
|
|
|
|
|
# The base cache key to use for storing the last page number.
|
2023-03-04 22:38:38 +05:30
|
|
|
CACHE_KEY = '%{import_type}/page-counter/%{object}/%{collection}'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def initialize(object, collection, import_type = 'github-importer')
|
|
|
|
@cache_key = CACHE_KEY % { import_type: import_type, object: object.id, collection: collection }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the page number to the given value.
|
|
|
|
#
|
|
|
|
# Returns true if the page number was overwritten, false otherwise.
|
|
|
|
def set(page)
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::Cache::Import::Caching.write_if_greater(cache_key, page)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the current value from the cache.
|
|
|
|
def current
|
2020-04-08 14:13:33 +05:30
|
|
|
Gitlab::Cache::Import::Caching.read_integer(cache_key) || 1
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
def expire!
|
|
|
|
Gitlab::Cache::Import::Caching.expire(cache_key, 0)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|