2017-10-13 16:50:36 +00:00
|
|
|
# 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.
|
2019-08-31 19:25:25 +00:00
|
|
|
CACHE_KEY = 'github-importer/page-counter/%{project}/%{collection}'
|
2017-10-13 16:50:36 +00:00
|
|
|
|
|
|
|
def initialize(project, collection)
|
|
|
|
@cache_key = CACHE_KEY % { project: project.id, collection: collection }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the page number to the given value.
|
|
|
|
#
|
|
|
|
# Returns true if the page number was overwritten, false otherwise.
|
|
|
|
def set(page)
|
2020-03-14 00:09:30 +00:00
|
|
|
Gitlab::Cache::Import::Caching.write_if_greater(cache_key, page)
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the current value from the cache.
|
|
|
|
def current
|
2020-03-14 00:09:30 +00:00
|
|
|
Gitlab::Cache::Import::Caching.read_integer(cache_key) || 1
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
2021-06-10 18:10:05 +00:00
|
|
|
|
|
|
|
def expire!
|
|
|
|
Gitlab::Cache::Import::Caching.expire(cache_key, 0)
|
|
|
|
end
|
2017-10-13 16:50:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|