Class: Gemstar::CacheWarmer

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

Constant Summary collapse

DEFAULT_THREADS =
10

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CacheWarmer.



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

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

Instance Method Details

#enqueue_many(package_states) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gemstar/cache_warmer.rb', line 24

def enqueue_many(package_states)
  states = normalize_package_states(package_states)

  @mutex.synchronize do
    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)


74
75
76
77
78
79
# File 'lib/gemstar/cache_warmer.rb', line 74

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gemstar/cache_warmer.rb', line 44

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