Class: Gemstar::CacheWarmer

Inherits:
Object
  • Object
show all
Defined in:
lib/gemstar/cache_warmer.rb

Constant Summary collapse

DEFAULT_THREADS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io: $stderr, debug: false, thread_count: DEFAULT_THREADS, detail_cache_fetcher: nil) ⇒ CacheWarmer

Returns a new instance of CacheWarmer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gemstar/cache_warmer.rb', line 10

def initialize(io: $stderr, debug: false, thread_count: DEFAULT_THREADS, detail_cache_fetcher: nil)
  @io = io
  @debug = debug
  @thread_count = thread_count
  @detail_cache_fetcher = detail_cache_fetcher
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @queue = []
  @queued = Set.new
  @in_progress = Set.new
  @completed = Set.new
  @workers = []
  @started = false
  @stopping = false
  @total = 0
  @completed_count = 0
end

Instance Attribute Details

#detail_cache_fetcher=(value) ⇒ Object (writeonly)

Sets the attribute detail_cache_fetcher

Parameters:

  • value

    the value to set the attribute detail_cache_fetcher to.



28
29
30
# File 'lib/gemstar/cache_warmer.rb', line 28

def detail_cache_fetcher=(value)
  @detail_cache_fetcher = value
end

Instance Method Details

#enqueue_many(package_states) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gemstar/cache_warmer.rb', line 30

def enqueue_many(package_states)
  states = normalize_package_states(package_states)

  @mutex.synchronize do
    return self if @stopping

    states.each do |package_state|
      key = package_key(package_state)
      next if @completed.include?(key) || @queued.include?(key) || @in_progress.include?(key)

      @queue << package_state
      @queued << key
    end
    @total += states.count
    start_workers_unlocked unless @started
  end

  log "Background cache refresh queued for #{states.count} packages."
  @condition.broadcast
  self
end

#pending?(package_name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
# File 'lib/gemstar/cache_warmer.rb', line 104

def pending?(package_name)
  @mutex.synchronize do
    @queue.any? { |item| item[:name] == package_name || item.dig(:source, :package_name) == package_name } ||
      @in_progress.any? { |key| key.end_with?(":#{package_name}") }
  end
end

#prioritize(package_name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gemstar/cache_warmer.rb', line 52

def prioritize(package_name)
  @mutex.synchronize do
    existing = @queue.find do |item|
      item[:name] == package_name || item.dig(:source, :package_name) == package_name
    end

    if existing
      key = package_key(existing)
      return if @completed.include?(key) || @in_progress.include?(key)
      @queue.delete(existing)
      @queue.unshift(existing)
    else
      synthetic = {
        name: package_name,
        package_scope: "gems",
        source: {}
      }
      key = package_key(synthetic)
      return if @completed.include?(key) || @in_progress.include?(key)
      @queued << key
      @queue.unshift(synthetic)
      @total += 1
    end
    start_workers_unlocked unless @started
  end

  log "Prioritized #{package_name}"
  @condition.broadcast
end

#shutdown(timeout: 0.5) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gemstar/cache_warmer.rb', line 82

def shutdown(timeout: 0.5)
  workers = nil

  @mutex.synchronize do
    @stopping = true
    @queue.clear
    @queued.clear
    @condition.broadcast
    workers = @workers.dup
  end

  workers.each do |worker|
    worker.join(timeout)
    worker.kill if worker.alive?
  end

  @mutex.synchronize do
    @workers.clear
    @started = false
  end
end