Class: Gitlab::GithubImport::PageCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/github_import/page_counter.rb

Overview

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.

Constant Summary collapse

CACHE_KEY =

The base cache key to use for storing the last page number.

'github-importer/page-counter/%{project}/%{collection}'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, collection) ⇒ PageCounter

Returns a new instance of PageCounter.


14
15
16
# File 'lib/gitlab/github_import/page_counter.rb', line 14

def initialize(project, collection)
  @cache_key = CACHE_KEY % { project: project.id, collection: collection }
end

Instance Attribute Details

#cache_keyObject (readonly)

Returns the value of attribute cache_key


9
10
11
# File 'lib/gitlab/github_import/page_counter.rb', line 9

def cache_key
  @cache_key
end

Instance Method Details

#currentObject

Returns the current value from the cache.


26
27
28
# File 'lib/gitlab/github_import/page_counter.rb', line 26

def current
  Gitlab::Cache::Import::Caching.read_integer(cache_key) || 1
end

#set(page) ⇒ Object

Sets the page number to the given value.

Returns true if the page number was overwritten, false otherwise.


21
22
23
# File 'lib/gitlab/github_import/page_counter.rb', line 21

def set(page)
  Gitlab::Cache::Import::Caching.write_if_greater(cache_key, page)
end